From 8fb443f8746c0054a1f4a79969c9c3e6a5d11a43 Mon Sep 17 00:00:00 2001 From: Julien Genestoux Date: Tue, 19 Mar 2024 11:00:01 -0400 Subject: [PATCH] feat(locksmith) trying to renew keys before they actually expire (#13471) trying to renew keys before they actually expire --- .../src/graphql/datasource/keysToRenew.ts | 13 ++++++++---- .../src/worker/taskUtils/getRenewalKeys.ts | 12 ++++++----- .../worker/tasks/renewal/addRenewalJobs.ts | 20 +++++++++++-------- locksmith/src/worker/worker.ts | 16 +++++++++------ 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/locksmith/src/graphql/datasource/keysToRenew.ts b/locksmith/src/graphql/datasource/keysToRenew.ts index 0a5afc7abfb..cf74988ba7a 100644 --- a/locksmith/src/graphql/datasource/keysToRenew.ts +++ b/locksmith/src/graphql/datasource/keysToRenew.ts @@ -3,14 +3,18 @@ import logger from '../../logger' interface Options { limit: number - start?: number - end?: number + start: number + end: number network: number page: number minimumLockVersion: number allowNativeCurrency?: boolean } +// Catch any key that will expire in the next hour : +// start: 0, +// end: 60 * 60 +// Their expiration date is larger than now - start and smaller than now + end export const getKeysToRenew = async ({ network, start, @@ -20,6 +24,7 @@ export const getKeysToRenew = async ({ limit = 500, allowNativeCurrency = false, }: Options) => { + const now = Math.floor(Date.now() / 1000) try { const subgraph = new SubgraphService() // Pagination starts at 0 @@ -29,8 +34,8 @@ export const getKeysToRenew = async ({ skip, first: limit, where: { - expiration_gte: start, - expiration_lte: end, + expiration_gte: now - start, + expiration_lte: now + end, cancelled: false, }, }, diff --git a/locksmith/src/worker/taskUtils/getRenewalKeys.ts b/locksmith/src/worker/taskUtils/getRenewalKeys.ts index a5dce259c3d..e392dcd6569 100644 --- a/locksmith/src/worker/taskUtils/getRenewalKeys.ts +++ b/locksmith/src/worker/taskUtils/getRenewalKeys.ts @@ -1,15 +1,17 @@ import { getKeysToRenew } from '../../graphql/datasource' +// gets keys to renew that expire(d) within start + now and end + now +// Catch any key that will expire in the next hour : +// createAddRenewalJobs(0, 60 * 60) export const getRenewalKeys = async ({ - within, + start, + end, network, }: { - within: number + start: number + end: number network: number }) => { - // timeframe to check for renewal - const end = Math.floor(Date.now() / 1000) - const start = within ? end - within : undefined const items = [] let more = true let page = 0 diff --git a/locksmith/src/worker/tasks/renewal/addRenewalJobs.ts b/locksmith/src/worker/tasks/renewal/addRenewalJobs.ts index 606e2f85149..f1b8bc7d061 100644 --- a/locksmith/src/worker/tasks/renewal/addRenewalJobs.ts +++ b/locksmith/src/worker/tasks/renewal/addRenewalJobs.ts @@ -5,7 +5,7 @@ import config from '../../../config/config' import { getRenewalKeys } from '../../taskUtils/getRenewalKeys' import normalizer from '../../../utils/normalizer' -export const createAddRenewalJobs = (within: number) => { +export const createAddRenewalJobs = (start: number, end: number) => { const addRenewalJobs: Task = async (_, helper) => { for (const network of Object.values(networks)) { if (network.isTestNetwork && config.isProduction) { @@ -17,7 +17,8 @@ export const createAddRenewalJobs = (within: number) => { } const expiredKeys = await getRenewalKeys({ - within, + start, + end, network: network.id, }) @@ -57,9 +58,12 @@ export const createAddRenewalJobs = (within: number) => { } return addRenewalJobs } -// Run this job frequently to catch any expired keys in the last 30 minutes -export const addRenewalJobs = createAddRenewalJobs(1800) -// Run this job once a day to catch any keys that may have been missed during the last week -export const addRenewalJobsDaily = createAddRenewalJobs(86400 * 7) -// Run this job once a week to catch any keys that may have been missed during the last year -export const addRenewalJobsWeekly = createAddRenewalJobs(86400 * 365) + +// Catch any key that will expire in the next 15 minutes or have expired 15 minutes ago +export const addRenewalJobs = createAddRenewalJobs(60 * 15, 60 * 15) +// Catch any key that will expire in the next hour (this should be most of them!) +export const addRenewalJobsHourly = createAddRenewalJobs(0, 60 * 60) +// Catch any keys that may have been missed during the last week +export const addRenewalJobsDaily = createAddRenewalJobs(60 * 60 * 24 * 7, 0) +// Catch any keys that may have been missed during the last year +export const addRenewalJobsWeekly = createAddRenewalJobs(60 * 60 * 24 * 365, 0) diff --git a/locksmith/src/worker/worker.ts b/locksmith/src/worker/worker.ts index 824f6ed6bff..5f19ae067d9 100644 --- a/locksmith/src/worker/worker.ts +++ b/locksmith/src/worker/worker.ts @@ -3,6 +3,7 @@ import { makeWorkerUtils, run, quickAddJob } from 'graphile-worker' import config from '../config/config' import { addRenewalJobs, + addRenewalJobsHourly, addRenewalJobsWeekly, addRenewalJobsDaily, } from './tasks/renewal/addRenewalJobs' @@ -23,9 +24,10 @@ import exportKeysJob from './tasks/exportKeysJob' const crontabProduction = ` */5 * * * * monitor */2 * * * * allJobs -*/5 * * * * addRenewalJobs -0 0 * * * addRenewalJobsDaily -0 0 * * 0 addRenewalJobsWeekly +*/4 * * * * addRenewalJobs +30 * * * * addRenewalJobsHourly +15 0 * * * addRenewalJobsDaily +45 6 * * 0 addRenewalJobsWeekly */5 * * * * addKeyJobs */5 * * * * addHookJobs 0 0 * * * notifyExpiringKeysForNetwork @@ -36,9 +38,10 @@ const crontabProduction = ` const cronTabTesting = ` */1 * * * * monitor */2 * * * * allJobs -*/1 * * * * addRenewalJobs -0 0 * * * addRenewalJobsDaily -0 0 * * * addRenewalJobsWeekly +*/4 * * * * addRenewalJobs +30 * * * * addRenewalJobsHourly +15 0 * * * addRenewalJobsDaily +45 6 * * 0 addRenewalJobsWeekly */1 * * * * addKeyJobs */1 * * * * addHookJobs 0 0 * * * notifyExpiringKeysForNetwork @@ -92,6 +95,7 @@ export async function startWorker() { notifyExpiredKeysForNetwork, notifyExpiringKeysForNetwork, addRenewalJobs, + addRenewalJobsHourly, addRenewalJobsDaily, addRenewalJobsWeekly, addHookJobs,