@promise-watch/core
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -36,3 +36,3 @@ "use strict"; | ||
return ({ | ||
name: (_a = r.name) !== null && _a !== void 0 ? _a : files[idx], | ||
name: (_a = r.name) !== null && _a !== void 0 ? _a : files[idx].replace("runs/", ""), | ||
run: r.run, | ||
@@ -43,8 +43,8 @@ options: r.options, | ||
} | ||
async function sendNotifications(title, body, notifiers) { | ||
async function sendNotifications({ title, body, notifiers, isSuccess = false }) { | ||
for (const notify of notifiers) { | ||
await notify.send({ title, body }).catch(console.error); | ||
await notify.send({ title, body, isSuccess }).catch(console.error); | ||
} | ||
} | ||
const errorMap = new Map(); | ||
const errors = {}; | ||
async function recursiveRun(page, globalNotifiers = []) { | ||
@@ -54,15 +54,23 @@ var _a; | ||
const notifiers = (_a = options.notifiers) !== null && _a !== void 0 ? _a : globalNotifiers; | ||
const errorStartTime = errorMap.get(name); | ||
try { | ||
await run(); | ||
if (errorStartTime) { | ||
errorMap.delete(name); | ||
const message = `Run is back online! was down for ${(0, time_1.millisecondsToStr)(errorStartTime.getTime())}`; | ||
await sendNotifications(name, message, notifiers); | ||
if (errors[name]) { | ||
const message = `Recovered after ${(0, time_1.millisecondsToStr)(errors[name])}`; | ||
await sendNotifications({ | ||
title: name, | ||
body: message, | ||
notifiers, | ||
isSuccess: true, | ||
}); | ||
delete errors[name]; | ||
} | ||
} | ||
catch (err) { | ||
if (!errorStartTime) { | ||
errorMap.set(name, new Date()); | ||
await sendNotifications(name, err.message, notifiers); | ||
if (!errors[name]) { | ||
errors[name] = Date.now(); | ||
await sendNotifications({ | ||
title: name, | ||
body: err.message, | ||
notifiers, | ||
}); | ||
} | ||
@@ -69,0 +77,0 @@ } |
export declare type SendOptions = { | ||
title: string; | ||
body: string; | ||
isSuccess?: boolean; | ||
}; | ||
@@ -5,0 +6,0 @@ export declare type Notifier = { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.millisecondsToStr = void 0; | ||
function numberEnding(number) { | ||
return (number > 1) ? "s" : ""; | ||
} | ||
function millisecondsToStr(milliseconds) { | ||
function numberEnding(number) { | ||
return (number > 1) ? "s" : ""; | ||
} | ||
let temp = Math.floor(milliseconds / 1000); | ||
let years = Math.floor(temp / 31536000); | ||
if (years) { | ||
return years + " year" + numberEnding(years); | ||
} | ||
let days = Math.floor((temp %= 31536000) / 86400); | ||
if (days) { | ||
return days + " day" + numberEnding(days); | ||
} | ||
let hours = Math.floor((temp %= 86400) / 3600); | ||
if (hours) { | ||
return hours + " hour" + numberEnding(hours); | ||
} | ||
let minutes = Math.floor((temp %= 3600) / 60); | ||
if (minutes) { | ||
return minutes + " minute" + numberEnding(minutes); | ||
} | ||
let seconds = temp % 60; | ||
const seconds = Math.floor((Date.now() - milliseconds) / 1000); | ||
if (seconds) { | ||
@@ -27,0 +10,0 @@ return seconds + " second" + numberEnding(seconds); |
{ | ||
"name": "@promise-watch/core", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "author": "Jason Raimondi <jason@raimondi.us> (https://jasonraimondi.com)", |
@@ -33,3 +33,3 @@ import { resolve } from "path"; | ||
return imports.map((r: RunPage, idx) => ({ | ||
name: r.name ?? files[idx], | ||
name: r.name ?? files[idx].replace("runs/", ""), | ||
run: r.run, | ||
@@ -40,9 +40,11 @@ options: r.options, | ||
async function sendNotifications(title: string, body: string, notifiers: Notifier[]) { | ||
type SendNotifications = { title: string; body: string; notifiers: Notifier[]; isSuccess?: boolean; } | ||
async function sendNotifications({ title, body, notifiers, isSuccess = false }: SendNotifications) { | ||
for (const notify of notifiers) { | ||
await notify.send({ title, body }).catch(console.error); | ||
await notify.send({ title, body, isSuccess }).catch(console.error); | ||
} | ||
} | ||
const errorMap = new Map<string, Date>(); | ||
const errors: Record<string, number> = {}; | ||
@@ -53,11 +55,14 @@ async function recursiveRun(page: Required<RunPage>, globalNotifiers: Notifier[] = []) { | ||
const errorStartTime = errorMap.get(name); | ||
try { | ||
await run(); | ||
if (errorStartTime) { | ||
errorMap.delete(name); | ||
const message = `Run is back online! was down for ${millisecondsToStr(errorStartTime.getTime())}`; | ||
await sendNotifications(name, message, notifiers); | ||
if (errors[name]) { | ||
const message = `Recovered after ${millisecondsToStr(errors[name])}`; | ||
await sendNotifications({ | ||
title: name, | ||
body: message, | ||
notifiers, | ||
isSuccess: true, | ||
}); | ||
delete errors[name]; | ||
} | ||
@@ -67,5 +72,9 @@ } catch (err: any) { | ||
// wait until success before sending another notification | ||
if (!errorStartTime) { | ||
errorMap.set(name, new Date()); | ||
await sendNotifications(name, err.message, notifiers); | ||
if (!errors[name]) { | ||
errors[name] = Date.now(); | ||
await sendNotifications({ | ||
title: name, | ||
body: err.message, | ||
notifiers, | ||
}); | ||
} | ||
@@ -72,0 +81,0 @@ } |
export type SendOptions = { | ||
title: string; | ||
body: string; | ||
isSuccess?: boolean; | ||
}; | ||
@@ -5,0 +6,0 @@ |
@@ -1,31 +0,8 @@ | ||
// https://stackoverflow.com/a/8212878 | ||
// Thank you, stranger | ||
function numberEnding(number: number) { | ||
return (number > 1) ? "s" : ""; | ||
} | ||
export function millisecondsToStr(milliseconds: number) { | ||
const seconds = Math.floor((Date.now() - milliseconds) / 1000); | ||
function numberEnding(number: number) { | ||
return (number > 1) ? "s" : ""; | ||
} | ||
let temp = Math.floor(milliseconds / 1000); | ||
let years = Math.floor(temp / 31536000); | ||
if (years) { | ||
return years + " year" + numberEnding(years); | ||
} | ||
let days = Math.floor((temp %= 31536000) / 86400); | ||
if (days) { | ||
return days + " day" + numberEnding(days); | ||
} | ||
let hours = Math.floor((temp %= 86400) / 3600); | ||
if (hours) { | ||
return hours + " hour" + numberEnding(hours); | ||
} | ||
let minutes = Math.floor((temp %= 3600) / 60); | ||
if (minutes) { | ||
return minutes + " minute" + numberEnding(minutes); | ||
} | ||
let seconds = temp % 60; | ||
if (seconds) { | ||
@@ -32,0 +9,0 @@ return seconds + " second" + numberEnding(seconds); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15388
300