Skip to content

Commit

Permalink
feat: run cron job to start signature request processing
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pheuberger committed Dec 18, 2024
1 parent 126e4eb commit 4baee53
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"kysely": "^0.27.4",
"lodash": "^4.17.21",
"lru-cache": "^11.0.0",
"node-cron": "^3.0.3",
"pg": "^8.12.0",
"reflect-metadata": "^0.2.2",
"rollup": "^4.12.0",
Expand Down Expand Up @@ -93,6 +94,7 @@
"@swc/cli": "^0.3.12",
"@swc/core": "^1.4.15",
"@types/body-parser": "^1.19.5",
"@types/node-cron": "^3.0.11",
"@types/pg": "^8.11.6",
"@types/sinon": "^17.0.2",
"@types/swagger-ui-express": "^4.1.6",
Expand Down
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions src/cron/SignatureRequestProcessing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import cron from "node-cron";

import SignatureRequestProcessor from "../services/SignatureRequestProcessor.js";

export default class SignatureRequestProcessorCron {
private static instance: SignatureRequestProcessorCron;
private processor: SignatureRequestProcessor;

private constructor() {
this.processor = SignatureRequestProcessor.getInstance();
this.setupCronJob();
}

private setupCronJob() {
// Run every 30 seconds
cron.schedule("*/30 * * * * *", async () => {
try {
await this.processor.processPendingRequests();
} catch (error) {
console.error("Error in signature request processor cron job:", error);
}
});
}

public static start(): void {
if (!SignatureRequestProcessorCron.instance) {
SignatureRequestProcessorCron.instance =
new SignatureRequestProcessorCron();
}
}
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import swaggerUi from "swagger-ui-express";
import swaggerJson from "./__generated__/swagger.json" assert { type: "json" };
import { RegisterRoutes } from "./__generated__/routes/routes.js";
import * as Sentry from "@sentry/node";
import SignatureRequestProcessorCron from "./cron/SignatureRequestProcessing.js";

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

// Start Safe signature request processing cron job
SignatureRequestProcessorCron.start();

app.listen(PORT, () => {
console.log(
`🕸️ Running a GraphQL API server at http://localhost:${PORT}/v1/graphql`,
Expand Down
2 changes: 1 addition & 1 deletion src/services/SignatureRequestProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class SignatureRequestProcessor {
async processPendingRequests(): Promise<void> {
const pendingRequests = await this.getPendingRequests();

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

for (const request of pendingRequests) {
const command = getCommand(request);
Expand Down

0 comments on commit 4baee53

Please sign in to comment.