Skip to content

fix: better scheduling after restarts#150

Closed
SgtPooki wants to merge 7 commits intomainfrom
fix/better-scheduling
Closed

fix: better scheduling after restarts#150
SgtPooki wants to merge 7 commits intomainfrom
fix/better-scheduling

Conversation

@SgtPooki
Copy link
Copy Markdown
Collaborator

  • Schedule deal/retrieval runs from last created_at + interval; use start offsets only for fresh DBs.
  • Metrics jobs now anchor to created_at/refreshed_at with initial offsets; weekly cleanup stays cron‑based.
  • Documented start offsets as “first run only” in env var docs.

fixes #119

also removes wall-clock time dependency of cron jobs to address #119

fixes #119
@vercel
Copy link
Copy Markdown

vercel Bot commented Jan 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
dealbot-web Ready Ready Preview, Comment Jan 22, 2026 8:23pm

Request Review

@SgtPooki SgtPooki self-assigned this Jan 22, 2026
@SgtPooki SgtPooki marked this pull request as ready for review January 22, 2026 20:23
Copilot AI review requested due to automatic review settings January 22, 2026 20:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the scheduling system to handle restarts more gracefully by anchoring job schedules to database timestamps rather than relying solely on startup offsets. The system now uses the last created_at or refreshed_at timestamp from the database to calculate when the next job should run, making scheduling consistent across restarts and redeployments.

Changes:

  • Replaced cron-based scheduling with dynamic setTimeout-based scheduling that anchors to database timestamps
  • Startup offsets are now only used for fresh databases with no existing records
  • Updated documentation to clarify that start offset environment variables only apply to the first run

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
docs/environment-variables.md Updated documentation for DEAL_START_OFFSET_SECONDS, RETRIEVAL_START_OFFSET_SECONDS, and METRICS_START_OFFSET_SECONDS to clarify they only apply when no database rows exist
apps/backend/src/scheduler/scheduler.service.ts Replaced scheduleJobWithOffset with new dynamic scheduling methods (scheduleInitialRun, scheduleRunAt, executeScheduledJob) that query database timestamps; removed SchedulerRegistry dependency
apps/backend/src/scheduler/scheduler.service.spec.ts Added tests for the new initial scheduling logic with both fresh and populated database scenarios
apps/backend/src/retrieval/retrieval.service.ts Added getLastCreatedTime() method to query the most recent retrieval timestamp
apps/backend/src/metrics/services/metrics-scheduler.service.ts Implemented similar dynamic scheduling pattern with three DB query methods for different timestamp sources; removed SchedulerRegistry dependency
apps/backend/src/metrics/services/metrics-scheduler.service.spec.ts Added tests for metrics scheduling logic with fresh DB and timestamp-based scenarios
apps/backend/src/deal/deal.service.ts Added getLastCreatedTime() method to query the most recent deal timestamp

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread apps/backend/src/retrieval/retrieval.service.ts
Comment thread apps/backend/src/metrics/services/metrics-scheduler.service.ts
Comment thread apps/backend/src/scheduler/scheduler.service.spec.ts Outdated
Comment thread apps/backend/src/scheduler/scheduler.service.ts
Comment thread apps/backend/src/deal/deal.service.ts
@SgtPooki
Copy link
Copy Markdown
Collaborator Author

@beck-8 this doesn't quite solve #119 if you're using the same database for all dealbot instances... and it's still not a robust solution.. but more will come when we get to #91

@beck-8
Copy link
Copy Markdown
Contributor

beck-8 commented Jan 23, 2026

I have created a database for each instance.

try {
const lastRunAt = await getLastRunAt();
if (lastRunAt && lastRunAt.getTime() >= runStartedAt.getTime()) {
nextRunAt = new Date(lastRunAt.getTime() + intervalSeconds * 1000);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this- job frequency becomes unpredictable. Example - If Deal Creation job takes 15 mins to complete and the interval is 30 mins- now it'll be 45 mins interval for next Deal Creation. And, in web we show these infrastructure configuration at the top, which will not make sense anymore.

);
}

private async scheduleInitialRun({
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: looks unnecessary, increasing maintenance burden

@SgtPooki SgtPooki marked this pull request as draft January 23, 2026 18:00
@SgtPooki
Copy link
Copy Markdown
Collaborator Author

converting to draft because we need a better solution..

@BigLep BigLep moved this from 📌 Triage to 🐱 Todo in FOC Jan 26, 2026
@BigLep BigLep moved this from 🐱 Todo to ⌨️ In Progress in FOC Jan 26, 2026
@SgtPooki
Copy link
Copy Markdown
Collaborator Author

i'm going to enable a default-off pg-boss implementation that should solve #119 and help us better manage scheduling between restarts/downtime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🎉 Done

Development

Successfully merging this pull request may close these issues.

Change task scheduling to use startup-relative intervals instead of wall-clock time

6 participants