linkdotnet / linkdotnet/BlogExamples
Cron - Not running multiple instances of a job
Nessuno ha ancora preso questa issue.
- Lingua principale
- C#
- Stelle
- 124
- Fork
- 24
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
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);
}
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia individuando la gestione del tick dello scheduler, il punto di ingresso ICronJob e le chiamate a job.Run(stoppingToken). Traccia il modo in cui i job pianificati vengono selezionati e monitorati. Il lavoro è completato quando un job pianificato non viene avviato nuovamente mentre la sua attività precedente è ancora in esecuzione, mentre le esecuzioni successive possono iniziare dopo il completamento.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- csharp
- Ambito
- backend
- Tipo di issue
- Funzionalità
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100