error handling
This commit is contained in:
parent
233ffc79ea
commit
bf6fc14abe
|
@ -35,6 +35,9 @@ export default async function createNote(text) {
|
||||||
.then(data => {
|
.then(data => {
|
||||||
return data
|
return data
|
||||||
})
|
})
|
||||||
|
.catch(error =>{
|
||||||
|
throw error
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,17 +2,32 @@ import getNewPrompt from "./getNewPrompt.mjs"
|
||||||
import createNote from "./createNote.mjs"
|
import createNote from "./createNote.mjs"
|
||||||
import makeText from "./makeText.mjs"
|
import makeText from "./makeText.mjs"
|
||||||
import insertPublished from "./insertPublished.mjs"
|
import insertPublished from "./insertPublished.mjs"
|
||||||
|
import logger from "./logger.mjs"
|
||||||
const maxCount = 30000000
|
const maxCount = 30000000
|
||||||
const minCount = 200000
|
const minCount = 200000
|
||||||
|
|
||||||
export default async function daily () {
|
export default async function daily () {
|
||||||
const prompt = await getNewPrompt({ minCount, maxCount, rarityBias: 0.7 })
|
|
||||||
|
|
||||||
const text = makeText(prompt)
|
try {
|
||||||
|
const prompt = await getNewPrompt({ minCount, maxCount, rarityBias: 0.7 })
|
||||||
const note = await createNote(text)
|
try {
|
||||||
|
const text = makeText(prompt)
|
||||||
insertPublished(note, prompt.word)
|
try {
|
||||||
|
const note = await createNote(text)
|
||||||
|
try {
|
||||||
|
await insertPublished(note, prompt.word)
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error, 'insertPublished failed!')
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error, 'createNote failed!')
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error, 'makeText failed!')
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error,'getNewPrompt failed!')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,9 +23,7 @@ export default async function getNewPrompt({ minCount = 200000, maxCount = 30000
|
||||||
.whereRaw('length(word) > 3')
|
.whereRaw('length(word) > 3')
|
||||||
.whereNotNull('pronunciation')
|
.whereNotNull('pronunciation')
|
||||||
.orderByRaw('count desc')
|
.orderByRaw('count desc')
|
||||||
.catch(error=>{return error})
|
.catch(error=>{throw error})
|
||||||
|
|
||||||
if(prompts instanceof Error){return prompts}
|
|
||||||
|
|
||||||
const getBiasedRng = (min, max, bias, influence) => {
|
const getBiasedRng = (min, max, bias, influence) => {
|
||||||
const random = Math.random() * (max - min) + min
|
const random = Math.random() * (max - min) + min
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import bodyParser from "body-parser";
|
import bodyParser from "body-parser";
|
||||||
|
import logger from "./logger.mjs";
|
||||||
const app = express()
|
const app = express()
|
||||||
const port = 4000
|
const port = 4000
|
||||||
app.use(bodyParser.json())
|
app.use(bodyParser.json())
|
||||||
|
@ -11,4 +12,20 @@ app.post('/api', (req,res) => {
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`listening on port ${port}`)
|
console.log(`listening on port ${port}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
process.on('uncaughtException', (err) => {
|
||||||
|
// log the exception
|
||||||
|
logger.fatal(err, 'uncaught exception detected');
|
||||||
|
// shutdown the server gracefully
|
||||||
|
server.close(() => {
|
||||||
|
process.exit(1); // then exit
|
||||||
|
});
|
||||||
|
|
||||||
|
// If a graceful shutdown is not achieved after 1 second,
|
||||||
|
// shut down the process completely
|
||||||
|
setTimeout(() => {
|
||||||
|
process.abort(); // exit immediately and generate a core dump file
|
||||||
|
}, 1000).unref()
|
||||||
|
process.exit(1);
|
||||||
|
});
|
|
@ -57,8 +57,6 @@ export default async function insertPublished(res, word){
|
||||||
.then(res => {
|
.then(res => {
|
||||||
return res
|
return res
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {throw error})
|
||||||
return error
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
//console.log(await insertPublished(sampleRes,'marmalade'))
|
//console.log(await insertPublished(sampleRes,'marmalade'))
|
|
@ -1,6 +1,13 @@
|
||||||
import pino from 'pino'
|
import pino from 'pino'
|
||||||
|
import path from 'path'
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
|
// const fileTransport = pino.transport({
|
||||||
|
// target: 'pino/file',
|
||||||
|
// options: { destination: `${__dirname}/app.log` },
|
||||||
|
// });
|
||||||
|
|
||||||
export default pino({
|
export default pino({
|
||||||
level: process.env.PINO_LOG_LEVEL || 'info',
|
level: process.env.PINO_LOG_LEVEL || 'info',
|
||||||
|
|
|
@ -8,45 +8,45 @@ const sampleData = {
|
||||||
meanings: '[{"type":"noun","definitions":[{"definition":"Malted grain (sprouted grain) (usually barley), used in brewing and otherwise.","form_of":false,"topics":null},{"definition":"Malt liquor, especially malt whisky.","form_of":false,"topics":null},{"definition":"A milkshake with malted milk powder added for flavor.","form_of":false,"topics":null},{"definition":"Maltose-rich sugar derived from malted grain.","form_of":false,"topics":null}]},{"type":"verb","definitions":[{"definition":"To convert a cereal grain into malt by causing it to sprout (by soaking in water) and then halting germination (by drying with hot air) in order to develop enzymes that can break down starches and proteins in the grain.","form_of":false,"topics":null},{"definition":"To become malt.","form_of":false,"topics":null},{"definition":"To drink malt liquor.","form_of":false,"topics":null}]}]',
|
meanings: '[{"type":"noun","definitions":[{"definition":"Malted grain (sprouted grain) (usually barley), used in brewing and otherwise.","form_of":false,"topics":null},{"definition":"Malt liquor, especially malt whisky.","form_of":false,"topics":null},{"definition":"A milkshake with malted milk powder added for flavor.","form_of":false,"topics":null},{"definition":"Maltose-rich sugar derived from malted grain.","form_of":false,"topics":null}]},{"type":"verb","definitions":[{"definition":"To convert a cereal grain into malt by causing it to sprout (by soaking in water) and then halting germination (by drying with hot air) in order to develop enzymes that can break down starches and proteins in the grain.","form_of":false,"topics":null},{"definition":"To become malt.","form_of":false,"topics":null},{"definition":"To drink malt liquor.","form_of":false,"topics":null}]}]',
|
||||||
derivative: 0,
|
derivative: 0,
|
||||||
scientific: 0
|
scientific: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function makeText(prompt){
|
export default function makeText(prompt) {
|
||||||
const childLogger = logger.child({prompt})
|
const childLogger = logger.child({ prompt })
|
||||||
childLogger.trace("makeText called")
|
childLogger.trace("makeText called")
|
||||||
const meanings = JSON.parse(prompt.meanings)
|
const meanings = JSON.parse(prompt.meanings)
|
||||||
const word = prompt.word
|
const word = prompt.word
|
||||||
const pronunciation = prompt.pronunciation
|
const pronunciation = prompt.pronunciation
|
||||||
|
|
||||||
let text = "Today's #micro365 prompt is:\n<small><small><small># </small></small></small>$[x2 $[font.serif **"+word+"**]]\n"
|
let text = "Today's #micro365 prompt is:\n<small><small><small># </small></small></small>$[x2 $[font.serif **" + word + "**]]\n"
|
||||||
+pronunciation+"\n"
|
+ pronunciation + "\n"
|
||||||
|
|
||||||
|
|
||||||
let meaningsText = "<small>"
|
let meaningsText = "<small>"
|
||||||
const maxDefsPerMeaning = [3,1,1,0] //this array must have at least four entries to account for different word types
|
const maxDefsPerMeaning = [3, 1, 1, 0] //this array must have at least four entries to account for different word types
|
||||||
let meaningsIterator = 0
|
let meaningsIterator = 0
|
||||||
for (const meaning of meanings) {
|
for (const meaning of meanings) {
|
||||||
if(maxDefsPerMeaning[meaningsIterator]>0){
|
if (maxDefsPerMeaning[meaningsIterator] > 0) {
|
||||||
meaningsText = meaningsText+"**"+meaning.type+"**:\n"
|
meaningsText = meaningsText + "**" + meaning.type + "**:\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
let definitionsIterator = 1
|
|
||||||
for (const definition of meaning.definitions) {
|
|
||||||
if(definitionsIterator<=maxDefsPerMeaning[meaningsIterator]){
|
|
||||||
meaningsText = meaningsText+"- "+removeUrls(definition.definition)+"\n"
|
|
||||||
} else {
|
|
||||||
meaningsText=meaningsText
|
|
||||||
break
|
|
||||||
}
|
|
||||||
definitionsIterator++
|
|
||||||
}
|
|
||||||
meaningsIterator++
|
|
||||||
}
|
|
||||||
meaningsText = meaningsText+"</small>"
|
|
||||||
|
|
||||||
let postScript = "#writing #microfiction #vss #"+word
|
let definitionsIterator = 1
|
||||||
return text+meaningsText+postScript
|
for (const definition of meaning.definitions) {
|
||||||
|
if (definitionsIterator <= maxDefsPerMeaning[meaningsIterator]) {
|
||||||
|
meaningsText = meaningsText + "- " + removeUrls(definition.definition) + "\n"
|
||||||
|
} else {
|
||||||
|
meaningsText = meaningsText
|
||||||
|
break
|
||||||
|
}
|
||||||
|
definitionsIterator++
|
||||||
|
}
|
||||||
|
meaningsIterator++
|
||||||
|
}
|
||||||
|
meaningsText = meaningsText + "</small>"
|
||||||
|
|
||||||
|
let postScript = "#writing #microfiction #vss #" + word
|
||||||
|
return text + meaningsText + postScript
|
||||||
}
|
}
|
||||||
|
|
||||||
function removeUrls(string){
|
function removeUrls(string) {
|
||||||
return string.replace(/(https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z]{2,}(\.[a-zA-Z]{2,})(\.[a-zA-Z]{2,})?\/[a-zA-Z0-9]{2,}|((https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z]{2,}(\.[a-zA-Z]{2,})(\.[a-zA-Z]{2,})?)|(https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z0-9]{2,}\.[a-zA-Z0-9]{2,}\.[a-zA-Z0-9]{2,}(\.[a-zA-Z0-9]{2,})?/g, '')
|
return string.replace(/(https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z]{2,}(\.[a-zA-Z]{2,})(\.[a-zA-Z]{2,})?\/[a-zA-Z0-9]{2,}|((https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z]{2,}(\.[a-zA-Z]{2,})(\.[a-zA-Z]{2,})?)|(https:\/\/www\.|http:\/\/www\.|https:\/\/|http:\/\/)?[a-zA-Z0-9]{2,}\.[a-zA-Z0-9]{2,}\.[a-zA-Z0-9]{2,}(\.[a-zA-Z0-9]{2,})?/g, '')
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,5 +15,5 @@ const delay = ms => new Promise(res=>{setTimeout(res,ms)})
|
||||||
do{
|
do{
|
||||||
await delay(1000)
|
await delay(1000)
|
||||||
await spam()
|
await spam()
|
||||||
logger.info("Spam!")
|
logger.trace("Spam!")
|
||||||
}while(true)
|
}while(true)
|
|
@ -7,7 +7,8 @@ export default async function todaysPromptAlreadyPublished() {
|
||||||
const number = await db('published')
|
const number = await db('published')
|
||||||
.count('* as count')
|
.count('* as count')
|
||||||
.where('date', isoDate())
|
.where('date', isoDate())
|
||||||
|
.catch(error=>{throw error})
|
||||||
return number[0].count > 0
|
return number[0].count > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(await todaysPromptAlreadyPublished())
|
//console.log(await todaysPromptAlreadyPublished())
|
||||||
|
|
Loading…
Reference in New Issue