Implementing IScheduledTaskHandler, Process is not called

Topics: Troubleshooting
Aug 29, 2013 at 1:18 PM
Hi, I have implemented IScheduledTaskHandler, and inside my constructor I am calling ScheduleNextTask() and I can see with success. Everytime the ScheduleNextTask is called my log is updated and a new entry is created inside the db table [Scheduling_ScheduledTaskRecord]. The issue is that the Process method as show below is not called. Any ideas why? :(
public void Process(ScheduledTaskContext context)
{
            Logger.Warning("Process() " + TaskType);
}
Aug 30, 2013 at 12:30 PM
I would like some help with this please.
Aug 30, 2013 at 1:46 PM
I have post my code as well to make this more clear. WHen the app pool is restarted ScheduleNextTask() is called I can see that in the logs and record in db is created. But when the time comes the Process method is not called.
public class ScheduledTaskHandler : IScheduledTaskHandler
    {
        private static bool started = false;
        private const int INTERVAL = 2; //MINUTES
        private const string TaskType = "MyTask";
        private readonly IScheduledTaskManager _taskManager;        
        private readonly IRepository<OnePartRecord> _repository1;
        private readonly IRepository<TwoPartRecord> _repository2;
        private readonly IRepository<ThreePartRecord> _repository3;
        private readonly IContentManager _contentManager;
        public ILogger Logger { get; set; }

        public ScheduledTaskHandler(IScheduledTaskManager taskManager,
                                    ILoggerFactory LoggerFactory,
                                    IRepository<OnePartRecord> repository1,
                                    IRepository<TwoPartRecord> repository2,
                                    IRepository<ThreePartRecord> repository3,
                                    IContentManager contentManager)
        {
            try
            {
                _taskManager = taskManager;
                Logger = LoggerFactory.CreateLogger(typeof(ScheduledTaskHandler));
                _repository1 = repository1;
                _repository2 = repository2;
                _repository3 = repository3;
                _contentManager = contentManager;
                if (!started)
                {
                    started = true;
                    ScheduleNextTask();
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "ScheduledTaskHandler.Constructor failed!");
            }
        }

        public void Process(ScheduledTaskContext context)
        {            
            Logger.Warning("Processing... " + TaskType);
            if (context.Task.TaskType == TaskType)
            {
                try
                {                    
                    var rss = new MyService(_repository1, _contentManager, _repository2, _repository3, Logger);
                    rss.Synchronize();
                }
                catch (Exception e)
                {
                    Logger.Error(e, "ScheduledTaskHandler.Process exception caught!");
                }
                finally
                {                    
                    ScheduleNextTask();
                }
            }
        }

        private void ScheduleNextTask()
        {
            try
            {
                var date = DateTime.UtcNow.AddMinutes(INTERVAL);
                _taskManager.DeleteTasks(null, a => a.TaskType == TaskType);
                _taskManager.CreateTask(TaskType, date, null);
                Logger.Warning("ScheduleNextTask " + date.ToString());
            }
            catch (Exception e)
            {
                Logger.Error(e, "ScheduledTaskHandler.ScheduleNextTask failed to schedule next task!");
            }
        }
    }