public async Task Run(DevOpsTaskStatus status)
        {
            if (runTask == null)
            {
            }

            await runTask(status);
        }
        //public void Run(ref DevOpsTaskStatus status)
        //{
        //    runActionRef?.Invoke(ref status);
        //}

        public async Task Run(DevOpsTaskStatus status)
        {
            if (runTask == null)
            {
            }

            await runTask(status);



            //runActionRef?.Invoke(ref status);
        }
        private string GetDetails(DevOpsTaskStatus d_status)
        {
            if (d_status == DevOpsTaskStatus.Completed)
            {
                string runPath = GetRunPath();

                if (!string.IsNullOrEmpty(runPath))
                {
                    return(string.Format("<a href='{0}'>Build Packages</a>", Path.Combine(runPath, WebConfigurationManager.AppSettings[BUILD_LOG_DATA_FILE])));
                }
            }

            return(string.Empty);
        }
Exemple #4
0
        /// <summary>
        /// Run collection of Dev Ops Tasks
        /// </summary>
        /// <param name="devOpsTasks"></param>
        public DevOpsTaskStatus Run(CancellationToken ct, IEnumerable <DevOpsTask> devOpsTasks)
        {
            DevOpsTaskStatus result = DevOpsTaskStatus.Faulted;

            try
            {
                var funcCount = BindFuncs(devOpsTasks.Where(t => t.Enabled == true)); //filter out tasks marked as disabled
                if (funcCount <= 0)
                {
                    throw new DevOpsTaskException("Unable to link methods to tasks");
                }
            }
            catch (Exception ex)
            {
                throw new DevOpsTaskException("Unable to bind methods to task declarations", ex);
            }

            _devOpsTasks = devOpsTasks.ToList();
            DevOpsTask currentDevOpsTask;
            int        currentOrder = 0;

            //split into groups by order
            List <List <DevOpsTask> > orderGrouped = new List <List <DevOpsTask> >();
            var devOpsTaskGroups = _devOpsTasks.GroupBy(t => t.Order);

            string correlationId = Guid.NewGuid().ToString();

            BroadcastStatus(String.Format("Started processing tasks {0} {1}", correlationId, DateTime.UtcNow));

            foreach (var devOpsTaskSet in devOpsTaskGroups)
            {
                List <Task> tasksCollection = new List <Task>();

                foreach (var devOpsTask in devOpsTaskSet)
                {
                    if (ct.IsCancellationRequested)
                    {
                        ct.ThrowIfCancellationRequested();
                    }

                    currentDevOpsTask = devOpsTask;
                    currentOrder      = devOpsTask.Order;

                    try
                    {
                        devOpsTask.Status = DevOpsTaskStatus.Started;

                        if (!devOpsTask.Enabled)
                        {
                            devOpsTask.Status = DevOpsTaskStatus.Skipped;
                        }

                        BroadcastStatus(String.Format("{0} {1} {2} ({3}) {4}", devOpsTask.Status, devOpsTask.Description, correlationId, devOpsTask.Order, DateTime.UtcNow));

                        List <Task <DevOpsTaskStatus> > tasks = new List <Task <DevOpsTaskStatus> >();
                        if (devOpsTask.Enabled)
                        {
                            Task <DevOpsTaskStatus> task = new Task <DevOpsTaskStatus>(() =>
                            {
                                BroadcastStatus(String.Format("Task Id: {0} Thread Id {1} {2} ({3}) {4}", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, correlationId, devOpsTask.Order, devOpsTask.Description));

                                var start = Environment.TickCount;

                                if (devOpsTask.func != null)
                                {
                                    try
                                    {
                                        devOpsTask.Status = devOpsTask.func.Invoke(_taskRunnerPath).Result; //implicit wait
                                    }
                                    catch (AggregateException ae)
                                    {//... exception occured within the custom task
                                        HandleException(ae);

                                        //rethrow with devops task details
                                        throw new DevOpsTaskException(devOpsTask, string.Format("Error received while running task: {0}", devOpsTask.Description));
                                    }

                                    if (devOpsTask.Status != DevOpsTaskStatus.Completed)
                                    {//...custom task did not return completed status
                                        //throw with devops task details
                                        throw new DevOpsTaskException(devOpsTask, string.Format("Error task {0} did not return a completed status", devOpsTask.Description));
                                    }
                                }

                                var stop = Environment.TickCount;
                                double elapsedTimeSeconds        = (stop - start) / 1000;
                                devOpsTask.LastExecutionDuration = string.Format("{0:#,##0.00}", elapsedTimeSeconds);

                                if (devOpsTask.Enabled) //only report on enabled tasks
                                {
                                    BroadcastStatus(String.Format("{0} {1} {2} ({3}) {4} {5}", devOpsTask.Status, devOpsTask.Description, correlationId, devOpsTask.Order, devOpsTask.LastExecutionDuration, DateTime.UtcNow));
                                    //task.Enabled = false; //updates to disabled
                                }

                                return(devOpsTask.Status);
                            });

                            tasksCollection.Add(task);
                            task.Start();
                        }
                    }
                    catch (Exception ex)
                    {
                        HandleException(ex);
                        //ToDo: change method of logging as this may also cause an exception
                    }
                }


                Task t = Task.WhenAll(tasksCollection);
                try
                {
                    t.Wait();
                }
                catch (AggregateException ae)
                {
                    HandleException(ae);
                }
                catch (Exception e)
                {
                    HandleException(e);
                }

                if (t.Status == System.Threading.Tasks.TaskStatus.RanToCompletion)
                {
                    BroadcastStatus(String.Format("All tasks in the set completed {0} ({1})", correlationId, currentOrder));
                    result = DevOpsTaskStatus.Completed;
                }
                else
                {
                    BroadcastStatus(String.Format("At least one task failed to run with status: {0} {1}", correlationId, t.Status));

                    //show report of all tasks for brevity
                    foreach (var task in devOpsTaskSet)
                    {
                        if (task.Enabled) //only report on enabled tasks
                        {
                            BroadcastStatus(String.Format("{0} {1} {2} ({3}) {4}", task.Status, task.Description, correlationId, task.Order, task.LastExecutionDuration));
                        }
                    }

                    result = DevOpsTaskStatus.Faulted;

                    break;
                }
            }

            BroadcastStatus(String.Format("Stopped processing tasks {0}", DateTime.UtcNow));

            RaiseTasksCompletedEvent(result.ToString());

            #region Test Raise Exception
            //used to test exception being raised from outside the task library

            /*
             * throw new DevOpsTaskException("this is bad");
             */

            #endregion

            return(result);
        }