How to run scheduled tasks in Orchard?

I have to run automated work every 5 hours.

I found this post on how to create scheduled tasks using IScheduledTaskHandler and IScheduledTaskManager. Scheduled Tasks Using Orchard CMS

I copied the same code, I added my service call inside the Process function. It compiles fine. But I'm not sure that I need to "start" this scheduled task, for example, start the Windows service. Does this work out automatically after I build the solution? When does the clock start ticking if I want to run this task in 5 hours? And if I want to stop / pause, how can I do this?

Thanks.

EDIT:

I get an exception if I try to enable a custom module with a task handler.

Exception Details: System.ArgumentNullException: The value cannot be null. Parameter Name: Source

Line 241: var shellContext = _shellContexts.FirstOrDefault(c => c.Settings.Name == settings.Name); 

Source file: \ orchard-1.4 \ src \ Orchard \ Environment \ DefaultOrchardHost.cs Line: 241

_shellContexts appears as null. If I remove the task handler class from the project / module, everything will be fine.

Here is the code for the task handler.

 public class ScheduledTaskHandler : IScheduledTaskHandler { private const string TaskType = "MyTaskUniqueID"; private readonly IScheduledTaskManager _taskManager; private readonly IMyService _myService; public ILogger Logger { get; set; } public ScheduledTaskHandler(IScheduledTaskManager taskManager, IMyService myService) { _myService = myService; _taskManager = taskManager; Logger = NullLogger.Instance; try { DateTime firstDate = new DateTime().AddMinutes(5); ScheduleNextTask(firstDate); } catch (Exception e) { this.Logger.Error(e, e.Message); } } public void Process(ScheduledTaskContext context) { if (context.Task.TaskType == TaskType) { try { _myService.RunJob(); } catch (Exception e) { this.Logger.Error(e, e.Message); } finally { DateTime nextTaskDate = new DateTime().AddHours(5); ScheduleNextTask(nextTaskDate); } } } private void ScheduleNextTask(DateTime date) { if (date > DateTime.UtcNow) { var tasks = this._taskManager.GetTasks(TaskType); if (tasks == null || tasks.Count() == 0) this._taskManager.CreateTask(TaskType, date, null); } } } 
+4
source share
3 answers

The clock starts ticking automatically when the application starts - you cannot stop / pause it.

The scheduler works with an interval of 1 minute - it checks to see if there are tasks that must be completed and run them. Tasks are stored in the database - the corresponding record is always deleted only before the start of the task (to ensure that the task will be performed only once).

If you need to complete the current task, you need to create a new task immediately after completing the previous one (for example, in the example to which you are attached).

+4
source

If anyone is interested, I just deleted the starter code from ScheduledTaskHandler. The following, which I imposed on some controller constructor

 private void ScheduleStartTask() { var tasks = _scheduledTaskManager.GetTasks(TaskType); if (tasks == null || tasks.Count() == 0) { var date = _clock.UtcNow.AddSeconds(5); _scheduledTaskManager.CreateTask(TaskType, date, null); } } 

and on the handler

 public void Process(ScheduledTaskContext context) { if (context.Task.TaskType == TaskType) { try { var x = "kuku"; } catch (Exception e) { this.Logger.Error(e, e.Message); } finally { this.ScheduleNextTask(); } } } private void ScheduleNextTask() { var date = _clock.UtcNow.AddSeconds(5); _taskManager.DeleteTasks(null, a => a.TaskType == TaskType); _taskManager.CreateTask(TaskType, date, null); } 
+1
source

Use ILoggerFactory instead of ILogger, and then get an instance of Logger.

 public ScheduledTaskHandler(IScheduledTaskManager taskManager,ILoggerFactory LoggerFactory, IMyService myService) { _myService = myService; _taskManager = taskManager; Logger = NLoggerFactory.CreateLogger(typeof(ScheduledTaskHandler));; try { DateTime firstDate = new DateTime().AddMinutes(5); ScheduleNextTask(firstDate); } catch (Exception e) { this.Logger.Error(e, e.Message); } } 
0
source

All Articles