/// <summary> /// Initializes the task manager with the property values specified in the configuration file. /// </summary> public void Initialize() { this._taskThreads.Clear(); var taskService = EngineContext.Current.Resolve<IScheduleTaskService>(); var scheduleTasks = taskService.GetAllTasks(); var eventPublisher = EngineContext.Current.Resolve<IEventPublisher>(); eventPublisher.Publish(new AppInitScheduledTasksEvent { ScheduledTasks = scheduleTasks }); // group by threads with the same seconds foreach (var scheduleTaskGrouped in scheduleTasks.GroupBy(x => x.Seconds)) { // create a thread var taskThread = new TaskThread(); taskThread.Seconds = scheduleTaskGrouped.Key; foreach (var scheduleTask in scheduleTaskGrouped) { var taskType = System.Type.GetType(scheduleTask.Type); if (taskType != null) { var isActiveModule = PluginManager.IsActivePluginAssembly(taskType.Assembly); if (isActiveModule) { var job = new Job(scheduleTask); taskThread.AddJob(job); } } } if (taskThread.HasJobs) { this._taskThreads.Add(taskThread); } } //one thread, one task //foreach (var scheduleTask in scheduleTasks) //{ // var taskThread = new TaskThread(scheduleTask); // this._taskThreads.Add(taskThread); // var task = new Task(scheduleTask); // taskThread.AddTask(task); //} }
/// <summary> /// Adds a job to the thread /// </summary> /// <param name="job">The task to be added</param> public void AddJob(Job job) { if (!this._jobs.ContainsKey(job.Name)) { this._jobs.Add(job.Name, job); } }
public ActionResult RunJob(int id, string returnUrl = "") { if (!_permissionService.Authorize(StandardPermissionProvider.ManageScheduleTasks)) return AccessDeniedView(); returnUrl = returnUrl.NullEmpty() ?? Request.UrlReferrer.ToString(); var t = AsyncRunner.Run(c => { try { var svc = c.Resolve<IScheduleTaskService>(); var scheduleTask = svc.GetTaskById(id); if (scheduleTask == null) throw new Exception("Schedule task cannot be loaded"); var job = new Job(scheduleTask); job.Enabled = true; job.Execute(c, false); } catch { try { _scheduleTaskService.EnsureTaskIsNotRunning(id); } catch (Exception) { } } }); // wait only 100 ms. t.Wait(100); if (t.IsCompleted) { if (!t.IsFaulted) { NotifySuccess(T("Admin.System.ScheduleTasks.RunNow.Completed")); } else { NotifyError(t.Exception.Flatten().InnerException); } } else { NotifyInfo(T("Admin.System.ScheduleTasks.RunNow.Progress")); } return Redirect(returnUrl); }
public bool RunScheduleTask() { if (ScheduleTask.IsRunning) { Notifier.Information(GetProgressInfo()); return true; } string scheduleTaskType = ScheduleTaskType; var task = AsyncRunner.Run((container, ct) => { int taskId = 0; try { var svc = container.Resolve<IScheduleTaskService>(); var scheduleTask = svc.GetTaskByType(scheduleTaskType); if (scheduleTask == null) throw new Exception("Schedule task cannot be loaded"); taskId = scheduleTask.Id; var job = new Job(scheduleTask); job.Enabled = true; job.Execute(ct, container, false); } catch (Exception) { _scheduleTaskService.EnsureTaskIsNotRunning(taskId); } }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); task.Wait(100); if (task.IsCompleted) { if (!task.IsFaulted) { Notifier.Success(GetResource("Admin.System.ScheduleTasks.RunNow.Completed")); } else { var exc = task.Exception.Flatten().InnerException; Notifier.Error(exc.Message); Logger.Error(exc.Message, exc); } } else { Notifier.Information(GetResource("Admin.System.ScheduleTasks.RunNow.Progress")); return true; } return false; }