Exemple #1
0
 protected void OnActionExecuting(ActionExecutingEventArgs e)
 {
     if (this.ActionExecuting != null)
     {
         this.ActionExecuting(this, e);
     }
 }
Exemple #2
0
        public void Run(object parameter)
        {
            bool canceled = false;

            try
            {
                this.IsRunning = true;
                List <BaseAction> actions = (List <BaseAction>)parameter;

                foreach (BaseAction action in actions)
                {
                    Stopwatch stopwatch = new Stopwatch();
                    try
                    {
                        string message;

                        // Execution phase
                        ActionExecutingEventArgs eExecuting = new ActionExecutingEventArgs(ActionStatusEnum.Executing, DateTime.MinValue, "", action);
                        OnActionExecuting(eExecuting);

                        // Completion phase
                        stopwatch.Start();
                        bool actionResult = action.Execute(out message);
                        stopwatch.Stop();

                        ActionExecutedEventArgs eExecuted = new ActionExecutedEventArgs((actionResult ? ActionStatusEnum.Succeeded : ActionStatusEnum.Failed), DateTime.Now, message, action, stopwatch.Elapsed);
                        OnActionExecuted(eExecuted);

                        if (!actionResult)
                        {
                            break;
                        }

                        if (eExecuted.Cancel)
                        {
                            canceled = true;
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (stopwatch.IsRunning)
                        {
                            stopwatch.Stop();
                        }

                        ActionExecutedEventArgs eExecuted = new ActionExecutedEventArgs(ActionStatusEnum.Failed, DateTime.Now, "Deployment process terminated: " + ex.Message, action, stopwatch.Elapsed);
                        OnActionExecuted(eExecuted);
                        break;
                    }
                }
            }
            finally
            {
                OnCompleted(new CompletedEventArgs(canceled)); // Check if there are any subscripbers to this event.
                _actionThread  = null;
                this.IsRunning = false;
            }
        }
Exemple #3
0
        protected void RunControlledWorkUnits(object parameter, int maxDegreeOfParallelism)
        {
            bool canceled = false;

            try
            {
                this.IsRunning = true;
                List <BaseAction> actions = (List <BaseAction>)parameter;

                // Partition the list of actions into actions that can be run in parallel
                List <List <BaseAction> > workUnits = new List <List <BaseAction> >();
                List <BaseAction>         workUnit  = new List <BaseAction>();

                BaseAction previousAction = null;
                foreach (BaseAction action in actions)
                {
                    // Different types of actions are grouped in different work units
                    if (previousAction != null && action.GetType() != previousAction.GetType())
                    {
                        workUnits.Add(workUnit);
                        workUnit = new List <BaseAction>();
                    }

                    workUnit.Add(action);
                    previousAction = action;
                }
                if (workUnit.Count > 0)
                {
                    workUnits.Add(workUnit);
                }

                // Iterate over
                foreach (List <BaseAction> partition in workUnits)
                {
                    ParallelLoopResult loopResult = Parallel.ForEach <BaseAction>(partition, new ParallelOptions {
                        MaxDegreeOfParallelism = maxDegreeOfParallelism
                    }, (action, loopState) =>
                    {
                        Stopwatch stopwatch = new Stopwatch();
                        try
                        {
                            string message;

                            // Execution phase
                            ActionExecutingEventArgs eExecuting = new ActionExecutingEventArgs(ActionStatusEnum.Executing, DateTime.MinValue, "", action);
                            OnActionExecuting(eExecuting);

                            // Completion phase
                            stopwatch.Start();
                            bool actionResult = action.Execute(out message);
                            stopwatch.Stop();

                            ActionExecutedEventArgs eExecuted = new ActionExecutedEventArgs((actionResult ? ActionStatusEnum.Succeeded : ActionStatusEnum.Failed), DateTime.Now, message, action, stopwatch.Elapsed);
                            OnActionExecuted(eExecuted);

                            //if (!actionResult) break;
                            //if (!actionResult) loopState.Break();

                            if (eExecuted.Cancel)
                            {
                                canceled = true;
                                loopState.Break();
                                //break;
                            }
                        }
                        catch (Exception ex)
                        {
                            if (stopwatch.IsRunning)
                            {
                                stopwatch.Stop();
                            }

                            ActionExecutedEventArgs eExecuted = new ActionExecutedEventArgs(ActionStatusEnum.Failed, DateTime.Now, "Deployment process terminated: " + ex.Message, action, stopwatch.Elapsed);
                            OnActionExecuted(eExecuted);
                            // break;
                            loopState.Break();
                        }
                    });
                    if (loopResult.LowestBreakIteration.HasValue)
                    {
                        break;
                    }
                }
            }
            finally
            {
                OnCompleted(new CompletedEventArgs(canceled)); // Check if there are any subscripbers to this event.
                _actionThread  = null;
                this.IsRunning = false;
            }
        }