Skip to content

Commit

Permalink
feat(locksmith) trying to renew keys before they actually expire (#13471
Browse files Browse the repository at this point in the history
)

trying to renew keys before they actually expire
  • Loading branch information
julien51 committed Mar 19, 2024
1 parent da7aa92 commit 8fb443f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
13 changes: 9 additions & 4 deletions locksmith/src/graphql/datasource/keysToRenew.ts
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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,
},
},
Expand Down
12 changes: 7 additions & 5 deletions 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
Expand Down
20 changes: 12 additions & 8 deletions locksmith/src/worker/tasks/renewal/addRenewalJobs.ts
Expand Up @@ -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) {
Expand All @@ -17,7 +17,8 @@ export const createAddRenewalJobs = (within: number) => {
}

const expiredKeys = await getRenewalKeys({
within,
start,
end,
network: network.id,
})

Expand Down Expand Up @@ -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)
16 changes: 10 additions & 6 deletions locksmith/src/worker/worker.ts
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -92,6 +95,7 @@ export async function startWorker() {
notifyExpiredKeysForNetwork,
notifyExpiringKeysForNetwork,
addRenewalJobs,
addRenewalJobsHourly,
addRenewalJobsDaily,
addRenewalJobsWeekly,
addHookJobs,
Expand Down

0 comments on commit 8fb443f

Please sign in to comment.