如何运行在果园计划任务? [英] How to run scheduled tasks in Orchard?
问题描述
我要运行一个自动执行的任务,每5个小时。
我发现这个职位上如何创建计划任务,使用IScheduledTaskHandler和IScheduledTaskManager。 使用计划任务果园CMS
我复制相同的code,我加入了过程的功能在我的服务电话。它编译罚款。但我不知道我是否有启动这个计划的任务,就像一个窗口服务启动。它是否得到回升自动后,我构建解决方案?什么时候钟开始计时,如果我想在5小时内运行此作业?如果我想停止/暂停,我该怎么办呢?
感谢。
编辑:
我收到一个异常,如果我尝试启用与任务处理的自定义模块。
异常详细信息:System.ArgumentNullException:值不能为空。参数名:源
241线中:var shellContext = _shellContexts.FirstOrDefault(C => c.Settings.Name == settings.Name);
源文件:\果园1.4的\ src \果园\环境\ DefaultOrchardHost.cs线:241
该_shellContexts快到了为空。如果我从项目/模块删除该任务处理类,一切工作正常。
下面是任务处理code。
公共类ScheduledTaskHandler:IScheduledTaskHandler
{
私人常量字符串任务类型=MyTaskUniqueID;
私人只读IScheduledTaskManager _taskManager;
私人只读IMyService _myService;
公共ILogger记录器{获得;组; }
公共ScheduledTaskHandler(IScheduledTaskManager任务管理器,IMyService为myService)
{
_myService =为myService;
_taskManager =任务管理器;
记录仪= NullLogger.Instance;
尝试
{
日期时间firstDate =新的日期时间()AddMinutes(5)。
ScheduleNextTask(firstDate);
}
赶上(例外五)
{
this.Logger.Error(即e.Message);
}
}
公共无效过程(ScheduledTaskContext上下文)
{
如果(context.Task.TaskType ==任务类型)
{
尝试
{
_myService.RunJob();
}
赶上(例外五)
{
this.Logger.Error(即e.Message);
}
最后
{
日期时间nextTaskDate =新的日期时间()AddHours(5)。
ScheduleNextTask(nextTaskDate);
}
}
}
私人无效ScheduleNextTask(日期时间为准)
{
如果(日期> DateTime.UtcNow)
{
VAR任务= this._taskManager.GetTasks(任务类型);
如果(任务== NULL || tasks.Count()== 0)
this._taskManager.CreateTask(任务类型,日期,空);
}
}
}
当你启动应用程序时钟启动自动滴答 - 你不能停止/暂停。
计划程序以1分钟的间隔 - 它会检查是否存在现在应该运行并执行这些任务。任务都被存储在数据库中 - 相应的记录,始终将删除之前执行任务即将开始(以确保给定的任务将只运行一次)。
如果你需要经常性的工作,要运行,你只需要在previous一个人完成(像你链接到的例子)后,创建一个新的任务。
I have to run a automated job 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 am not sure if I have to 'start' this scheduled task, like a windows service start. Does it get picked up automatically after I build the solution? When does the clock starts ticking if I want to run this job in 5 hours? And if I want to stop/pause, how can I do that?
Thanks.
EDIT:
I am getting an exception if I try to enable the custom module with task handler.
Exception Details: System.ArgumentNullException: 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
The _shellContexts is coming up as null. If I remove the task handler class from the project/module, everything works fine.
Here is the task handler code.
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);
}
}
}
Clock starts ticking automatically when you start the app - you cannot stop/pause it.
Scheduler runs in 1 minute intervals - it checks if there are tasks that should be ran now and runs them. Tasks are being stored in database - corresponding record is always deleted just before task execution is about to start (to ensure that a given task will run only once).
If you need a recurrent job to be ran, you need to create a new task just after the previous one has finished (like in the example you linked to).
这篇关于如何运行在果园计划任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!