Skip to content

Commit

Permalink
Use expiring locks
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Jan 13, 2025
1 parent 98b6447 commit 15c799d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 33 deletions.
39 changes: 13 additions & 26 deletions api/resolvers/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,33 +468,20 @@ const resolvers = {
// we use queryRawUnsafe because Prisma does not support tsrange
// see https://www.prisma.io/docs/orm/overview/databases/postgresql
return await models.$queryRaw`
WITH failed AS (
UPDATE "Invoice"
SET "lockedAt" = now()
WHERE id IN (
SELECT id FROM "Invoice"
WHERE "userId" = ${me.id}
AND "actionState" = 'FAILED'
AND "userCancel" = false
AND "cancelledAt" < now() - ${`${WALLET_RETRY_AFTER_MS} milliseconds`}::interval
AND "cancelledAt" > now() - ${`${WALLET_RETRY_BEFORE_MS} milliseconds`}::interval
AND "lockedAt" IS NULL
AND "paymentAttempt" < ${WALLET_MAX_RETRIES}
ORDER BY id DESC
FOR UPDATE SKIP LOCKED
)
RETURNING *
),
_ AS (
INSERT INTO pgboss.job (name, data, startafter, keepuntil)
SELECT
'unlockInvoice',
jsonb_build_object('id', id),
now() + interval '10 minutes',
now() + interval '15 minutes'
FROM failed
UPDATE "Invoice"
SET "lockedAt" = now()
WHERE id IN (
SELECT id FROM "Invoice"
WHERE "userId" = ${me.id}
AND "actionState" = 'FAILED'
AND "cancelledAt" < now() - ${`${WALLET_RETRY_AFTER_MS} milliseconds`}::interval
AND "cancelledAt" > now() - ${`${WALLET_RETRY_BEFORE_MS} milliseconds`}::interval
AND ("lockedAt" IS NULL OR "lockedAt" < now() - interval '1 minute')
AND "paymentAttempt" < ${WALLET_MAX_RETRIES}
ORDER BY id DESC
FOR UPDATE SKIP LOCKED
)
SELECT * FROM failed`
RETURNING *`
}
},
Wallet: {
Expand Down
4 changes: 1 addition & 3 deletions worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import createPrisma from '@/lib/create-prisma'
import {
checkInvoice, checkPendingDeposits, checkPendingWithdrawals,
checkWithdrawal,
finalizeHodlInvoice, subscribeToWallet,
unlockInvoice
finalizeHodlInvoice, subscribeToWallet
} from './wallet'
import { repin } from './repin'
import { trust } from './trust'
Expand Down Expand Up @@ -103,7 +102,6 @@ async function work () {
await boss.work('autoWithdraw', jobWrapper(autoWithdraw))
await boss.work('checkInvoice', jobWrapper(checkInvoice))
await boss.work('checkWithdrawal', jobWrapper(checkWithdrawal))
await boss.work('unlockInvoice', jobWrapper(unlockInvoice))
// paidAction jobs
await boss.work('paidActionForwarding', jobWrapper(paidActionForwarding))
await boss.work('paidActionForwarded', jobWrapper(paidActionForwarded))
Expand Down
4 changes: 0 additions & 4 deletions worker/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,3 @@ export async function checkPendingWithdrawals (args) {
}
}
}

export async function unlockInvoice ({ data: { id }, models }) {
await models.invoice.update({ where: { id }, data: { lockedAt: null } })
}

0 comments on commit 15c799d

Please sign in to comment.