diff --git a/src/createNote.mjs b/src/createNote.mjs
index 59babb5..7d0cc31 100644
--- a/src/createNote.mjs
+++ b/src/createNote.mjs
@@ -35,6 +35,9 @@ export default async function createNote(text) {
.then(data => {
return data
})
+ .catch(error =>{
+ throw error
+ })
}
diff --git a/src/daily.mjs b/src/daily.mjs
index 44a5531..7d31361 100644
--- a/src/daily.mjs
+++ b/src/daily.mjs
@@ -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 () {
- const prompt = await getNewPrompt({ minCount, maxCount, rarityBias: 0.7 })
- const text = makeText(prompt)
-
- const note = await createNote(text)
-
- insertPublished(note, prompt.word)
+ try {
+ const prompt = await getNewPrompt({ minCount, maxCount, rarityBias: 0.7 })
+ try {
+ const text = makeText(prompt)
+ 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!')
+ }
}
diff --git a/src/getNewPrompt.mjs b/src/getNewPrompt.mjs
index f87801d..1f2d663 100644
--- a/src/getNewPrompt.mjs
+++ b/src/getNewPrompt.mjs
@@ -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
diff --git a/src/index.mjs b/src/index.mjs
index 8e00810..2fb5a42 100644
--- a/src/index.mjs
+++ b/src/index.mjs
@@ -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())
@@ -11,4 +12,20 @@ app.post('/api', (req,res) => {
app.listen(port, () => {
console.log(`listening on port ${port}`)
-})
\ No newline at end of file
+})
+
+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);
+ });
\ No newline at end of file
diff --git a/src/insertPublished.mjs b/src/insertPublished.mjs
index b5aed33..6378965 100644
--- a/src/insertPublished.mjs
+++ b/src/insertPublished.mjs
@@ -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'))
\ No newline at end of file
diff --git a/src/logger.mjs b/src/logger.mjs
index 747018e..78c9688 100644
--- a/src/logger.mjs
+++ b/src/logger.mjs
@@ -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',
diff --git a/src/makeText.mjs b/src/makeText.mjs
index 479fa67..9bf4ab9 100644
--- a/src/makeText.mjs
+++ b/src/makeText.mjs
@@ -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# $[x2 $[font.serif **"+word+"**]]\n"
-+pronunciation+"\n"
+ let text = "Today's #micro365 prompt is:\n# $[x2 $[font.serif **" + word + "**]]\n"
+ + pronunciation + "\n"
-let meaningsText = ""
-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"
- } else {
- meaningsText=meaningsText
- break
- }
- definitionsIterator++
- }
- meaningsIterator++
-}
-meaningsText = meaningsText+""
+ let meaningsText = ""
+ 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 postScript = "#writing #microfiction #vss #"+word
-return text+meaningsText+postScript
+ 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 + ""
+
+ 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, '')
}
diff --git a/src/spam.mjs b/src/spam.mjs
index c9ab0fa..47d5484 100644
--- a/src/spam.mjs
+++ b/src/spam.mjs
@@ -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)
\ No newline at end of file
diff --git a/src/todaysPromptAlreadyPublished.mjs b/src/todaysPromptAlreadyPublished.mjs
index 4f7437f..a2b3184 100644
--- a/src/todaysPromptAlreadyPublished.mjs
+++ b/src/todaysPromptAlreadyPublished.mjs
@@ -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())