Skip to content

Instantly share code, notes, and snippets.

@hawkins
Created June 6, 2017 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hawkins/c585e5255d37811591a5e4b09ce18299 to your computer and use it in GitHub Desktop.
Save hawkins/c585e5255d37811591a5e4b09ce18299 to your computer and use it in GitHub Desktop.
Canary.js - Chirp til you drop
const { readFile, appendFile } = require("fs")
let logfile = "canary.log"
const now = () => new Date().toISOString()
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
const appendFileP = (filename, data) =>
new Promise((resolve, reject) => {
appendFile(filename, `\n${data}`, err => {
if (err) reject(err)
else resolve()
})
})
const addToLog = async log => {
const notification = log !== undefined ? log : now()
console.log(notification)
await appendFileP(logfile, notification)
}
const loop = async () => {
while (true) {
await sleep(10000)
addToLog()
}
}
;(() => {
logfile = process.argv[2] || logfile
console.log(`> writing to ${logfile}`)
addToLog(`Started shutdown canary at ${now()}`)
try {
loop()
} catch (err) {
console.error(err)
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment