Example #1
0
        public void Execute(Task task)
        {
            _log.Debug(string.Format("Task Started {0} at {1}", task.WorkItemId, DateTime.Now));
            if (!_fileSystem.Exists(task.Runner))
            {
                var taskExecutionResult = new TaskExecutionResult(TaskExecutionResult.TaskExecutionStatus.Failed,
                                                                  task.WorkItemId, null);
                taskExecutionResult.AddLogEntry(RunnerNotFoundOnAgent.FormatWith(new { task.Runner }));
                _channel.WorkComplete(taskExecutionResult);
                return;
            }

            CreateWorkingDirectory(task.WorkDirectoryArchive);
            var startInfo = new ProcessStartInfo
            {
                WorkingDirectory = WorkingDirectory,
                FileName         = task.Runner,
                Arguments        = task.Arguments
            };
            var resetEvent = new ManualResetEvent(false);

            //spawn process on a new thread to prevent take down of main thread
            ThreadPool.QueueUserWorkItem(state =>
            {
                try
                {
                    EventHandler <string> exitEvent = (sender, args) =>
                    {
                        //signal to main thread that our work is done
                        resetEvent.Set();
                        var zip = GetsArtifacts(task);
                        var taskExecutionResult =
                            new TaskExecutionResult(TaskExecutionResult.TaskExecutionStatus.Succeeded, task.WorkItemId,
                                                    zip);
                        taskExecutionResult.AddLogEntry(args);
                        _channel.WorkComplete(taskExecutionResult);
                    };
                    EventHandler <string> outputEvent = (sender, output) => _log.WriteToConsole(output);

                    _processRunner.Start(startInfo, exitEvent, outputEvent);
                }
                catch (Exception e)
                {
                    _log.Error(e);
                    resetEvent.Set();
                    var result = new TaskExecutionResult(TaskExecutionResult.TaskExecutionStatus.Failed, task.WorkItemId, null);
                    result.AddException(e);
                    _channel.WorkComplete(result);
                }
            });
            resetEvent.WaitOne();
            _log.Debug(string.Format("Task Ended {0} at {1}", task.WorkItemId, DateTime.Now));
        }
Example #2
0
        public void Execute_ShouldSendFailureToServerChannel_WhenProcessStartThrows()
        {
            var task = AutoFixture.Create <Task>();
            var expectedTaskResult = new TaskExecutionResult(TaskExecutionResult.TaskExecutionStatus.Failed, task.WorkItemId, null);
            var exception          = AutoFixture.Create <System.ComponentModel.Win32Exception>();

            expectedTaskResult.AddException(exception);

            _processRunner.Setup(p => p.Start(It.IsAny <ProcessStartInfo>(), It.IsAny <EventHandler <string> >(), It.IsAny <EventHandler <string> >())).Throws(exception);
            _fileSystemMock.Setup(fs => fs.Exists(It.IsAny <string>())).Returns(true);

            _taskExecutor.Execute(task);

            _serverChannel.Verify(sc => sc.WorkComplete(expectedTaskResult));
        }