Exemplo n.º 1
0
        private TaskExecutionResult ExecuteInternal <TWorkItem>(ITask <TWorkItem> task, Arguments arguments)
        {
            TWorkItem tWorkItem;

            if (task == null)
            {
                throw new ArgumentNullException("task");
            }
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }
            List <string>   strs   = new List <string>();
            Action <string> action = (string message) => {
                message = string.Format("[{0:HH:mm:ss}] {1}", Time.Now, message);
                this._outputter.WriteLine(message);
                strs.Add(message);
            };
            ILogger logger = this._logger;

            using (TaskLog taskLog = new TaskLog(task, new Action <LogEntry>(logger.LogEntry), new Output(action)))
            {
                try
                {
                    tWorkItem = task.Start(new TaskExecutionContext(new Log(new Action <string>(taskLog.LogMessage), this._logger), arguments));
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    taskLog.ErrorLog = this._logger.LogError(exception, null);
                    throw new TaskExecutionFailedException("Starting task failed.", exception);
                }
                try
                {
                    foreach (IStep <TWorkItem> step in task.Steps)
                    {
                        Execution execution = step.ContinueWith(tWorkItem);
                        if (execution != Execution.StepOut)
                        {
                            if (execution == Execution.StepOver)
                            {
                                continue;
                            }
                            using (StepLog stepLog = taskLog.LogStep(step))
                            {
                                try
                                {
                                    step.Execute(tWorkItem, new TaskExecutionContext(new Log(new Action <string>(stepLog.LogMessage), this._logger), arguments));
                                }
                                catch (Exception exception3)
                                {
                                    Exception exception2 = exception3;
                                    ErrorLog  errorLog   = this._logger.LogError(exception2, null);
                                    taskLog.ErrorLog = errorLog;
                                    stepLog.ErrorLog = errorLog;
                                    throw new TaskExecutionFailedException(string.Format("Step '{0}' failed.", stepLog.Name), exception2);
                                }
                            }
                        }
                        else
                        {
                            goto Label0;
                        }
                    }
Label0:
                    try
                    {
                        task.End(tWorkItem, new TaskExecutionContext(new Log(new Action <string>(taskLog.LogMessage), this._logger), arguments));
                    }
                    catch (Exception exception5)
                    {
                        Exception exception4 = exception5;
                        taskLog.ErrorLog = this._logger.LogError(exception4, null);
                        throw new TaskExecutionFailedException("Ending task failed.", exception4);
                    }
                }
                finally
                {
                    tWorkItem.DisposeIfDisposable();
                }
            }
            return(new TaskExecutionResult(strs.ToArray()));
        }
Exemplo n.º 2
0
        private TaskExecutionResult ExecuteInternal <TWorkItem>(ITask <TWorkItem> task, Arguments arguments)
        {
            if (task == null)
            {
                throw new ArgumentNullException(nameof(task));
            }
            if (arguments == null)
            {
                throw new ArgumentNullException(nameof(arguments));
            }

            var output = new List <string>();

            void Outputter(string message)
            {
                message = $"[{Time.Now:HH:mm:ss}] {message}";

                _console.WriteLine(message);
                output.Add(message);
            }

            using (var taskLog = new TaskLog(task, _logger.LogEntry, new Output(Outputter)))
                using (ConcurrentTaskExecutionResult taskExecution = HandleConcurrentTaskExecution(task, arguments, taskLog))
                {
                    if (taskExecution.StopTask)
                    {
                        return(new TaskExecutionResult(output.ToArray()));
                    }

                    Action <string>[] logMessage = { x => { } };

                    var log     = new Log(message => logMessage[0]?.Invoke(message), _logger);
                    var context = new TaskExecutionContext <TWorkItem>(taskLog, log, arguments, _shutdown.Token);

                    if (task.IsDisabled(context))
                    {
                        Outputter($"Task '{task.Name()}' is disabled.");

                        return(new TaskExecutionResult(output.ToArray()));
                    }

                    try
                    {
                        logMessage[0] = taskLog.LogMessage;
                        context.ThrowIfCancelled();

                        context.WorkItem = task.Start(context);
                    }
                    catch (Exception ex)
                    {
                        ErrorLog errorLog = _logger.LogError(ex);
                        taskLog.ErrorLog = errorLog;

                        throw new TaskExecutionFailedException($"Starting Task '{taskLog.Name}' failed with message: '{ex.DestructMessage()}'. ErrorID: {errorLog?.Id ?? "<null>"}.", ex);
                    }

                    try
                    {
                        foreach (IStep <TWorkItem> step in task.Steps)
                        {
                            using (StepLog stepLog = taskLog.LogStep(step))
                            {
                                try
                                {
                                    logMessage[0] = stepLog.LogMessage;
                                    context.ThrowIfCancelled();

                                    Execution continueWith = step.ContinueWith(context);

                                    if (continueWith == Execution.StepOut)
                                    {
                                        break;
                                    }

                                    if (continueWith == Execution.StepOver)
                                    {
                                        continue;
                                    }

                                    context.ThrowIfCancelled();

                                    step.Execute(context);
                                }
                                catch (Exception ex)
                                {
                                    ErrorLog errorLog = _logger.LogError(ex);

                                    taskLog.ErrorLog = errorLog;
                                    stepLog.ErrorLog = errorLog;

                                    throw new TaskExecutionFailedException($"Step '{stepLog.Name}' on Task '{taskLog.Name}' failed with message: '{ex.Message}'. ErrorID: {errorLog?.Id ?? "<null>"}.", ex);
                                }
                            }
                        }

                        try
                        {
                            logMessage[0] = taskLog.LogMessage;
                            context.ThrowIfCancelled();

                            task.End(context);
                        }
                        catch (Exception ex)
                        {
                            ErrorLog errorLog = _logger.LogError(ex);
                            taskLog.ErrorLog = errorLog;

                            throw new TaskExecutionFailedException($"Ending Task '{taskLog.Name}' failed with message: '{ex.Message}'. ErrorID: {errorLog?.Id ?? "<null>"}.", ex);
                        }
                    }
                    finally
                    {
                        context.WorkItem.DisposeIfDisposable();
                    }
                }

            return(new TaskExecutionResult(output.ToArray()));
        }