Quartz.NET
- Quartz.Net 定制UI維護了常用作業添加、删除、修改、停止、啟動功能,直接使用cron表達式設定作業執行間隔,有完整的日志記錄。
- Quartz.NET是一個功能齊全的開源作業排程系統,可用于從最小的應用程式到大型企業系統。
- Quartz.NET是一個用C#編寫的純.NET庫,是對JAVA開源排程架構Quartz的移植。目前支援.Net Core
- Quartz.NET官方位址:https://www.quartz-scheduler.net/
1.Quartz.NetUI 開發環境
.net core2.1及以上版本、vs2017、Quartz.NET 3.0.7 、 vue 2.0 、IView
2.開箱即用、不依賴資料庫
- 直接運作Quartz.NetUI\Quartz.NET.Web目錄下run.bat檔案或部署項目
- 登陸密碼token位于appsettings.json節點token
- 管理者帳号位于appsettings.json節點superToken
3.主要代碼檔案
- QuartzSettings 檔案夾由系統自動生成,與項目檔案夾同級,存放作業配置資訊及日志資訊(釋出時不需要釋出或複制此檔案夾)
- TaskAuthorizeFilter.cs 帳号權限、AllowAnonymous過濾
- QuartzNETExtension.cs 處理作業添加、删除、修改、停止、啟動功能
- FileQuartz.cs 集中處理作業相關日志
- HttpManager.cs 接口處理
- HealthController 對外開放的健康檢查接口,判斷作業站點是否處理活動狀态。
- TaskOptions.cs 作業相關字段
- TaskBackGround/Index.cshtml 作業UI
- task-index.js 前端Vue+IView
4、作業觸發器操作 QuartzNETExtension.cs
/// <summary>
/// 觸發新增、删除、修改、暫停、啟用、立即執行事件
/// </summary>
/// <param name="schedulerFactory"></param>
/// <param name="taskName"></param>
/// <param name="groupName"></param>
/// <param name="action"></param>
/// <param name="taskOptions"></param>
/// <returns></returns>
public static async Task<object> TriggerAction(this ISchedulerFactory schedulerFactory, string taskName, string groupName, JobAction action, TaskOptions taskOptions = null)
{
string errorMsg = "";
try
{
IScheduler scheduler = await schedulerFactory.GetScheduler();
List<JobKey> jobKeys = scheduler.GetJobKeys(GroupMatcher<JobKey>.GroupEquals(groupName)).Result.ToList();
if (jobKeys == null || jobKeys.Count() == 0)
{
errorMsg = $"未找到分組[{groupName}]";
return new { status = false, msg = errorMsg };
}
JobKey jobKey = jobKeys.Where(s => scheduler.GetTriggersOfJob(s).Result.Any(x => (x as CronTriggerImpl).Name == taskName)).FirstOrDefault();
if (jobKey == null)
{
errorMsg = $"未找到觸發器[{taskName}]";
return new { status = false, msg = errorMsg };
}
var triggers = await scheduler.GetTriggersOfJob(jobKey);
ITrigger trigger = triggers?.Where(x => (x as CronTriggerImpl).Name == taskName).FirstOrDefault();
if (trigger == null)
{
errorMsg = $"未找到觸發器[{taskName}]";
return new { status = false, msg = errorMsg };
}
object result = null;
switch (action)
{
case JobAction.删除:
case JobAction.修改:
await scheduler.PauseTrigger(trigger.Key);
await scheduler.UnscheduleJob(trigger.Key);// 移除觸發器
await scheduler.DeleteJob(trigger.JobKey);
result = taskOptions.ModifyTaskEntity(schedulerFactory, action);
break;
case JobAction.暫停:
case JobAction.停止:
case JobAction.開啟:
result = taskOptions.ModifyTaskEntity(schedulerFactory, action);
if (action == JobAction.暫停)
{
await scheduler.PauseTrigger(trigger.Key);
}
else if (action == JobAction.開啟)
{
await scheduler.ResumeTrigger(trigger.Key);
// await scheduler.RescheduleJob(trigger.Key, trigger);
}
else
{
await scheduler.Shutdown();
}
break;
case JobAction.立即執行:
await scheduler.TriggerJob(jobKey);
break;
}
return result ?? new { status = true, msg = $"作業{action.ToString()}成功" };
}
catch (Exception ex)
{
errorMsg = ex.Message;
return new { status = false, msg = ex.Message };
}
finally
{
FileQuartz.WriteJobAction(action, taskName, groupName, errorMsg);
}
}
UI
5.線上示範位址
- http://132.232.2.109:9950 登陸密碼:task123456
- GitHub:https://github.com/cq-panda/Quartz.NetUI