From 676094a856b30d9e90594124fcc88bb98366fe8e Mon Sep 17 00:00:00 2001 From: Satoshi Nakamoto Date: Thu, 28 Sep 2023 19:54:51 -0400 Subject: [PATCH] worker job to clear out unused lnurlp requests after 30 minutes --- worker/index.js | 4 ++++ worker/lnurlp-expire.js | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 worker/lnurlp-expire.js diff --git a/worker/index.js b/worker/index.js index e3cd909b80..ff4c6b97d5 100644 --- a/worker/index.js +++ b/worker/index.js @@ -11,6 +11,7 @@ import { indexItem, indexAllItems } from './search.js' import { timestampItem } from './ots.js' import { computeStreaks, checkStreak } from './streak.js' import { nip57 } from './nostr.js' +import { lnurlpExpire } from './lnurlp-expire.js' import fetch from 'cross-fetch' import { authenticatedLndGrpc } from 'ln-service' @@ -69,6 +70,9 @@ async function work () { await boss.work('views', views(args)) await boss.work('rankViews', rankViews(args)) + // Not a pg-boss job, but still a process to execute on an interval + lnurlpExpire({ models }) + console.log('working jobs') } diff --git a/worker/lnurlp-expire.js b/worker/lnurlp-expire.js new file mode 100644 index 0000000000..9906c703bc --- /dev/null +++ b/worker/lnurlp-expire.js @@ -0,0 +1,19 @@ +import { datePivot } from '../lib/time.js' + +export function lnurlpExpire ({ models }) { + setInterval(async () => { + try { + const { count } = await models.lnUrlpRequest.deleteMany({ + where: { + createdAt: { + // clear out any requests that are older than 30 minutes + lt: datePivot(new Date(), { minutes: -30 }) + } + } + }) + console.log(`deleted ${count} lnurlp requests`) + } catch (err) { + console.error('error occurred deleting lnurlp requests', err) + } + }, 1000 * 60) // execute once per minute +} \ No newline at end of file