NCronJob-Dev/NCronJob

Jobs added during runtime do not run

Opened this issue · 1 comments

Bug description

The Job will be triggered at the expected time if it is added to the runtime registry at the start of the application.
When the job is added later to the registry using the same service, it will appear in the registry (breakpoint view) as it should, but will not be triggered at the expected time.

The instant job registration and execution works fine.

The jobs below are added and deleted from the registries in the same manner as in the documentation and other issues. (referencing issue and docs)

Context

I am making a task scheduler, and am using NCronJob as an alternative to Quartz. During runtime, I want to have the user define a job that is triggered on a defined, repeating schedule.
The jobs that are allready defined in the database are added to the registry using a hosted service during runtime, after startup.
These jobs are can be removed and new jobs can be added through the runtime registry.
We did not make an interface for an update at this time

Task

In the example provided below, one task can have N triggers, where every trigger will equate one job registered during runtime.
When unregistering a task, all related jobs will be unregistered.

Custom Interface for job registries

All the additions and deletions of jobsfrom the registry are done through the same service, which shows the same behaviour both as a transient and a singleton: service example

classDiagram
    class IJobRegistrationService {
        <<interface>>
        +RegisterOneTimeJob(AutomatedTask task) Task
        +UpsertRecurringJobRegistration(AutomatedTask task) Task
        +TryUnregisteringRecurringJob(Guid taskId) Task
    }

    class JobRegistrationService {
        -IRuntimeJobRegistry _jobRegistry
        -IInstantJobRegistry _instantJobRegistry
        -ILogger<JobRegistrationService> _logger
        -Func<Guid, Guid, string> JobName
        -Func<Guid> ManualJobTriggerId
        +RegisterOneTimeJob(AutomatedTask task) Task
        +UpsertRecurringJobRegistration(AutomatedTask task) Task
        +TryUnregisteringRecurringJob(Guid taskId) Task
        -RegisterJobWithSingleTrigger(Guid taskId, PeriodicallyTrigger trigger) void
        -DeleteAllJobsForTask(Guid taskId) void
        -IsJobRelatedToAutomatedTask(RecurringJobSchedule job, Guid taskId) bool
    }

    class IRuntimeJobRegistry {
        <<interface>>
    }

    class IInstantJobRegistry {
        <<interface>>
    }

    class ILogger {
        <<interface>>
    }

    JobRegistrationService ..|> IJobRegistrationService
    JobRegistrationService --> IRuntimeJobRegistry
    JobRegistrationService --> IInstantJobRegistry
    JobRegistrationService --> ILogger
Loading

Expected behavior

The jobs will run at the expected time regardless of how they were registered (given they are well formed and saved in the registry)

Version information

  • NCronJobVersion: 3.1.3.
  • .NET runtime version: 8.0
  • Framework: Blazor

Hey @IvaTutis,

thanks for raising the issue. Do you have a reproducible example by any chance?.

I tried a very minimal setup:

using NCronJob;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddNCronJob();

var app = builder.Build();

app.MapGet("/", (IRuntimeJobRegistry registry) =>
{
    registry.AddJob(j => j.AddJob(() => { Console.WriteLine("HEY"); }, "* * * * * *"));
});

app.Run();

If you hit the "/" endpoint with a browser you will see "HEY" every second on the console. So jobs are executed.