Orchard Task in a cloud scenario

Topics: Core
Oct 28, 2013 at 3:16 PM

We are planning to migrate our Orchard Multitenant Site from an standalone server to Azure Websites (running in more tan one instance). However we have doubts regarding how we should face a farm scenario like Azure for an Orchard Task that we have on the parent site of our multitenant configuration.

Reading following posts, we concluded the trick is to use TaskLeaseService.Acquire in order to avoid another instance of the site to run at the same time this task_
http://orchard.codeplex.com/discussions/455155 ttps://orchard.codeplex.com/discussions/462734

This a simplified draft of the code we think we would need:
public class MyScheduledTaskHandler : IScheduledTaskHandler
        public const string TaskName = "MyTask";

        private readonly IScheduledTaskManager _taskManager;
        private readonly ITaskLeaseService _taskLeaseService;
        private readonly IMyTaskService _myTaskService;

        public ILogger Logger { get; set; }

        public MyScheduledTaskHandler(IScheduledTaskManager taskManager, ITaskLeaseService taskLeaseService, IMyTaskService myTaskService)
            _taskManager = taskManager;
            _taskLeaseService = taskLeaseService;
            _myTaskService = myTaskService;
            Logger = NullLogger.Instance;

        public void Process(ScheduledTaskContext context)
            if (context.Task.TaskType == TaskName)
                //Acquire the lease
                if (_taskLeaseService.Acquire(TaskName, DateTime.UtcNow.AddDays(30)) == null)

                    //Do work
                catch (Exception e)
                    this.Logger.Error(e, e.Message);
                    //Finalize the lease 
                    _taskLeaseService.Update(TaskName, string.Empty, DateTime.UtcNow.AddMinutes(-30));

                    //schedule next task

        private void ScheduleNextTask()
            var date = DateTime.UtcNow.AddDays(1);
            _taskManager.DeleteTasks(null, a => a.TaskType == TaskName);
            _taskManager.CreateTask(TaskName, date, null);
Do you think this is the best way to do what we want? Or is there a better alternative?

Oct 28, 2013 at 6:54 PM
You don't HAVE to Update the task. If you set a high lease (like you did) it just means the current worker will keep the lock for as long. I would actually suggest a shorter lease, like one hour, this way, if the machine is revoked, another one will be able to process tasks once the lease is lost.

Otherwise it looks good to me. You should follow up on how it behaves on prod, and see if it works correctly. Maybe by adding some log about when a task is processed.
Oct 29, 2013 at 7:30 AM
Edited Oct 29, 2013 at 7:30 AM
Oh, it was a mistake, it should be DateTime.UtcNow.AddMinutes(30).

Thank you Sabastien