monksoul / sundial Goto Github PK
View Code? Open in Web Editor NEW.NET 功能齐全的开源分布式作业调度系统,可从最小的应用程序到大型企业系统使用。
Home Page: https://furion.net/docs/job
License: MIT License
.NET 功能齐全的开源分布式作业调度系统,可从最小的应用程序到大型企业系统使用。
Home Page: https://furion.net/docs/job
License: MIT License
若是能有一个支持分布式可视化WebUI任务调度中心的实现就更好了.最好是支持多种存储方式,如:MongoDB,MSSQL,SQLite等.
任务在同时实现 IJob 和 IDisposable 的时候,在 ISchedulerFactory 被释放或相关任务被删除的时候,任务Dispose方法未被调用。
当前只能先手动调用 builder.Services.AddSingleton 将任务注册到 IServiceCollection 中,最后终止程序时才能任务实例被正确释放。
IJob任务实例未被正确释放_2
VS2022 17.7.3
net 6.0 Work Service程序
Sundial v2.25.0
Program.CS 代码
services.AddSchedule(options =>
{
options.AddExecutor<JobExecutor>();
options.AddMonitor<JobMonitor>();
options.AddJob(JobBuilder.Create<MyJob>().SetIncludeAnnotations(true));
});
JobExecutor代码
public class JobExecutor : IJobExecutor
{
private readonly ILogger<JobExecutor> _logger;
public JobExecutor(ILogger<JobExecutor> logger)
{
_logger = logger;
}
public async Task ExecuteAsync(JobExecutingContext context, IJob jobHandler, CancellationToken stoppingToken)
{
// 实现失败重试策略
await Retry.InvokeAsync(async () =>
{
await jobHandler.ExecuteAsync(context, stoppingToken);
}, context.Trigger.NumRetries, context.Trigger.RetryTimeout
// 每次重试输出日志
, retryAction: (total, times) =>
{
_logger.LogWarning($"Retrying {times}/{total} times for 触发器{context.Trigger.TriggerId} 第{context.Trigger.NumberOfRuns}次执行的Job【{context.JobId}】");
});
}
}
JobMonitor代码
public class JobMonitor : IJobMonitor
{
private readonly ILogger<JobMonitor> _logger;
private readonly ISchedulerFactory _schedulerFactory;
public JobMonitor(ILogger<JobMonitor> logger, ISchedulerFactory schedulerFactory)
{
_logger = logger;
_schedulerFactory = schedulerFactory;
}
public async Task OnExecutingAsync(JobExecutingContext context, CancellationToken stoppingToken)
{
_logger.LogInformation($"Job【{context.JobId}】第{context.Trigger.NumberOfRuns}次执行开始");
await Task.CompletedTask;
}
public async Task OnExecutedAsync(JobExecutedContext context, CancellationToken stoppingToken)
{
if (context.Exception != null)
{
_logger.LogError(context.Exception,$"Job【{context.JobId}】for 触发器{context.Trigger.TriggerId} 第{context.Trigger.NumberOfRuns}次执行过程中出错啦,具体Job信息: {context.ConvertToJSON()},具体错误信息:{context.Exception}");
}
//context.Trigger.ElapsedTime不起作用?
_logger.LogInformation($"Job【{context.JobId}】for 触发器{context.Trigger.TriggerId} 第{context.Trigger.NumberOfRuns}次执行结束,执行总耗时为{context.Trigger.ElapsedTime / 1000}秒");
await Task.CompletedTask;
}
}
MyJob代码
[JobDetail("MyJob", Concurrent = false, Description = "这是一段MyJob描述", GroupName = "MyService")]
[PeriodSeconds(3, NumRetries = 3, RetryTimeout = 1000, TriggerId = "MyJobTrigger")]
public class MyJob : IJob
{
private readonly ILogger<MyJob> _logger;
public MyJob(ILogger<MyJob> logger)
{
_logger = logger;
}
public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
{
// 模拟运行第三次出异常
if (context.Trigger.NumberOfRuns == 3)
{
throw new Exception("假装出错");
}
await Task.CompletedTask;
}
}
1. Concurrent = false 串行模式下JobMonitor的OnExecutedAsync中的获取当前context.Trigger.ElapsedTime时间异常
2. Concurrent = true 并行模式下JobMonitor的OnExecutedAsync中在1的基础上还出现线程安全问题导致输出信息异常
错误1:
Concurrent = false 串行模式下JobMonitor的OnExecutedAsync中的获取当前context.Trigger.ElapsedTime时间异常
错误2:
Concurrent = true下除上述的context.Trigger.ElapsedTime时间异常外,日志信息输出线程安全异常
以上还只是一个Job的情况,如果是多个Job共用该全局的JobMonitor情况类似,该怎么通用的保证每个Job的执行前后输出信息且保证线程安全和每个job的执行时间统计上线程安全?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.