public async Task <bool> Cancel(Task task) { if (task?.JobDetail == null) { return(true); } try { var result = await _generalScheduler.DeleteJob(task.JobDetail.Key); if (result) { task.Cancelled = true; TaskIdManager.Instance.ReleaseId(task.Id); } return(result); } catch (SchedulerException e) { _log.Warn(e); } return(task.Cancelled); }
public async void Schedule(Task task, TimeSpan?startTime = null, TimeSpan?repeatInterval = null, int count = -1) { if (_generalScheduler.IsShutdown) { return; } task.Id = TaskIdManager.Instance.GetNextId(); while (await _generalScheduler.CheckExists(new JobKey(task.Name + task.Id, task.Name))) { task.Id = TaskIdManager.Instance.GetNextId(); } var job = JobBuilder .Create <TaskJob>() .WithIdentity(task.Name + task.Id, task.Name) .Build(); job.JobDataMap.Put("Logger", _log); job.JobDataMap.Put("Task", task); task.JobDetail = job; var triggerBuild = TriggerBuilder .Create() .WithIdentity(job.Key.Name, job.Key.Group); if (startTime == null) { triggerBuild.StartNow(); } else { triggerBuild.StartAt(DateTime.Now.Add((TimeSpan)startTime)); } if (task.Scheduler == null) { triggerBuild.WithSimpleSchedule(scheduler => { if (repeatInterval == null) { return; } scheduler.WithInterval((TimeSpan)repeatInterval); if (count > 0) { scheduler.WithRepeatCount(count); } else if (count == -1) { scheduler.RepeatForever(); } }); } else { triggerBuild.WithSchedule(task.Scheduler); } task.Trigger = triggerBuild.Build(); task.ExecuteCount = 0; task.MaxCount = count; task.ScheduleTime = Helpers.UnixTimeNowInMilli(); await _generalScheduler.ScheduleJob(job, task.Trigger); }