Skip to content

Commit 4baee53

Browse files
committed
feat: run cron job to start signature request processing
Without this patch we rely on users making a POST request to /v1/signature-requests/process to kick off signature request processing. Since it's not going to be high load, we want to have a regular cron job that relieves users of that duty and will run signature request validations on autopilot. This way users just have to wait a bit to find their signature requests validated once the Safe message has reached the required threshold. Also made a small change to the log statement in the processor to make it more clear that it's running signature requests, now that a console log will show up every 30 seconds.
1 parent 126e4eb commit 4baee53

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"kysely": "^0.27.4",
6363
"lodash": "^4.17.21",
6464
"lru-cache": "^11.0.0",
65+
"node-cron": "^3.0.3",
6566
"pg": "^8.12.0",
6667
"reflect-metadata": "^0.2.2",
6768
"rollup": "^4.12.0",
@@ -93,6 +94,7 @@
9394
"@swc/cli": "^0.3.12",
9495
"@swc/core": "^1.4.15",
9596
"@types/body-parser": "^1.19.5",
97+
"@types/node-cron": "^3.0.11",
9698
"@types/pg": "^8.11.6",
9799
"@types/sinon": "^17.0.2",
98100
"@types/swagger-ui-express": "^4.1.6",

pnpm-lock.yaml

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import cron from "node-cron";
2+
3+
import SignatureRequestProcessor from "../services/SignatureRequestProcessor.js";
4+
5+
export default class SignatureRequestProcessorCron {
6+
private static instance: SignatureRequestProcessorCron;
7+
private processor: SignatureRequestProcessor;
8+
9+
private constructor() {
10+
this.processor = SignatureRequestProcessor.getInstance();
11+
this.setupCronJob();
12+
}
13+
14+
private setupCronJob() {
15+
// Run every 30 seconds
16+
cron.schedule("*/30 * * * * *", async () => {
17+
try {
18+
await this.processor.processPendingRequests();
19+
} catch (error) {
20+
console.error("Error in signature request processor cron job:", error);
21+
}
22+
});
23+
}
24+
25+
public static start(): void {
26+
if (!SignatureRequestProcessorCron.instance) {
27+
SignatureRequestProcessorCron.instance =
28+
new SignatureRequestProcessorCron();
29+
}
30+
}
31+
}

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import swaggerUi from "swagger-ui-express";
88
import swaggerJson from "./__generated__/swagger.json" assert { type: "json" };
99
import { RegisterRoutes } from "./__generated__/routes/routes.js";
1010
import * as Sentry from "@sentry/node";
11+
import SignatureRequestProcessorCron from "./cron/SignatureRequestProcessing.js";
1112

1213
// @ts-expect-error BigInt is not supported by JSON
1314
BigInt.prototype.toJSON = function () {
@@ -48,6 +49,9 @@ RegisterRoutes(app);
4849
// The error handler must be registered before any other error middleware and after all controllers
4950
Sentry.setupExpressErrorHandler(app);
5051

52+
// Start Safe signature request processing cron job
53+
SignatureRequestProcessorCron.start();
54+
5155
app.listen(PORT, () => {
5256
console.log(
5357
`🕸️ Running a GraphQL API server at http://localhost:${PORT}/v1/graphql`,

src/services/SignatureRequestProcessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class SignatureRequestProcessor {
2222
async processPendingRequests(): Promise<void> {
2323
const pendingRequests = await this.getPendingRequests();
2424

25-
console.log(`Found ${pendingRequests.length} pending requests`);
25+
console.log(`Found ${pendingRequests.length} pending signature requests`);
2626

2727
for (const request of pendingRequests) {
2828
const command = getCommand(request);

0 commit comments

Comments
 (0)