public void ExecuteScheduledTask(ScheduledTaskExecutionInfo scheduledTaskInfo)
        {
            try
            {
                ScheduledTaskExecution execution = PrepareScheduledTaskForExecution(scheduledTaskInfo);
                execution.Execute();
                RescheduleTask(scheduledTaskInfo);

                Logger.Debug(string.Format(@"Scheduled Task ""{0}"" has finished.", scheduledTaskInfo.ScheduledTaskName),
                            LogCategories.ScheduledTaskMonitor);

            }
            catch (DataThreadDeadlockException ex)
            {
                Logger.Fatal(string.Format(@"Scheduled Task {0} could not be rescheduled. Exiting...",
                                            scheduledTaskInfo.ScheduledTaskName),
                             ex, OperationContext.LoggingCategory);

                ExitStrategy.Quitting = true;
                throw;
            }
            catch (Exception ex)
            {
                var message = PrepareExecutionErrorMessage(scheduledTaskInfo.ScheduledTaskId, scheduledTaskInfo.ScheduledTaskName, ex);
                Logger.Error(message, ex, OperationContext.LoggingCategory);

                var outcome = HandleScheduledTaskFailure(scheduledTaskInfo.ScheduledTaskId,
                                                         message,
                                                         scheduledTaskInfo.MaximumRetries,
                                                         scheduledTaskInfo.CurrentRetries);

                if (outcome == ScheduledTaskStatusId.Failed)
                    SendFailureReportViaEmail(scheduledTaskInfo.ScheduledTaskId, message);
            }
        }
        public MockedFailedScheduledTaskEngine(ScheduledTaskMonitorContext context, ScheduledTaskExecutionInfo fakeItemForGetScheduledTasksToRun = null)
            : base(context)
        {
            ExitStrategy.Quitting = false; // <<==== IMPORTANT! Otherwise the engine "loop" will just quit before it has even started
            _getReturnItem = fakeItemForGetScheduledTasksToRun;

            FinalStatusOutcomeDeterminedHandler += outcomeStatusId =>
            {
                StatusResult = outcomeStatusId;
                ExitStrategy.Quitting = true;
            };

        }
 private void RescheduleTask(ScheduledTaskExecutionInfo scheduledTaskInfo)
 {
     OperationContext.ThreadSafeDataAccess.DataAccessOperation(() =>
     {
         using (var database = new CloudCoreDB())
         {
             database.Cloudcore_ScheduledTaskUpdateOutcome(
                 scheduledTaskInfo.ScheduledTaskId,
                 (int)ScheduledTaskStatusId.Pending,
                 reason: string.Empty);
         }
     });
 }
        private ScheduledTaskExecution PrepareScheduledTaskForExecution(ScheduledTaskExecutionInfo scheduledTaskInfo)
        {
            ScheduledTaskExecution execution;
            var scheduleTaskGuidName = scheduledTaskInfo.ScheduledTaskGuid.ToString()
                .Replace("-", "_")
                .Replace("{", string.Empty)
                .Replace("}", string.Empty).ToLower();


            if (scheduledTaskInfo.ScheduledTaskType == ExecutionType.Sql)
            {
                var storedProcName = string.Format("[cloudcore].[CCScheduledTask_{0}]", scheduleTaskGuidName);
                execution = new ScheduledTaskExecution(storedProcName, scheduledTaskInfo.ScheduledTaskId, OperationContext.ThreadSafeDataAccess, ExitStrategy);
            }
            else
            {
                var classTypeName = (string.Format(@"_{0}", scheduleTaskGuidName));
                Type scheduledTaskClassType;

                if (!ScheduledTaskClassTypes.TryGetValue(classTypeName, out scheduledTaskClassType))
                {
                    throw new UnknownWorkerTaskTypeException(string.Format(@"A Class Type could not be found in the loaded modules to instantiate " +
                                                                           @"the scheduled task for execution. Scheduled Task GUID {0}." +
                                                                           @"(Have you registered all your modules? Please see " +
                                                                           @"""CloudCore.Deployment.VirtualWorker.Worker.RegisterWorkerModules()"" " +
                                                                           @"for more information.)", scheduledTaskInfo.ScheduledTaskGuid));
                }

                var scheduledTask = ((IScheduledTask)Activator.CreateInstance(scheduledTaskClassType));
                execution = new ScheduledTaskExecution(scheduledTask, scheduledTaskInfo.ScheduledTaskId, OperationContext.ThreadSafeDataAccess, ExitStrategy);
            }
            return execution;
        }