-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathworkers.ts
More file actions
163 lines (143 loc) · 6.01 KB
/
workers.ts
File metadata and controls
163 lines (143 loc) · 6.01 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Worker } from 'bullmq'
import { redisBullMQ } from 'redis/clientInstance'
import { DEFAULT_WORKER_LOCK_DURATION, NETWORK_SLUGS_FROM_IDS } from 'constants/index'
import { multiBlockchainClient } from 'services/chronikService'
import { connectAllTransactionsToPrices, fetchUnconfirmedNonOrphanedTransactions, markTransactionConfirmed, markTransactionsOrphaned } from 'services/transactionService'
import { cleanupExpiredClientPayments } from 'services/clientPaymentService'
import * as priceService from 'services/priceService'
export const syncCurrentPricesWorker = async (queueName: string): Promise<void> => {
const worker = new Worker(
queueName,
async (job) => {
console.log(`job ${job.id as string}: syncing current prices...`)
await priceService.syncCurrentPrices()
},
{
connection: redisBullMQ,
lockDuration: DEFAULT_WORKER_LOCK_DURATION
}
)
worker.on('completed', job => {
console.log('syncing of current prices finished')
})
worker.on('failed', (job, err) => {
if (job !== undefined) {
console.log('syncing of current prices FAILED')
console.log(`error for initial syncing of current prices: ${err.message}`)
}
})
}
export const syncBlockchainAndPricesWorker = async (queueName: string): Promise<void> => {
const worker = new Worker(
queueName,
async (job) => {
console.log(`job ${job.id as string}: syncing missed prices, transactions and connecting them...`)
await priceService.syncPastDaysNewerPrices()
await multiBlockchainClient.syncMissedTransactions()
await connectAllTransactionsToPrices()
},
{
connection: redisBullMQ,
lockDuration: DEFAULT_WORKER_LOCK_DURATION
}
)
worker.on('completed', (job) => {
// teardown
void (async () => {
console.log('Cleaning up MultiBlockchainClient global instance...')
await multiBlockchainClient.destroy()
console.log('Done.')
console.log(`job ${job.id as string}: blockchain + prices sync finished`)
})()
})
worker.on('failed', (job, err) => {
void (async () => {
if (job != null) {
console.error(`job ${job.id as string}: FAILED — ${err.message}`)
}
await multiBlockchainClient.destroy()
})()
})
}
export const cleanupClientPaymentsWorker = async (queueName: string): Promise<void> => {
const worker = new Worker(
queueName,
async (job) => {
console.log(`[CLIENT_PAYMENT CLEANUP] job ${job.id as string}: running expired payment cleanup...`)
await cleanupExpiredClientPayments()
console.log('[CLIENT_PAYMENT CLEANUP] cleanup finished.')
},
{
connection: redisBullMQ,
lockDuration: DEFAULT_WORKER_LOCK_DURATION
}
)
worker.on('completed', job => {
console.log(`[CLIENT_PAYMENT CLEANUP] job ${job.id as string}: completed successfully`)
})
worker.on('failed', (job, err) => {
console.error(`[CLIENT_PAYMENT CLEANUP] job ${job?.id as string}: FAILED — ${err.message}`)
})
}
export const verifyUnconfirmedTransactionsWorker = async (queueName: string): Promise<void> => {
const worker = new Worker(
queueName,
async (job) => {
console.log(`[UNCONFIRMED TX VERIFICATION] job ${job.id as string}: checking unconfirmed transactions...`)
const unconfirmedTxs = await fetchUnconfirmedNonOrphanedTransactions()
console.log(`[UNCONFIRMED TX VERIFICATION] Found ${unconfirmedTxs.length} unconfirmed transactions to verify`)
let confirmedCount = 0
let orphanedCount = 0
const oneDayAgo = Math.floor(Date.now() / 1000) - (24 * 60 * 60)
// Group transactions by hash to avoid duplicate chronik calls
const txsByHash = new Map<string, typeof unconfirmedTxs>()
for (const tx of unconfirmedTxs) {
const existing = txsByHash.get(tx.hash) ?? []
existing.push(tx)
txsByHash.set(tx.hash, existing)
}
for (const [hash, txs] of txsByHash.entries()) {
const networkId = txs[0].address.networkId
const networkSlug = NETWORK_SLUGS_FROM_IDS[networkId]
try {
const txDetails = await multiBlockchainClient.getTransactionDetails(hash, networkSlug)
// If tx has a block, it's confirmed
if (txDetails.block?.height !== undefined && txDetails.block.height !== null) {
const blockTimestamp = Number(txDetails.block.timestamp)
await markTransactionConfirmed(hash, blockTimestamp)
confirmedCount += txs.length
console.log(`[UNCONFIRMED TX VERIFICATION] Marked tx ${hash} as confirmed`)
} else {
// Still unconfirmed - txs over a day old should always be either confirmed or orphaned
const oldestTx = txs.reduce((oldest, tx) => tx.timestamp < oldest.timestamp ? tx : oldest, txs[0])
if (oldestTx.timestamp < oneDayAgo) {
console.warn(`[UNCONFIRMED TX VERIFICATION] WARNING: tx ${hash} is over 1 day old and still unconfirmed (address: ${oldestTx.address.address})`)
}
}
} catch (err: any) {
const errMsg = String(err?.message ?? err)
const is404 = /not found in the index|404/.test(errMsg)
if (is404) {
// Transaction no longer exists on the network
await markTransactionsOrphaned(hash)
orphanedCount += txs.length
console.log(`[UNCONFIRMED TX VERIFICATION] Marked tx ${hash} as orphaned (not found)`)
} else {
console.error(`[UNCONFIRMED TX VERIFICATION] Error checking tx ${hash}: ${errMsg}`)
}
}
}
console.log(`[UNCONFIRMED TX VERIFICATION] Finished: ${confirmedCount} confirmed, ${orphanedCount} orphaned`)
},
{
connection: redisBullMQ,
lockDuration: DEFAULT_WORKER_LOCK_DURATION
}
)
worker.on('completed', job => {
console.log(`[UNCONFIRMED TX VERIFICATION] job ${job.id as string}: completed successfully`)
})
worker.on('failed', (job, err) => {
console.error(`[UNCONFIRMED TX VERIFICATION] job ${job?.id as string}: FAILED — ${err.message}`)
})
}