Skip to content

refactor: remove metrics job scheduler#436

Merged
silent-cipher merged 11 commits intomainfrom
refactor/cleanup-metrics-jobs
Apr 21, 2026
Merged

refactor: remove metrics job scheduler#436
silent-cipher merged 11 commits intomainfrom
refactor/cleanup-metrics-jobs

Conversation

@silent-cipher
Copy link
Copy Markdown
Collaborator

@silent-cipher silent-cipher commented Apr 7, 2026

Summary

This PR removes the metrics job scheduling system from the backend, eliminating the periodic metrics aggregation and cleanup jobs that were previously managed by pg-boss. The metrics calculation functionality remains available but is no longer automatically scheduled.

Breaking Changes

  • Environment Variable: METRICS_PER_HOUR is no longer recognized (can be safely removed from .env files)
  • Job Types: Existing metrics and metrics_cleanup jobs in pg-boss will no longer be processed

Implements steps 4, 5 and 10 from #275 (comment)

Copilot AI review requested due to automatic review settings April 7, 2026 17:55
@FilOzzy FilOzzy added this to FOC Apr 7, 2026
@github-project-automation github-project-automation Bot moved this to 📌 Triage in FOC Apr 7, 2026
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 removes the pg-boss–scheduled metrics aggregation/cleanup jobs from the backend, leaving metrics computation available but no longer automatically executed on a schedule. It also removes the METRICS_PER_HOUR configuration surface from docs/config.

Changes:

  • Removed metrics-related pg-boss queues, workers, schedules, and configuration (METRICS_PER_HOUR / metricsPerHour).
  • Updated job type definitions and job-state reporting/mapping to exclude metrics jobs.
  • Updated backend documentation to reflect the removal of metrics scheduling.

Reviewed changes

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

Show a summary per file
File Description
docs/environment-variables.md Removes METRICS_PER_HOUR from the env var reference.
apps/backend/src/metrics/services/metrics-scheduler.service.ts Removes the metrics cleanup routine previously used by scheduled jobs.
apps/backend/src/jobs/repositories/job-schedule.repository.ts Removes metrics queue-name mappings from pg-boss job state queries.
apps/backend/src/jobs/jobs.service.ts Removes metrics queues/workers/schedules and related interval configuration.
apps/backend/src/jobs/jobs.service.spec.ts Updates unit tests to reflect removal of metrics job scheduling/queues.
apps/backend/src/jobs/jobs.module.ts Removes metrics module selection/import from JobsModule wiring.
apps/backend/src/jobs/job-queues.ts Removes metrics queue constants.
apps/backend/src/database/entities/job-schedule-state.entity.ts Removes metrics/metrics_cleanup from the JobType union.
apps/backend/src/config/app.config.ts Removes METRICS_PER_HOUR validation + config loading and metricsPerHour from IJobsConfig.
apps/backend/README.md Removes METRICS_PER_HOUR from the scheduling configuration table.

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

Comment thread apps/backend/src/jobs/jobs.service.ts
Comment thread apps/backend/src/jobs/jobs.service.ts
Comment thread apps/backend/src/database/entities/job-schedule-state.entity.ts Outdated
Comment thread apps/backend/src/jobs/jobs.service.spec.ts
Comment thread apps/backend/README.md
@silent-cipher silent-cipher requested a review from SgtPooki April 8, 2026 18:07
@BigLep BigLep moved this from 📌 Triage to 🔎 Awaiting review in FOC Apr 12, 2026
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

Copilot reviewed 16 out of 16 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

apps/backend/src/jobs/jobs.service.ts:1097

  • updateQueueMetrics() now initializes gauges only for a subset of JobTypes, but later sets jobsPausedGauge for all rows returned by countPausedSchedules() (no filtering). If legacy paused schedules exist (e.g. metrics), this will re-introduce those label series; and if they are later removed, the gauge series can retain a stale value because it is no longer reset each tick. Filter paused-schedule rows to the same jobTypes list (or explicitly reset/remove legacy labels) to keep exported metrics consistent.
  private async updateQueueMetrics(): Promise<void> {
    const jobTypes: JobType[] = ["deal", "retrieval", "data_set_creation", "data_retention_poll", "providers_refresh"];
    for (const jobType of jobTypes) {
      this.jobsQueuedGauge.set({ job_type: jobType }, 0);
      this.jobsRetryScheduledGauge.set({ job_type: jobType }, 0);
      this.jobsInFlightGauge.set({ job_type: jobType }, 0);
      this.jobsPausedGauge.set({ job_type: jobType }, 0);
      this.oldestQueuedAgeGauge.set({ job_type: jobType }, 0);
      this.oldestInFlightAgeGauge.set({ job_type: jobType }, 0);
    }

    const rows = await this.jobScheduleRepository.countBossJobStates(["created", "retry", "active"]);
    if (rows.length > 0) {
      for (const row of rows) {
        const jobType = row.job_type as JobType;
        if (!jobTypes.includes(jobType)) continue;
        const state = String(row.state).toLowerCase();
        if (state === "active") {
          this.jobsInFlightGauge.set({ job_type: jobType }, row.count);
        } else if (state === "retry") {
          this.jobsRetryScheduledGauge.set({ job_type: jobType }, row.count);
        } else {
          this.jobsQueuedGauge.set({ job_type: jobType }, row.count);
        }
      }
    } else {
      this.logger.error({
        event: "pgboss_job_state_query_empty",
        message:
          "pgboss.job returned zero rows for states created/retry/active; metrics will remain at 0. Verify the backend is connected to the expected database and schema.",
      });
    }

    const pausedSchedules = await this.jobScheduleRepository.countPausedSchedules();
    for (const row of pausedSchedules) {
      this.jobsPausedGauge.set({ job_type: row.job_type }, row.count);
    }

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

Comment thread apps/backend/src/jobs/jobs.service.ts Outdated
silent-cipher and others added 6 commits April 14, 2026 12:14
* refactor: remove legacy metrics API endpoints and module wiring

* refactor: add migration to drop legacy metrics schema

Drop metrics_daily table, sp_performance materialized views,
their refresh functions, and related enums. Remove corresponding
entity files and database module registrations.

* test: add e2e test for DropMetricsSchema migration
Copy link
Copy Markdown
Collaborator

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

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

lgtm, a few comments

Comment thread apps/backend/src/database/entities/job-schedule-state.entity.ts Outdated
Comment thread apps/backend/src/jobs/repositories/job-schedule.repository.ts
Comment thread apps/backend/src/jobs/repositories/job-schedule.repository.ts
Comment thread apps/backend/src/jobs/jobs.service.ts Outdated
@silent-cipher silent-cipher merged commit 430fb09 into main Apr 21, 2026
7 checks passed
@silent-cipher silent-cipher deleted the refactor/cleanup-metrics-jobs branch April 21, 2026 07:27
@github-project-automation github-project-automation Bot moved this from 🔎 Awaiting review to 🎉 Done in FOC Apr 21, 2026
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.

6 participants