question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jobs fail and stop executing after undetermined amount of time

See original GitHub issue

Version: 3.0.0

Expected behavior

Quartz runs the configured jobs on the configured cron schedules forever

Actual behavior

After an undetermined amount of time, between several days and several weeks, a load of jobs crash all at the same time with this error message being logged:

2018-02-07T04:15:00.0294285+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-23': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (8373252b)
2018-02-07T04:15:00.0450537+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-25': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (99aac8e1)
2018-02-07T04:15:00.0450537+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-27': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (4f42eca1)
2018-02-07T04:15:00.0450537+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-29': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (052177ee)
2018-02-07T04:15:00.0450537+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-30': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (169580e4)
2018-02-07T04:15:00.0450537+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-31': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (c34383b0)
2018-02-07T04:15:00.0450537+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-3': Problem instantiating type 'SystemChecker.Model.Jobs.WebRequestChecker' (4e03618f)
2018-02-07T04:15:00.0450537+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-33': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (15ff2bec)
2018-02-07T04:15:00.0450537+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-35': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (9b00c9b7)
2018-02-07T04:15:00.0450537+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-39': Problem instantiating type 'SystemChecker.Model.Jobs.WebRequestChecker' (9193c6e7)
2018-02-07T04:15:00.0606788+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-4': Problem instantiating type 'SystemChecker.Model.Jobs.WebRequestChecker' (cdd6f8ae)
2018-02-07T04:15:00.0606788+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-7': Problem instantiating type 'SystemChecker.Model.Jobs.WebRequestChecker' (ae06acc7)
2018-02-07T04:15:00.0606788+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-9': Problem instantiating type 'SystemChecker.Model.Jobs.WebRequestChecker' (d8270fbd)
2018-02-07T04:15:00.0606788+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-10': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (55cc06cb)
2018-02-07T04:20:00.0002806+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-24': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (1ed26de5)
2018-02-07T04:20:00.0002806+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-26': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (970db8c2)
2018-02-07T04:20:00.0002806+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-28': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (60a62a66)
2018-02-07T04:20:00.0002806+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-32': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (151f3418)
2018-02-07T04:20:00.0002806+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-36': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (2703dffc)
2018-02-07T04:20:00.0002806+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-40': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (ce2cc71e)
2018-02-07T05:00:00.0094520+00:00  [ERR] Scheduler error: An error occurred instantiating job to be executed. job= 'DEFAULT.check-6': Problem instantiating type 'SystemChecker.Model.Jobs.DatabaseChecker' (833baa0e)

After this point, the jobs do not run at all on their schedules anymore, until the schedules are reconfigured at which point it continues to work again.

Steps to reproduce

Not quite reproducible code but shows how I am creating the jobs


public async Task UpdateSchedule(Check check)
{
    await RemoveSchedule(check);
    if (!check.Active) { return; }
    var type = GetJobForCheck(check);
    IDictionary<string, object> data = new Dictionary<string, object>
    {
        ["CheckID"] = check.ID,
        ["Logger"] = _checkLogger
    };
    var job = JobBuilder.Create(type)
        .WithIdentity(GetJobKeyForCheck(check))
        .SetJobData(new JobDataMap(data))
        .Build();

    await _scheduler.AddJob(job, false, true);

    foreach (var schedule in check.Schedules.Where(x => x.Active))
    {
        if (CronExpression.IsValidExpression(schedule.Expression))
        {
            var trigger = TriggerBuilder.Create()
                .WithIdentity(GetTriggerKeyForSchedule(schedule))
                .WithCronSchedule(schedule.Expression)
                .ForJob(job)
                .Build();

            await _scheduler.ScheduleJob(trigger);
        }
        else
        {
            _logger.LogError($"Failed to create schedule for ID {schedule.ID} on check {check.Name} ({check.ID}): {schedule.Expression}");
        }
    }
}

I’m also using a custom job factory for dependency injection:

public class JobFactory : IJobFactory
{
    private readonly IServiceProvider _container;
    private Dictionary<IJob, IServiceScope> _scopes = new Dictionary<IJob, IServiceScope>();
    public JobFactory(IServiceProvider container)
    {
        _container = container;
    }
    public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
    {
        var scope = _container.CreateScope();
        var job = scope.ServiceProvider.GetRequiredService(bundle.JobDetail.JobType) as IJob;
        _scopes[job] = scope;
        return job;
    }

    public void ReturnJob(IJob job)
    {
        (job as IDisposable)?.Dispose();
        _scopes[job]?.Dispose();
        _scopes.Remove(job);
    }
}

Unfortunately the logs do not contain any stack traces so I’m not sure if the error originates from the JobFactory. I’m going to add some catching/logging there to see if it is the case but in any case the logged errors should be more informative (as per #561)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
njsentcommented, Dec 10, 2018

Thank you @MattJeanes for sharing that. Very helpful!

1reaction
MattJeanescommented, Dec 10, 2018

Hey @njsent - I injected the IServiceProvider into the Job Factory and essentially kept a concurrent dictionary containing the job as the key and a new service scope (serviceProvider.CreateScope()) and disposed it in the job factory return job method.

You can see my implementation here: https://github.com/MattJeanes/SystemChecker/blob/master/SystemChecker.Model/Helpers/JobFactory.cs

I’ve been running that in production for the last 10 months or so and no problems at all!

Hope this helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jobs get stuck in running state even after completed ...
The job is kept in running state even after all nodes have been executed. The job runs completely to the end, it shows...
Read more >
What happens when a procedure executed by a JOB is not ...
So it means: If a jobs last longer than 30 seconds then the new job will start immediately after the previous job has...
Read more >
Choose when to run jobs | GitLab
Use needs to configure a job to run as soon as the earlier jobs it depends on finish running. Specify when jobs run...
Read more >
Unknown job error on running service
I am getting an 'start: Unknown job: elasticsearch' error when I try to do service elasticsearch start. When I run service --status-all.
Read more >
Job fails with timeout errors - Universal Discovery and CMDB
Solution. The default connection timeout value of the Universal Discovery protocol is 20 seconds. If the command execution in your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found