error handling
This commit is contained in:
parent
233ffc79ea
commit
bf6fc14abe
|
@ -35,6 +35,9 @@ export default async function createNote(text) {
|
|||
.then(data => {
|
||||
return data
|
||||
})
|
||||
.catch(error =>{
|
||||
throw error
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -2,17 +2,32 @@ import getNewPrompt from "./getNewPrompt.mjs"
|
|||
import createNote from "./createNote.mjs"
|
||||
import makeText from "./makeText.mjs"
|
||||
import insertPublished from "./insertPublished.mjs"
|
||||
import logger from "./logger.mjs"
|
||||
const maxCount = 30000000
|
||||
const minCount = 200000
|
||||
|
||||
export default async function daily () {
|
||||
|
||||
try {
|
||||
const prompt = await getNewPrompt({ minCount, maxCount, rarityBias: 0.7 })
|
||||
|
||||
try {
|
||||
const text = makeText(prompt)
|
||||
|
||||
try {
|
||||
const note = await createNote(text)
|
||||
|
||||
insertPublished(note, prompt.word)
|
||||
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')
|
||||
.whereNotNull('pronunciation')
|
||||
.orderByRaw('count desc')
|
||||
.catch(error=>{return error})
|
||||
|
||||
if(prompts instanceof Error){return prompts}
|
||||
.catch(error=>{throw error})
|
||||
|
||||
const getBiasedRng = (min, max, bias, influence) => {
|
||||
const random = Math.random() * (max - min) + min
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import express from "express";
|
||||
import bodyParser from "body-parser";
|
||||
import logger from "./logger.mjs";
|
||||
const app = express()
|
||||
const port = 4000
|
||||
app.use(bodyParser.json())
|
||||
|
@ -12,3 +13,19 @@ app.post('/api', (req,res) => {
|
|||
app.listen(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 => {
|
||||
return res
|
||||
})
|
||||
.catch(error => {
|
||||
return error
|
||||
})
|
||||
.catch(error => {throw error})
|
||||
}
|
||||
//console.log(await insertPublished(sampleRes,'marmalade'))
|
|
@ -1,6 +1,13 @@
|
|||
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({
|
||||
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}]}]',
|
||||
derivative: 0,
|
||||
scientific: 0
|
||||
}
|
||||
}
|
||||
|
||||
export default function makeText(prompt){
|
||||
const childLogger = logger.child({prompt})
|
||||
export default function makeText(prompt) {
|
||||
const childLogger = logger.child({ prompt })
|
||||
childLogger.trace("makeText called")
|
||||
const meanings = JSON.parse(prompt.meanings)
|
||||
const word = prompt.word
|
||||
const pronunciation = prompt.pronunciation
|
||||
const meanings = JSON.parse(prompt.meanings)
|
||||
const word = prompt.word
|
||||
const pronunciation = prompt.pronunciation
|
||||
|
||||
let text = "Today's #micro365 prompt is:\n<small><small><small># </small></small></small>$[x2 $[font.serif **"+word+"**]]\n"
|
||||
+pronunciation+"\n"
|
||||
let text = "Today's #micro365 prompt is:\n<small><small><small># </small></small></small>$[x2 $[font.serif **" + word + "**]]\n"
|
||||
+ pronunciation + "\n"
|
||||
|
||||
|
||||
let meaningsText = "<small>"
|
||||
const maxDefsPerMeaning = [3,1,1,0] //this array must have at least four entries to account for different word types
|
||||
let meaningsIterator = 0
|
||||
for (const meaning of meanings) {
|
||||
if(maxDefsPerMeaning[meaningsIterator]>0){
|
||||
meaningsText = meaningsText+"**"+meaning.type+"**:\n"
|
||||
let meaningsText = "<small>"
|
||||
const maxDefsPerMeaning = [3, 1, 1, 0] //this array must have at least four entries to account for different word types
|
||||
let meaningsIterator = 0
|
||||
for (const meaning of meanings) {
|
||||
if (maxDefsPerMeaning[meaningsIterator] > 0) {
|
||||
meaningsText = meaningsText + "**" + meaning.type + "**:\n"
|
||||
}
|
||||
|
||||
let definitionsIterator = 1
|
||||
for (const definition of meaning.definitions) {
|
||||
if(definitionsIterator<=maxDefsPerMeaning[meaningsIterator]){
|
||||
meaningsText = meaningsText+"- "+removeUrls(definition.definition)+"\n"
|
||||
if (definitionsIterator <= maxDefsPerMeaning[meaningsIterator]) {
|
||||
meaningsText = meaningsText + "- " + removeUrls(definition.definition) + "\n"
|
||||
} else {
|
||||
meaningsText=meaningsText
|
||||
meaningsText = meaningsText
|
||||
break
|
||||
}
|
||||
definitionsIterator++
|
||||
}
|
||||
meaningsIterator++
|
||||
}
|
||||
meaningsText = meaningsText+"</small>"
|
||||
}
|
||||
meaningsText = meaningsText + "</small>"
|
||||
|
||||
let postScript = "#writing #microfiction #vss #"+word
|
||||
return text+meaningsText+postScript
|
||||
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, '')
|
||||
}
|
||||
|
|
|
@ -15,5 +15,5 @@ const delay = ms => new Promise(res=>{setTimeout(res,ms)})
|
|||
do{
|
||||
await delay(1000)
|
||||
await spam()
|
||||
logger.info("Spam!")
|
||||
logger.trace("Spam!")
|
||||
}while(true)
|
|
@ -7,7 +7,8 @@ export default async function todaysPromptAlreadyPublished() {
|
|||
const number = await db('published')
|
||||
.count('* as count')
|
||||
.where('date', isoDate())
|
||||
.catch(error=>{throw error})
|
||||
return number[0].count > 0
|
||||
}
|
||||
|
||||
console.log(await todaysPromptAlreadyPublished())
|
||||
//console.log(await todaysPromptAlreadyPublished())
|
||||
|
|
Loading…
Reference in New Issue