/
addRenewalJobs.ts
69 lines (64 loc) · 2.38 KB
/
addRenewalJobs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Task } from 'graphile-worker'
import { networks } from '@unlock-protocol/networks'
import { ethers } from 'ethers'
import config from '../../../config/config'
import { getRenewalKeys } from '../../taskUtils/getRenewalKeys'
import normalizer from '../../../utils/normalizer'
export const createAddRenewalJobs = (start: number, end: number) => {
const addRenewalJobs: Task = async (_, helper) => {
for (const network of Object.values(networks)) {
if (network.isTestNetwork && config.isProduction) {
continue
}
if (network.id === 31337) {
continue
}
const expiredKeys = await getRenewalKeys({
start,
end,
network: network.id,
})
for (const key of expiredKeys) {
if (key.lock.version > 11) {
await helper.addJob(
'fiatRenewalJob',
{
keyId: key.tokenId,
lockAddress: normalizer.ethereumAddress(key.lock.address),
network: network.id,
userAddress: normalizer.ethereumAddress(key.owner),
},
{
jobKey: `fiat-renewal-${key.tokenId}-${key.lock.address}-${network.id}`,
maxAttempts: 2,
}
)
}
if (key.lock.tokenAddress !== ethers.constants.AddressZero) {
await helper.addJob(
'cryptoRenewalJob',
{
keyId: key.tokenId,
lockAddress: normalizer.ethereumAddress(key.lock.address),
network: network.id,
userAddress: normalizer.ethereumAddress(key.owner),
},
{
jobKey: `crypto-renewal-${key.tokenId}-${key.lock.address}-${network.id}`,
maxAttempts: 2,
}
)
}
}
}
}
return addRenewalJobs
}
// 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)