Exemple #1
0
 public override string ToString()
 {
     return(TaskId
            + "," + (IsEnabled ? "enabled" : "disabled")
            + "," + When.ToString()
            + "," + SecondDelay
            + "," + DelayDifference
            + ", (" + Command + ")"
            );
 }
        public TaskInfo ExecuteAction(string taskId, TaskAction action, string assignee, List <VariableInfo> variables)
        {
            string  url  = string.Format("{0}/{1}", tasksUrl, taskId);
            JObject json = new JObject();

            json.Add("action", action.ToString().ToLower());
            if (!string.IsNullOrEmpty(assignee))
            {
                json.Add("assignee", assignee);
            }
            if (variables != null)
            {
                json.Add("variables", JsonConvert.SerializeObject(variables));
            }

            return(ExecuteOperation(url, HttpMethod.POST, "kermit", "kermit", json.ToString()));
        }
        /// <summary>
        /// 触发新增、删除、修改、暂停、启用、立即执行事件
        /// </summary>
        /// <param name="schedulerFactory"></param>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <param name="status"></param>
        /// <param name="taskOptions"></param>
        /// <returns></returns>
        public static async Task<object> TriggerAction(this ISchedulerFactory schedulerFactory, string taskName, string groupName, TaskAction 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 TaskAction.删除:
                    case TaskAction.修改:
                        //暂停触发器,移除的触发器,删除触发器相关联的任务
                        await scheduler.PauseTrigger(trigger.Key);
                        await scheduler.UnscheduleJob(trigger.Key);
                        await scheduler.DeleteJob(trigger.JobKey);
                        result = taskOptions.ModifyTaskEntity(schedulerFactory, action);
                        break;
                    case TaskAction.暂停:
                    case TaskAction.停止:
                    case TaskAction.开启:
                        result = taskOptions.ModifyTaskEntity(schedulerFactory, action);
                        if (action == TaskAction.暂停)
                        {
                            await scheduler.PauseTrigger(trigger.Key);
                        }
                        else if (action == TaskAction.开启)
                        {
                            await scheduler.ResumeTrigger(trigger.Key);
                        }
                        else
                        {
                            await scheduler.Shutdown();
                        }
                        break;
                    case TaskAction.立即执行:
                        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 };
            }
        }