Exemplo n.º 1
0
        /// <summary>
        /// Processes tasks that are ready to run.
        /// </summary>
        protected override void ProcessTasks()
        {
            // --- Create the task, and dispose it when processed
            using (var newTask = new TTask())
            {
                var success   = true;
                var stopwatch = new Stopwatch();
                try
                {
                    stopwatch.Restart();

                    // --- Setup with graceful cancellation
                    if (ShouldStopTaskProcessing)
                    {
                        return;
                    }
                    newTask.Setup(Context);

                    // --- Run with graceful cancellation
                    CancellationToken.ThrowIfCancellationRequested();
                    newTask.Run();
                    NumTasksPmc.Increment();
                    NumTasksPerSecondPmc.Increment();
                }
                catch (OperationCanceledException ex)
                {
                    // --- The message procession canceled, log this failure.
                    WindowsEventLogger.Log <TaskExecutionInterrupted>(
                        "Task execution interrupted while processing a scheduled task.", ex);
                    success = false;
                }
                catch (Exception ex)
                {
                    // --- The message procession failed, log this failure.
                    WindowsEventLogger.Log <TaskExecutionFailed>(
                        "Task execution failed while processing scheduled task.", ex);
                    success = false;
                }
                finally
                {
                    // --- Set up the next time when the task should run
                    _nextTimeToRun = ScheduleInfo.NextTimeToRun(EnvironmentInfo.GetCurrentDateTimeUtc());
                }
                stopwatch.Stop();
                if (!success)
                {
                    NumFailuresPmc.Increment();
                    NumFailuresPerSecondPmc.Increment();
                }
                LastProcessTimePmc.RawValue = (int)stopwatch.ElapsedMilliseconds;
            }
        }
        /// <summary>
        /// Processes a single message
        /// </summary>
        /// <param name="message">Message to process</param>
        /// <returns></returns>
        private bool ProcessMessage(IPoppedMessage message)
        {
            if (ShouldStopTaskProcessing)
            {
                return(true);
            }
            var task = new TTask();

            // --- Set input arguments
            task.Argument = task.ArgumentConverter.ConvertToArgument(message.MessageText);

            // --- Prepare task to run
            var cancel = false;

            OnTaskProcessing(task, ref cancel);

            var stopwatch = new Stopwatch();

            try
            {
                // --- Setup with graceful cancellation
                if (ShouldStopTaskProcessing)
                {
                    return(true);
                }
                task.Setup(Context);

                // --- Run with graceful cancellation
                CancellationToken.ThrowIfCancellationRequested();
                task.Run();
                OnTaskProcessed(task);
                NumTasksPmc.Increment();
                NumTasksPerSecondPmc.Increment();

                // --- At this point the message should be removed from the queue
                _requestQueue.DeleteMessage(message);
                return(false);
            }
            catch
            {
                NumFailuresPmc.Increment();
                NumFailuresPerSecondPmc.Increment();
                throw;
            }
            finally
            {
                LastProcessTimePmc.RawValue = (int)stopwatch.ElapsedMilliseconds;
                task.Dispose();
            }
        }