private Option<DroneTaskResult> TryRunDefaultTask(DroneModule module, DroneEnv env) { var task = module.TryGet(DroneModule.DefaultTaskName); if(task.HasValue) { this.log.Debug("default task found, running..."); return Option.From(this.Run(module, task.Value, env, true)); } else { this.log.Warn("no default task found"); return Option.None<DroneTaskResult>(); } }
/// <summary> /// Runs the tasks from the given module /// </summary> /// <param name="module">The module.</param> /// <param name="taskNames">The task names.</param> /// <param name="env">The env.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException">module /// or /// taskNames /// or /// config</exception> public IList<DroneTaskResult> Run(DroneModule module, IEnumerable<string> taskNames, DroneEnv env) { if (module == null) throw new ArgumentNullException("module"); if (taskNames == null) throw new ArgumentNullException("taskNames"); if(env == null) throw new ArgumentNullException("env"); var names = taskNames.ToList(); var results = new List<DroneTaskResult>(); var sw = new Stopwatch(); sw.Start(); if (names.Count == 0) { this.log.Debug("no task names provided, trying to run default task"); var result = this.TryRunDefaultTask(module, env); if(result.HasValue) results.Add(result.Value); } else { this.log.Debug("checking task names exist..."); this.EnsureTaskNamesExists(module, names); this.log.Debug("all task names found"); var taskFaulted = false; this.log.Debug("running tasks..."); foreach(var taskName in names) { this.log.Debug("finding task '{0}'", taskName); var task = module.TryGet(taskName); if(taskFaulted) { this.log.Debug("skipping task '{0}' due to previous task failure", taskName); results.Add(new DroneTaskResult(task, DroneTaskState.NotRan, TimeSpan.Zero, Option.None<Exception>())); continue; } this.log.Debug("task '{0}' found!, running...", taskName); var result = this.Run(module, task.Value, env, true); results.Add(result); if (!result.IsSuccess) { this.log.Debug("task '{0}' has failed, skipping all other tasks", taskName); taskFaulted = true; } this.log.Info(string.Empty); } this.log.Debug("all tasks completed"); } if(results.Count == 0) return results; var maxNameLen = results.Max(x => x.Task.Get(t => t.Name.Length, 0)); var totalTime = TimeSpan.FromMilliseconds(results.Select(x => x.TimeElapsed.TotalMilliseconds).Sum()); var summaryTitle = string.Format("tasks: ({0})", HumanTime.Format(totalTime)); this.log.Info(summaryTitle); this.log.Info(string.Join(string.Empty, Enumerable.Repeat("-", summaryTitle.Length))); foreach(var result in results) { var glyph = this.GetTaskStateGlyph(result.State); var state = this.GetTaskStateDesc(result.State); var name = result.Task.Get(x => x.Name, "[null]"); var time = string.Format("({0})", this.GetTaskStateFormatedTime(result.State, result.TimeElapsed)); var fmt = string.Format("{{0}} {{1, -{0}}} {{2, -10}} {{3, -10}}", maxNameLen); this.log.Info(fmt, glyph, name, state, time); } sw.Stop(); this.log.Info(string.Empty); this.log.Info("total time: ({0})", HumanTime.Format(sw.Elapsed)); return results; }
private void EnsureTaskNamesExists(DroneModule module, IList<string> taskNames) { var tasksNotFound = taskNames .Where(x => !module.TryGet(x).HasValue) .Distinct() .ToList(); if (tasksNotFound.Count > 0) throw DroneTasksNotFoundException.Get(tasksNotFound); }