From c33beabef6084c2e3930fe05fb86391ca837e0ad Mon Sep 17 00:00:00 2001 From: Adam Janis Date: Sun, 15 Nov 2020 15:40:57 +0100 Subject: [PATCH] feat(cron): detect and save location of last check --- pages/api/triggerCron.js | 2 +- pages/index.js | 6 ++---- src/functions/cronTrigger.js | 13 +++++++++---- src/functions/helpers.js | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pages/api/triggerCron.js b/pages/api/triggerCron.js index c4a1ce3..2cf5056 100644 --- a/pages/api/triggerCron.js +++ b/pages/api/triggerCron.js @@ -2,5 +2,5 @@ import { processCronTrigger } from '../../src/functions/cronTrigger' export default async event => { // used only for local debugging - //return processCronTrigger() + //return processCronTrigger(event) } diff --git a/pages/index.js b/pages/index.js index ab0844c..4dc5c0b 100644 --- a/pages/index.js +++ b/pages/index.js @@ -79,12 +79,10 @@ export default function Index({ ? config.settings.allmonitorsOperational : config.settings.notAllmonitorsOperational} - {typeof window !== 'undefined' ? ( + {kvLastUpdate.metadata && typeof window !== 'undefined' && (
- checked {Math.round((Date.now() - kvLastUpdate) / 1000)} sec ago + checked {Math.round((Date.now() - kvLastUpdate.value) / 1000)} sec ago (from {kvLastUpdate.metadata.loc})
- ) : ( - '' )} diff --git a/src/functions/cronTrigger.js b/src/functions/cronTrigger.js index 93b5811..6deee6c 100644 --- a/src/functions/cronTrigger.js +++ b/src/functions/cronTrigger.js @@ -61,11 +61,16 @@ export async function processCronTrigger(event) { await setKV(kvFailedDayStatusKey, null) } } - - // save last check timestamp - await setKV('lastUpdate', Date.now()) } - await gcMonitors(config) + + // save last check timestamp including PoP location + const res = await fetch("https://www.cloudflare.com/cdn-cgi/trace") + const resText = await res.text() + const loc = /loc=([\w]{2})/.exec(resText)[1] + await setKV('lastUpdate', Date.now(), {loc}) + + // gc monitor statuses + event.waitUntil(gcMonitors(config)) return new Response('OK') } diff --git a/src/functions/helpers.js b/src/functions/helpers.js index 3a2cfcf..c5a730c 100644 --- a/src/functions/helpers.js +++ b/src/functions/helpers.js @@ -11,7 +11,7 @@ export async function getMonitorsHistory() { } export async function getLastUpdate() { - return await getKV('lastUpdate') + return await getKVWithMetadata('lastUpdate') } export async function listKV(prefix = '', cacheTtl = false) {