Skip to content

Commit

Permalink
feat(rest-api-server): add swarm stamp syncer
Browse files Browse the repository at this point in the history
  • Loading branch information
PJColombo committed Jun 14, 2024
1 parent fb9c084 commit e74971f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-terms-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blobscan/rest-api-server": minor
---

Added Swarm stamp syncer
2 changes: 1 addition & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ GOOGLE_STORAGE_ENABLED=true

BEE_DEBUG_ENDPOINT=http://localhost:1635
BEE_ENDPOINT=http://localhost:1633

SWARM_BATCH_ID=f89e63edf757f06e89933761d6d46592d03026efb9871f9d244f34da86b6c242

FILE_SYSTEM_STORAGE_PATH=test-blobscan-blobs

Expand Down
2 changes: 1 addition & 1 deletion apps/rest-api-server/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export const env = createEnv({
METRICS_ENABLED: booleanSchema.default("false"),
REDIS_URI: z.string().default("redis://localhost:6379"),
DENCUN_FORK_SLOT: z.coerce.number().optional(),
SYNCER_SWARM_STAMP_CRON_PATTERN: z.string().default("*/15 * * * *"),
STATS_SYNCER_DAILY_CRON_PATTERN: z.string().default("30 0 * * * *"),
STATS_SYNCER_OVERALL_CRON_PATTERN: z.string().default("*/15 * * * *"),
SWARM_BATCH_ID: z.string().optional(),
SWARM_STORAGE_ENABLED: booleanSchema.default("false"),
SWARM_SYNCER_CRON: z.string().default("42 * * * *"),
SENTRY_DSN_API: z.string().url().optional(),
},

Expand Down
56 changes: 43 additions & 13 deletions apps/rest-api-server/src/syncers.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,62 @@
/* eslint-disable @typescript-eslint/no-misused-promises */

import type { BaseSyncer } from "@blobscan/syncers";
import {
DailyStatsSyncer,
OverallStatsSyncer,
SwarmStampSyncer,
createRedisConnection,
} from "@blobscan/syncers";

import { env } from "./env";
import { logger } from "./logger";
import { getNetworkDencunForkSlot } from "./utils";

export function setUpSyncers() {
const connection = createRedisConnection(env.REDIS_URI);
const syncers: BaseSyncer[] = [];

if (env.SWARM_STORAGE_ENABLED) {
if (!env.SWARM_BATCH_ID) {
logger.warn(`Swarm stamp syncer not created: no batch ID provided`);
} else if (!env.BEE_ENDPOINT) {
logger.warn("Swarm stamp syncer not created: no Bee endpoint provided");
} else {
syncers.push(
new SwarmStampSyncer({
cronPattern: env.SYNCER_SWARM_STAMP_CRON_PATTERN,
redisUriOrConnection: connection,
batchId: env.SWARM_BATCH_ID,
beeEndpoint: env.BEE_ENDPOINT,
})
);
}
}

const dailyStatsSyncer = new DailyStatsSyncer({
cronPattern: env.STATS_SYNCER_DAILY_CRON_PATTERN,
redisUriOrConnection: connection,
});
syncers.push(
new DailyStatsSyncer({
cronPattern: env.STATS_SYNCER_DAILY_CRON_PATTERN,
redisUriOrConnection: connection,
})
);

const overallStatsSyncer = new OverallStatsSyncer({
cronPattern: env.STATS_SYNCER_OVERALL_CRON_PATTERN,
redisUriOrConnection: connection,
lowestSlot:
env.DENCUN_FORK_SLOT ?? getNetworkDencunForkSlot(env.NETWORK_NAME),
});
syncers.push(
new OverallStatsSyncer({
cronPattern: env.STATS_SYNCER_OVERALL_CRON_PATTERN,
redisUriOrConnection: connection,
lowestSlot:
env.DENCUN_FORK_SLOT ?? getNetworkDencunForkSlot(env.NETWORK_NAME),
})
);

Promise.all([dailyStatsSyncer.start(), overallStatsSyncer.start()]);
Promise.all(syncers.map((syncer) => syncer.start()));

return () => {
return dailyStatsSyncer.close().finally(() => overallStatsSyncer.close());
let teardownPromise = Promise.resolve();

for (const syncer of syncers) {
teardownPromise = teardownPromise.finally(() => syncer.close());
}

return teardownPromise;
};
}
39 changes: 11 additions & 28 deletions turbo.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,34 @@
{
"$schema": "https://turborepo.org/schema.json",
"globalDependencies": [
"**/.env"
],
"globalDependencies": ["**/.env"],
"pipeline": {
"db:generate": {
"inputs": [
"prisma/schema.prisma"
],
"inputs": ["prisma/schema.prisma"],
"cache": false
},
"push": {
"inputs": [
"prisma/schema.prisma"
],
"inputs": ["prisma/schema.prisma"],
"cache": false
},
"dev": {
"persistent": true,
"cache": false
},
"build": {
"dependsOn": [
"^build",
"^db:generate"
],
"outputs": [
".next/**"
]
"dependsOn": ["^build", "^db:generate"],
"outputs": [".next/**"]
},
"lint": {},
"lint:fix": {},
"svg:format": {},
"test": {
"inputs": [
"test/**/*.test.ts"
]
"inputs": ["test/**/*.test.ts"]
},
"test:ui": {
"inputs": [
"test/**/*.test.ts"
]
"inputs": ["test/**/*.test.ts"]
},
"test:dev": {
"inputs": [
"test/**/*.test.ts"
],
"inputs": ["test/**/*.test.ts"],
"cache": false
},
"test:setup": {
Expand All @@ -55,9 +38,7 @@
"cache": false
},
"type-check": {
"dependsOn": [
"@blobscan/db#db:generate"
],
"dependsOn": ["@blobscan/db#db:generate"],
"cache": false
}
},
Expand Down Expand Up @@ -105,8 +86,10 @@
"REDIS_URI",
"SECRET_KEY",
"SENTRY_DSN_API",
"SYNCER_SWARM_STAMP_CRON_PATTERN",
"SKIP_ENV_VALIDATION",
"SWARM_STORAGE_ENABLED",
"SWARM_BATCH_ID",
"VERCEL_URL",
"TS_NODE"
]
Expand Down

0 comments on commit e74971f

Please sign in to comment.