Appearance
Providers
GrydJobs is designed to be provider-agnostic. The IJobScheduler abstraction allows swapping the underlying job processing engine without changing application code.
Hangfire (Default)
Hangfire is the default and recommended provider for GrydJobs.
Storage
GrydJobs uses PostgreSQL for Hangfire storage via Hangfire.PostgreSql:
csharp
builder.Services.AddGrydJobs(options =>
{
options.ConnectionString = "Host=localhost;Database=gryd_jobs;...";
});Hangfire will create its own schema (hangfire) in the specified database.
When no host tenant context is registered, AddGrydJobsInfrastructure supplies a scoped Core TenantContext and its ITenantContextWriter / ITenantContext aliases so JobsDbContext and its save interceptor can resolve in a standalone worker. This infrastructure fallback does not configure authentication, request tenant routing, middleware, or tenant query-filter policy; web hosts must continue to configure those through their auth and multi-tenancy registrations.
Serialization
GrydJobs configures Hangfire with:
UseSimpleAssemblyNameTypeSerializer— shorter type namesUseRecommendedSerializerSettings— optimized JSON serialization
Job Activation
GrydJobs provides a custom HangfireJobActivator that:
- Creates a DI scope per job execution
- Captures Hangfire execution metadata and restores the trusted tenant context
- Resolves job instances from the service provider after tenant context is established
Job Execution
Four adapter types handle the bridge between Hangfire and GrydJobs:
| Adapter | Purpose |
|---|---|
JobExecutionAdapter<TJob, TArgs> | Strongly-typed fire-and-forget/delayed jobs |
RecurringJobExecutionAdapter<TJob> | Strongly-typed recurring jobs |
DynamicJobExecutionAdapter | Runtime type-resolved jobs (from API) |
DynamicRecurringJobExecutionAdapter | Runtime type-resolved recurring jobs |
Each adapter delegates to the shared execution service, which creates the execution context, applies the configured cooperative deadline, runs the job, and records lifecycle state. Recurring jobs keep their registered recurring key in JobExecutionContext.JobId; ExecutionId identifies the individual Hangfire execution.
Dynamic one-off jobs preserve exceptions thrown by IJob<TArgs> methods, including exceptions raised before the method returns its task and exceptions raised while that task runs. Error filters and failure results receive the original exception instance. Execution history and dead-letter records retain its original message and user-method stack; an exception that is itself a TargetInvocationException remains unchanged.
Quartz.NET (Future)
A Quartz.NET provider is planned for scenarios requiring:
- Clustered scheduling across multiple nodes
- Calendar-based exclusions
- Job data map persistence
The IJobScheduler abstraction ensures zero code changes when switching providers.
Custom Provider
Application handlers that submit or register jobs require the durable envelope capability. A custom provider must implement IJobSchedulerWithEnvelope as well as IJobScheduler, persist the immutable JobSchedulingEnvelope with each provider invocation, and provide execution metadata through IJobExecutionMetadataAccessor. Providers without this capability fail closed when an application handler tries to submit a tracked job.
Public management commands also need safe persistence capabilities. Custom stores used for job cancellation must implement IJobManagementStore; recurring schedule update, pause, and resume use IJobScheduleManagementStore for authorized reads and narrow field patches. Safe coordination also requires IJobScheduleManagementLock, and deletion requires IJobScheduleManagementDeleteStore. The built-in Hangfire provider supplies IRecurringJobManagementScheduler to preserve the current invocation while pausing, resuming, or updating a managed registration. These optional interfaces keep the legacy IJobStore and IJobScheduleStore contracts source compatible. A custom store or provider that lacks the capability needed by a management command fails before reporting success. Implement tenant-write authorization before provider effects and field-level schedule patches.
Initial recurring creation also requires the optional IJobScheduleInitialRegistrationStore and IRecurringInitialRegistrationScheduler capabilities. The store must validate tenant and group write authority before provider effects, durably save the complete original envelope as Pending, and confirm only that state field. The provider must inspect one key without broad scans, reuse an exact compatible invocation unchanged, create only when the key is absent, and reject every conflicting registration. Custom implementations without both capabilities fail before a schedule is stored. Legacy rows have no captured creation envelope and are never upgraded by guessing missing user, correlation, or policy metadata.
Cancellation is conditional on the provider state observed by the scheduler. Providers must return false when that state changed or is no longer cancellable, and must let cancellation and provider errors reach the caller. Provider and database changes are not one distributed transaction, and a cancellation cannot reverse effects already completed by job code. Recurring pause changes the provider's effective cron to Cron.Never() while retaining the existing Hangfire invocation and saved desired cron. Previously enqueued work and running work continue; a manual trigger is an explicit override. Provider and database writes are not atomic. Retry the same operation after a synchronization error; unattended recovery after a process interruption is not guaranteed.
To implement a custom provider:
- Implement
IJobScheduler - Implement
IJobSchedulerWithEnvelopeand persist envelopes before publishing runnable work - Implement
IJobMonitorandIJobExecutionMetadataAccessor - Register in DI:
csharp
services.AddScoped<IJobScheduler, MyCustomJobScheduler>();
services.AddScoped<IJobSchedulerWithEnvelope>(sp =>
(IJobSchedulerWithEnvelope)sp.GetRequiredService<IJobScheduler>());
services.AddSingleton<IJobMonitor, MyCustomJobMonitor>();
services.AddScoped<IJobExecutionMetadataAccessor, MyExecutionMetadataAccessor>();Keep old serialized adapter signatures available when upgrading a provider with persisted jobs. Legacy invocations without an envelope may lack tenant, user, correlation, and retry metadata. GrydJobs recovers trusted values only from the stored definition or recurring schedule. If required tenant metadata is unavailable, execution fails before user work begins; resubmit through the GrydJobs scheduler. A job explicitly configured in GlobalJobs may run without tenant scope.