/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="code"></param> /// <param name="scalar"></param> /// <returns></returns> private static T ExecuteInternal <T>(PipelineAction code, bool scalar) { using (Pipeline pipe = Runspace.DefaultRunspace.CreateNestedPipeline()) { code.Invoke(pipe); Collection <PSObject> results = pipe.Invoke(); object returnValue = results; if (scalar) { // even a null result ends up in a collection of length 1. returnValue = results[0]; } return((T)LanguagePrimitives.ConvertTo(returnValue, typeof(T))); } }
/// <summary> /// This method must be called whenever the processing stops in a stage /// of the pipeline because an event must be received to continue. If /// Deadline is not MaxValue, the timer thread will run the pipeline again /// at Deadline. The pipeline will do another full run when this method is /// called. Do not call this outside the pipeline. /// </summary> private void StallPipeline(String reason, DateTime deadline) { Debug.Assert(StageAction == PipelineAction.None); String ls = "Pipeline stall in stage " + CurrentStage + ": " + reason + "."; if (deadline != DateTime.MaxValue) ls += " Wake-up date: " + deadline + "."; else ls += " Wake-up date: none."; Logging.Log(ls); TimerDate = deadline; StageAction = PipelineAction.Stall; }
/// <summary> /// Pass to the next pipeline stage. Do not call this outside the /// pipeline. /// </summary> private void NextStage() { Debug.Assert(StageAction == PipelineAction.None); StageAction = PipelineAction.Pass; PassFlag = false; }
/// <summary> /// Run the pipeline until it stalls. If FullFlag is true, all the /// stages of the pipeline will be synchronized. /// </summary> public void Run(String reason, bool fullFlag) { Logging.Log(1, "Running pipeline in stage " + CurrentStage + ": " + reason + ", fullFlag: " + fullFlag + "."); QueuedRun = false; // Set the FullSyncFlag as needed. This is done even if the pipeline // is being re-entered. if (fullFlag) FullSyncFlag = true; // Set the next run date to null. We don't want to be stuck in the // exit stage. This is done even if the pipeline is being re-entered. NextRunDate = DateTime.MinValue; // Don't re-enter the pipeline. if (RunFlag) { Logging.Log("The pipeline is already running, exiting."); return; } // We're now running the pipeline. RunFlag = true; try { while (true) { // We stall the pipeline when the UI gate has been entered. if (Share.Gate.EntryCount > 0) { Logging.Log("The UI gate has been entered, stalling the pipeline."); break; } StageAction = PipelineAction.None; // Process the current stage. if (CurrentStage == PipelineStage.Entry) EntryStage(); else if (CurrentStage == PipelineStage.InitScan) InitScanStage(); else if (CurrentStage == PipelineStage.ServerOp) ServerOpStage(); else if (CurrentStage == PipelineStage.PartialScan) PartialScanStage(); else if (CurrentStage == PipelineStage.Transfer) TransferStage(); else if (CurrentStage == PipelineStage.LocalUpdate) LocalUpdateStage(); else if (CurrentStage == PipelineStage.UI) UiStage(); else if (CurrentStage == PipelineStage.Exit) ExitStage(); else Debug.Assert(false); // Stall the pipeline. if (StageAction == PipelineAction.Stall) { // Disable the timer. if (TimerDate == DateTime.MaxValue) WakeupTimer.WakeMeUp(-1); // Enable the timer. else WakeupTimer.WakeMeUp(Math.Max((Int64)(TimerDate - DateTime.Now).TotalMilliseconds + 1, 1)); break; } // Pass to the next stage. else if (StageAction == PipelineAction.Pass) { CurrentStage = (PipelineStage)(((int)CurrentStage + 1) % 8); } // Oops. else throw new Exception("pipeline action not set"); } } catch (Exception ex) { Share.FatalError(ex); } // We're no longer running the pipeline. RunFlag = false; }
public static IStageCommandConfig <Visitor> WithVisitCommand(this IStageCommandConfig <Visitor> commandConfig, string commandName, PipelineAction actionAfterVisit = PipelineAction.Continue, PipelineAction actionAfterPostVisit = PipelineAction.Continue) { var stringCommand = new CommandDefinition <Visitor, string>( (s, arg) => { s.Visit(arg.Payload); return(actionAfterVisit); }, (s, arg) => { s.PostVisit(arg.Payload); return(actionAfterPostVisit); }); commandConfig .WithCommand(commandName, stringCommand); return(commandConfig); }
public static T ExecuteScalar <T>(PipelineAction code) { return(ExecuteInternal <T>(code, true)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="code"></param> /// <returns></returns> public static T Execute <T>(PipelineAction code) { return(ExecuteInternal <T>(code, false)); }
public JunkImporter( PipelineAction pipelineAction ) { _pipelineAction = pipelineAction; }