原文:.Net Core中使用Quartz.Net Vue开即用的UI管理

 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/// <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.在线演示地址

 

01-04 12:15