GhaylStackDocumentationv1.0.2
العربيةWebsite
Shared services

Shared services

Background jobs and schedules

Enqueue work, preserve tenant context and inspect the Hangfire execution path.

Choose background work intentionally

Use a background job for work that can complete after an HTTP response, such as tenant provisioning. Keep an operation synchronous when the caller must receive its actual result before continuing. Enqueuing successfully means the scheduler accepted the job; it does not mean the business work has finished.

GhaylStack registers Hangfire through the Jobs building block. IJobService exposes enqueue, schedule, delete and requeue operations. Recurring work uses Hangfire's IRecurringJobManager; the abstraction does not pretend these are the same operation.

Follow an existing enqueue path

TenantProvisioningService enqueues TenantProvisioningJob.RunAsync with the tenant identifier and a correlation identifier. The returned job identifier can be correlated with the execution record. The following expression is the existing call pattern inside that service:

csharp
_jobService.Enqueue<TenantProvisioningJob>(
    job => job.RunAsync(tenant.Id, correlationId, CancellationToken.None));

For your own job, register its class with DI, give it serializable identifiers and resolve current data when it runs. Avoid serializing a tracked EF entity or a request service into a queued expression. A job may execute after the request scope is gone or after a deployment changes the data.

Preserve tenant and user context

FshJobFilter captures tenant and user parameters when a job is created from an HTTP request. FshJobActivator restores them in the job's DI scope before resolving the job class. This supports tenant-dependent contexts and current-user services.

Jobs created without an HTTP context do not receive those parameters automatically. A recurring scanner must explicitly choose its tenant scope or enumerate tenants using the existing module pattern. Do not infer the tenant from a cached global variable. When testing, ensure a job for tenant A cannot change tenant B's rows.

Know the configured worker behavior

The current registration uses five workers, the default and email queues, a 30-second heartbeat and a 30-second schedule polling interval. These values are set in the Jobs registration code; they are not documented here as environment options.

Job storage follows DatabaseOptions. HangfireOptions supplies the dashboard route, username and password. The default route is /jobs. Startup validation requires a username of at least three characters and a password of at least twelve. Put actual credentials in the deployment's secret mechanism and keep the dashboard behind the intended access boundary.

Diagnose and validate

Inspect whether the job is queued, processing, succeeded or failed. If it remains queued, check that a worker listens to its queue and can connect to the configured storage. If it fails after a deployment, inspect its serialized method signature and dependencies before repeatedly retrying it.

Design externally visible effects to tolerate retries: a second execution must not charge twice or create duplicate approvals. Record a stable business identifier and check existing state before applying an effect. Use eventing when a durable integration event and transaction are the appropriate mechanism.

Validate successful execution, a controlled failure and retry, tenant scope, and execution after the initiating request ends. Check the resulting data or message, not only the green job status.

Implementation references

View MarkdownBack to top

Search documentation

Use ↑ ↓ to move, Enter to open and Esc to close.