108 lines
3.1 KiB
C#
108 lines
3.1 KiB
C#
|
|
using Quartz;
|
||
|
|
using Quartz.Impl.Matchers;
|
||
|
|
|
||
|
|
namespace Application;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 任务状态
|
||
|
|
/// </summary>
|
||
|
|
public record JobStatus
|
||
|
|
{
|
||
|
|
public string Name { get; init; } = string.Empty;
|
||
|
|
public string JobDescription { get; init; } = string.Empty;
|
||
|
|
public string TriggerDescription { get; init; } = string.Empty;
|
||
|
|
public string Status { get; init; } = string.Empty;
|
||
|
|
public string NextRunTime { get; init; } = string.Empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 任务应用服务接口
|
||
|
|
/// </summary>
|
||
|
|
public interface IJobApplication
|
||
|
|
{
|
||
|
|
Task<List<JobStatus>> GetJobsAsync();
|
||
|
|
Task<bool> ExecuteAsync(string jobName);
|
||
|
|
Task<bool> PauseAsync(string jobName);
|
||
|
|
Task<bool> ResumeAsync(string jobName);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 任务应用服务实现
|
||
|
|
/// </summary>
|
||
|
|
public class JobApplication(
|
||
|
|
ISchedulerFactory schedulerFactory,
|
||
|
|
ILogger<JobApplication> logger
|
||
|
|
) : IJobApplication
|
||
|
|
{
|
||
|
|
public async Task<List<JobStatus>> GetJobsAsync()
|
||
|
|
{
|
||
|
|
var scheduler = await schedulerFactory.GetScheduler();
|
||
|
|
var jobKeys = await scheduler.GetJobKeys(GroupMatcher<JobKey>.AnyGroup());
|
||
|
|
var jobStatuses = new List<JobStatus>();
|
||
|
|
|
||
|
|
foreach (var jobKey in jobKeys)
|
||
|
|
{
|
||
|
|
var jobDetail = await scheduler.GetJobDetail(jobKey);
|
||
|
|
var triggers = await scheduler.GetTriggersOfJob(jobKey);
|
||
|
|
var trigger = triggers.FirstOrDefault();
|
||
|
|
|
||
|
|
var status = "Unknown";
|
||
|
|
DateTime? nextFireTime = null;
|
||
|
|
|
||
|
|
if (trigger != null)
|
||
|
|
{
|
||
|
|
var triggerState = await scheduler.GetTriggerState(trigger.Key);
|
||
|
|
status = triggerState.ToString();
|
||
|
|
nextFireTime = trigger.GetNextFireTimeUtc()?.ToLocalTime().DateTime;
|
||
|
|
}
|
||
|
|
|
||
|
|
jobStatuses.Add(new JobStatus
|
||
|
|
{
|
||
|
|
Name = jobKey.Name,
|
||
|
|
JobDescription = jobDetail?.Description ?? jobKey.Name,
|
||
|
|
TriggerDescription = trigger?.Description ?? string.Empty,
|
||
|
|
Status = status,
|
||
|
|
NextRunTime = nextFireTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "无"
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
return jobStatuses;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> ExecuteAsync(string jobName)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(jobName))
|
||
|
|
{
|
||
|
|
throw new ValidationException("任务名称不能为空");
|
||
|
|
}
|
||
|
|
|
||
|
|
var scheduler = await schedulerFactory.GetScheduler();
|
||
|
|
await scheduler.TriggerJob(new JobKey(jobName));
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> PauseAsync(string jobName)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(jobName))
|
||
|
|
{
|
||
|
|
throw new ValidationException("任务名称不能为空");
|
||
|
|
}
|
||
|
|
|
||
|
|
var scheduler = await schedulerFactory.GetScheduler();
|
||
|
|
await scheduler.PauseJob(new JobKey(jobName));
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> ResumeAsync(string jobName)
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(jobName))
|
||
|
|
{
|
||
|
|
throw new ValidationException("任务名称不能为空");
|
||
|
|
}
|
||
|
|
|
||
|
|
var scheduler = await schedulerFactory.GetScheduler();
|
||
|
|
await scheduler.ResumeJob(new JobKey(jobName));
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|