Skip to content

chore: restart run loop if stale #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { initLogger, fallbackLogger } from '@snapshot-labs/snapshot-sentry';
import initMetrics from './helpers/metrics';
import api from './api';
import pkg from '../package.json';
import { last_mci, run } from './replay';
import { last_mci, lastActivityTime, run } from './replay';
import { closeDatabase } from './helpers/mysql';

const app = express();
Expand All @@ -22,6 +22,15 @@ app.use(bodyParser.urlencoded({ limit: '8mb', extended: false }));
app.use(cors({ maxAge: 86400 }));

app.get('/', async (req, res) => {
const timeSinceLastActivity = (Date.now() - lastActivityTime) / 1000;
const isStale = timeSinceLastActivity > 10 * 60; // 10 minutes

// Restart if stale
if (isStale) {
console.log(`[index] Restarting run loop`);
run();
}

return res.json({
name: pkg.name,
version: pkg.version,
Expand Down
2 changes: 2 additions & 0 deletions src/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { handleCreatedEvent, handleDeletedEvent } from './events';
const hubURL = process.env.HUB_URL || 'https://hub.snapshot.org';

export let last_mci = 0;
export let lastActivityTime = Date.now();

async function getLastMci() {
lastActivityTime = Date.now(); // Update activity time
const query = 'SELECT value FROM _metadatas WHERE id = ? LIMIT 1';
const results = await db.queryAsync(query, ['last_mci']);
last_mci = parseInt(results[0].value);
Expand Down