Cron - Not running multiple instances of a job
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by locating the scheduler tick handling, the ICronJob entry point, and the calls to job.Run(stoppingToken). Trace how scheduled jobs are selected and tracked. Done means a scheduled job is not started again while its previous task is still running, while later runs can start after completion.
Written by the indexing model from the issue text.
Description
This is not an elaborate solution, but it works (in theory). You can't choose to have multiple instances running or not. I just assumed not to have multiple instances running regardless. I assumed this is not required unless you are running a job every minute or two.
I added a field to the scheduler that tracks the Task returned by job.Run(stoppingToken);. On ever tick check the list for completed tasks and clear them. Prior to running a job skip if it is on the list of running jobs.
I'm sure this might need some tweaking this was my first attempt at it.
private Dictionary<string, Task> _runningJobs;
.
.
.
// Clear completed jobs
foreach (var key in _runningJobs.Keys)
{
if (_runningJobs[key].IsCompleted)
{
_runningJobs[key].Dispose();
_runningJobs.Remove(key);
}
}
// Run jobs that are in the map
RunActiveJobs(runMap, now, stoppingToken);
.
.
.
foreach (var run in currentRuns)
{
// We are sure (thanks to our extension method)
// that the service is of type ICronJob
var job = (ICronJob)_serviceProvider.GetRequiredService(run);
string jobName = job.GetType().Name;
// Continue to next job if job is already running
if (_runningJobs.ContainsKey(jobName))
{
continue;
}
// We don't want to await jobs explicitly because that
// could interfere with other job runs
var task = job.Run(stoppingToken);
// Add task to running jobs
_runningJobs.Add(jobName, task);
}
- Dominant language
- C#
- Stars
- 124
- Forks
- 24
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from linkdotnet/BlogExamples
-
Difficulty 3/5 1-2 days Newbie friendliness 30/100
linkdotnet/BlogExamples#1 · 3 comments ·
All issues in linkdotnet/BlogExamples
Similar issues
-
:watch: Not Triaged dotnet-fsharp/svc
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
Client customer-reported needs-team-attention question Service Attention WebPubSub
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Azure/azure-sdk-for-net#63292 · 3 comments · 1 reaction ·
-
Issue-Enhancement Needs-Triage
Difficulty 1/5 Under an hour Newbie friendliness 86/100
PowerShell/PowerShell#28061 · 2 reactions ·
-
dependencies needs-team-triage server-Azure.Mcp
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
aspnet-core/svc aspnetcore-signalr/subsvc doc-enhancement Pri2 SignalR
Difficulty 1/5 Under an hour Newbie friendliness 88/100
dotnet/AspNetCore.Docs#37729 ·