Beispiel #1
0
        public override async T.Task <bool> ProcessTaskItemAsync(TaskItem taskItem, CancellationToken token)
        {
            var message = taskItem.GetMessage <TaskEventMessage>();

            this.Logger.Information("Do work for TaskEvent {0}, {1}, {2}, message {3}", message.Id, message.JobType, message.EventVerb, taskItem.Id);

            try
            {
                // TODO: refactor the processor design.
                JobTaskProcessor processor = null;
                switch (message.EventVerb)
                {
                case "cancel":
                    processor = this.Provider.GetService <CancelJobOrTaskProcessor>();
                    break;

                case "start":
                    processor = this.Provider.GetService <StartJobAndTaskProcessor>();
                    break;

                default:
                    break;
                }

                if (processor is ServerObject so)
                {
                    so.CopyFrom(this);
                }

                var result = await processor.ProcessAsync(message, taskItem.GetInsertionTime(), token);

                this.Logger.Information("Finished process {0} {1} {2}, result {3}", message.EventVerb, message.JobId, message.Id, result);
                return(result);
            }
            catch (OperationCanceledException) { return(false); }
            catch (Exception ex)
            {
                this.Logger.Error("Exception occurred when process {0}, {1}, {2}, {3}", message.EventVerb, message.JobId, message.Id, ex);
                await this.Utilities.UpdateJobAsync(message.JobType, message.JobId, j =>
                {
                    j.State = (j.State == JobState.Canceled || j.State == JobState.Finished) ? j.State : JobState.Failed;
                    (j.Events ?? (j.Events = new List <Event>())).Add(new Event()
                    {
                        Content = $"Exception occurred when process job {message.JobId} {message.JobType} {message.EventVerb}. {ex}",
                        Source  = EventSource.Job,
                        Type    = EventType.Alert,
                    });
                }, token, this.Logger);
            }

            return(true);
        }
        public override async T.Task <bool> ProcessTaskItemAsync(TaskItem taskItem, CancellationToken token)
        {
            var message = taskItem.GetMessage <TaskEventMessage>();

            this.Logger.Information("Do work for TaskEvent {0}, {1}, {2}, message {3}", message.Id, message.JobType, message.EventVerb, taskItem.Id);

            try
            {
                // TODO: refactor the processor design.
                JobTaskProcessor processor = null;
                switch (message.EventVerb)
                {
                case "cancel":
                    processor = this.Provider.GetService <CancelJobOrTaskProcessor>();
                    break;

                case "start":
                    processor = this.Provider.GetService <StartJobAndTaskProcessor>();
                    break;

                default:
                    break;
                }

                if (processor is ServerObject so)
                {
                    so.CopyFrom(this);
                }

                var result = await processor.ProcessAsync(message, taskItem.GetInsertionTime(), token);

                this.Logger.Information("Finished process {0} {1} {2}, result {3}", message.EventVerb, message.JobId, message.Id, result);
                return(result);
            }
            catch (OperationCanceledException) { return(false); }
            catch (Exception ex)
            {
                this.Logger.Error("Exception occurred when process {0}, {1}, {2}, {3}", message.EventVerb, message.JobId, message.Id, ex);

                await this.Utilities.FailJobWithEventAsync(
                    message.JobType,
                    message.JobId,
                    $"Exception occurred when process job {message.JobId} {message.JobType} {message.EventVerb}. {ex}",
                    token);
            }

            return(true);
        }