Skip to content

Commit

Permalink
Add configurable delay before uploading to AWS (#10)
Browse files Browse the repository at this point in the history
* Add configurable delay before uploading to AWS

* UPLOAD_DELAY_MS
  • Loading branch information
fleupold authored Dec 15, 2023
1 parent 6e0a023 commit 2e9a82d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ const config = t.type({
BUCKET_NAME: t.string,
EXTERNAL_ID: t.string,
ROLES_TO_ASSUME: t.string,
UPLOAD_DELAY_MS: t.number,
});

const parsedEnv = {
...process.env,
UPLOAD_DELAY_MS: parseInt(process.env.UPLOAD_DELAY_MS, 10),
};

export type Config = t.TypeOf<typeof config>;
export default pipe(
config.decode(process.env),
config.decode(parsedEnv),
getOrElse(() => {
throw "Configuration error";
})
Expand Down
18 changes: 13 additions & 5 deletions src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router } from "express";
import { RpcBundle, JsonRpcRequest } from "./models";
import { S3Uploader } from "./upload";
import config from "./config";
import config, { Config } from "./config";
import log from "./log";

const routes = Router();
Expand All @@ -26,19 +26,27 @@ routes.post("/", async (req, res) => {
return;
}
const bundle: RpcBundle = request.params[0];
log.debug(`Received Bundle: ${JSON.stringify(bundle)}`);

const bundleId = `${Number(bundle.blockNumber)}_${request.id}`;
log.debug(`Received Bundle ${bundleId}: ${JSON.stringify(bundle)}`);

// Context on spelling https://www.sistrix.com/ask-sistrix/technical-seo/http/http-referrer/
const referrer: string =
(req.headers.referrer as string) || req.headers.referer;
await aws.upload(bundle, bundleId, referrer);

res.json({
jsonrpc: request.jsonrpc,
id: request.id,
result: null,
});

// Only upload after some delay
setTimeout(async () => {
try {
log.debug(`Uploading bundle ${bundleId}`);
await aws.upload(bundle, bundleId, referrer);
} catch (e) {
log.debug(e);
}
}, (config as Config).UPLOAD_DELAY_MS);
} catch (e) {
log.debug(e);
res.status(500).send();
Expand Down

0 comments on commit 2e9a82d

Please sign in to comment.