Exemplo n.º 1
0
        public override void JobWasExecuted(Quartz.IJobExecutionContext context, Quartz.JobExecutionException jobException)
        {
            base.JobWasExecuted(context, jobException);

            long scheduledTaskId = (long)context.MergedJobDataMap["scheduledTaskId"];
            ScheduledTaskRunStatistics lastRunStatistics = new ScheduledTaskRunStatistics();

            lastRunStatistics.StartTimeUtc = context.FireTimeUtc.HasValue ? context.FireTimeUtc.Value.DateTime : new DateTime?();
            lastRunStatistics.EndTimeUtc   = context.FireTimeUtc.HasValue ? context.FireTimeUtc.Value.Add(context.JobRunTime).DateTime : new DateTime?();
            if (jobException != null)
            {
                lastRunStatistics.Status = ScheduledTaskStatus.Failed;
                lastRunStatistics.Error  = jobException.Message;
            }
            else
            {
                ExecuteSequenceJobResult result = (ExecuteSequenceJobResult)context.Result;
                lastRunStatistics.Status = result.RunStatus;
                lastRunStatistics.Error  = result.RunError;
            }

            string connectionString         = (string)context.MergedJobDataMap["connectionString"];
            SchedulingController controller = new SchedulingController(connectionString);

            controller.SetScheduledTaskLastRunStatistics(scheduledTaskId, lastRunStatistics);

            zOnTaskComplete(new TaskEventArgs(scheduledTaskId));

            m_IsJobRunning = false;
        }
Exemplo n.º 2
0
        public void Execute(IJobExecutionContext context)
        {
            ThreadSynchronizer threadSynchronizer = null;
            frmAutomation      frmAutomation      = null;

            try
            {
                m_JobKey = context.JobDetail.Key;
                m_Logger.InfoFormat("ExecuteSequenceJob {0} executing at {1}.", m_JobKey, DateTime.Now.ToString("r"));

                string connectionString = (string)context.MergedJobDataMap["connectionString"];
                AutomationController automationController = new AutomationController(connectionString);
                SchedulingController schedulingController = new SchedulingController(connectionString);

                long           scheduledTaskId = (long)context.MergedJobDataMap["scheduledTaskId"];
                ScheduledTask  scheduledTask   = schedulingController.GetScheduledTask(scheduledTaskId, DeletedInclusion.All);
                SequenceDetail sequenceDetail  = automationController.GetSequenceDetail(scheduledTask.TaskSequence.SequenceId);

                threadSynchronizer = (ThreadSynchronizer)context.MergedJobDataMap["threadSynchronizer"];
                threadSynchronizer.RunOnSynchronizedThread(() => frmAutomation = new frmAutomation(), true);

                using (AutomationEngine automationEngine = new AutomationEngine(frmAutomation.Browser))
                {
                    automationEngine.SetSequence(sequenceDetail.SequenceSteps);
                    Dictionary <string, IStateVariable> persistedData = automationController.GetSequencePersistedData(sequenceDetail.Sequence.SequenceId);
                    automationEngine.DataContext.LoadPersistedVariables(persistedData);

                    automationEngine.ExecutionComplete += automationEngine_ExecutionComplete;
                    using (m_ExecutionWaitHandle = new AutoResetEvent(false))
                    {
                        automationEngine.ExecuteSequence();

                        if (!m_ExecutionWaitHandle.WaitOne(scheduledTask.RunDurationLimit.HasValue
                            ? scheduledTask.RunDurationLimit.Value
                            : TimeSpan.FromMilliseconds(-1)))
                        {
                            throw new TimeoutException(
                                      String.Format("Scheduled sequence execution timed out because it reached the specified Run Duration Limit of {0}.",
                                                    scheduledTask.RunDurationLimit));
                        }
                    }
                    automationEngine.ExecutionComplete -= automationEngine_ExecutionComplete;

                    persistedData = automationEngine.DataContext.GetPersistedVariables();
                    automationController.SetSequencePersistedData(sequenceDetail.Sequence.SequenceId, persistedData);
                }

                ExecuteSequenceJobResult result = new ExecuteSequenceJobResult(
                    zGetScheduledTaskStatusFromExecutionState(m_ExecutionCompleteEventArgs.RunResult),
                    "");     //TODO: get error from ExecutionCompleteEventArgs
                context.Result = result;
            }
            catch (Exception ex)
            {
                JobExecutionException jobEx = new JobExecutionException(ex, false);
                throw jobEx;
            }
            finally
            {
                //Clean up
                try
                {
                    if (frmAutomation != null)
                    {
                        threadSynchronizer.RunOnSynchronizedThread(() => frmAutomation.Dispose(), true);
                    }
                }
                catch (Exception ex)
                {
                    if (m_Logger != null)
                    {
                        m_Logger.ErrorFormat("Cleanup failed for ExecuteSequenceJob {0}: {1}", m_JobKey, ex.Message);
                    }
                }

                //Dereference global variables
                m_Logger = null;
                m_JobKey = null;
                m_ExecutionCompleteEventArgs = null;
                m_ExecutionWaitHandle        = null;
            }
        }