Created
June 6, 2017 20:12
-
-
Save hawkins/c585e5255d37811591a5e4b09ce18299 to your computer and use it in GitHub Desktop.
Canary.js - Chirp til you drop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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