linkdotnet / linkdotnet/BlogExamples
Cron - Not running multiple instances of a job
还没有人认领这个 Issue。
- 主要语言
- C#
- 星标
- 124
- 派生
- 24
- PR 合并指标
- 30 天内没有已合并 PR
描述
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);
}
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先定位 scheduler tick 的处理逻辑、ICronJob 入口点以及对 job.Run(stoppingToken) 的调用。跟踪计划任务的选择和跟踪方式。完成标准是:计划任务在其上一次 task 仍在运行时不会再次启动,但在完成后可以启动后续运行。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- csharp
- 领域
- backend
- Issue 类型
- 功能
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100