Skip to content

Replace Hangfire with an in-process cron scheduler and Humans-native jobs UI #2807

Backlog

Description

@peterdrier

Section: TBD — either a new lightweight Humans.Jobs section (/Jobs/Admin) or Shell scheduler + /Debug/Jobs.

Why

Humans is a single-server app with 34 recurring cron jobs (across 23 sections via ISectionJobs) and 6 non-recurring Enqueue/Schedule call sites. Almost every job already owns its own state and retry/backoff logic in its section's tables (Email, Expenses, Google), and standing policy D13 forbids relying on Hangfire storage continuity across restarts.

Keeping Hangfire costs far more than it gives:

  • Hangfire.Core referenced in 19 section .csproj files almost purely for [DisableConcurrentExecution] (including an unused ref in Humans.Gate.csproj).
  • Unowned hangfire.* Postgres tables + a second connection pool (docker-entrypoint.sh), outside section DB ownership.
  • Stale distributed-lock workarounds on a single-server app (/Debug/Maintenance/ClearHangfireLocks, raw DELETE FROM hangfire.lock, 30-min DistributedLockTimeout in Program.cs), while ExpenseReportBackgroundProcessor still carries its own SemaphoreSlim.
  • Expression-tree reflection + stale-schedule sweeper in RecurringJobExtensions.cs, JobStorage.Current static leakage in tests, 5 NuGet packages (LGPL-3.0 + lockstep Dependabot pinning), and an off-brand /hangfire UI.

Plan

1. Remove the one durable Hangfire queue dependency (Humans.Tickets + Humans.Google)

  • TicketDeliveryService.SendJoinInvitationAsync (currently jobs.Schedule(..., TimeSpan.FromDays(2)), the only multi-day persisted Hangfire timer in the app): replace with a DB-driven recurring sweep over valid non-child tickets where IssuedAt <= now - 2d and no invitation has been sent yet.
  • TicketDeliveryHandOff: TicketDeliveryService.SendAsync makes no external network calls (it only renders and writes to email_outbox). Dispatch through the in-process work queue and have TicketCheckoutReconcileJob (already every 5 min) backstop any paid order missing a TicketsEmailed timeline event.
  • GoogleGroupSyncService 15-min scoped retry: switch to an in-process delayed trigger (hourly teams-system-sync / daily google-resource-reconciliation already heal restarts) or route through google_sync_outbox.

2. In-process scheduler (BackgroundService + Cronos) + fire-and-forget queue

  • Consume existing ISectionJobs / RecurringJobDescriptor(Id, JobType, Cron) directly — optionally adding a lock-group key for shared exclusion (holded-api, tickets-vendor-sync).
  • Arm the scheduler in IHostedService.StartAsync (after the migration barrier).
  • Run each job in a fresh DI scope with the host's stoppingToken, guarded by a per-job/lock-group SemaphoreSlim(1, 1) (skip if already running — zero stale DB locks).
  • Centralize IHumansMetrics.RecordJobRun(...) and keep a bounded in-memory run history (last run, duration, status, recent errors — same pattern as InMemoryLogSink / ClientStatsTracker).
  • Expose TryTrigger(string jobId) for immediate nudges (ProcessEmailOutboxJob, HoldedMirrorCatchUp, TicketSyncJob, and the admin UI).
  • Add a small Channel-backed IBackgroundWorkQueue for parameterized off-request runs (DemoWorldJob, GoogleGroupSync, GoogleDriveSync).

3. Humans-native Jobs UI

  • Render a page (Admin + read-only Board, matching /hangfire's current access) listing every registered job, cron expression, next run, last run/duration/status, recent errors, and a "Run now" POST button.
  • Replace the Hangfire nav link and AspNetCore.HealthChecks.Hangfire check.

4. Remove Hangfire

  • Remove Hangfire.Core and [DisableConcurrentExecution] from all 19 section projects.
  • Remove Hangfire.Core, Hangfire.AspNetCore, Hangfire.PostgreSql, Hangfire.InMemory, and AspNetCore.HealthChecks.Hangfire from Directory.Packages.props, .github/dependabot.yml, Program.cs, RecurringJobExtensions.cs, HangfireAuthorizationFilter.cs, ClearHangfireLocks (/Debug/Maintenance), and test stubs.
  • Update Views/About/Index.cshtml (remove LGPL-3.0 Hangfire entries, add MIT Cronos), docs/features/global/background-jobs.md, docker-entrypoint.sh, memory/architecture/cancellation-token-propagation.md, and the public-surface analyzer wording for Jobs/ (HUM0034).
  • Follow-up (requires explicit approval per destructive-drop policy): drop the orphaned hangfire schema in Postgres once the new scheduler is live in production.

Open decisions for implementation

  • Home for the scheduler & UI: new Humans.Jobs section (/Jobs/Admin) vs. Shell + Humans.Debug (/Debug/Jobs).
  • Run history: purely in-memory (matching /Debug/Logs and /Debug/HttpErrors) vs. persisted last-run rows.

Activity

  1. added
    refactoring
    on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactoring

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions