public override Job2 NewJob(JobInvocationInfo specification)
        {
            if (specification == null)
            {
                throw new ArgumentNullException("specification");
            }

            if (specification.Definition == null)
            {
                throw new ArgumentException(Resources.NewJobDefinitionNull, "specification");
            }

            if (specification.Definition.JobSourceAdapterType != GetType())
            {
                throw new InvalidOperationException(Resources.NewJobWrongType);
            }

            if (specification.Parameters.Count == 0)
            {
                // If there are no parameters passed in, we create one child job with nothing specified.
                specification.Parameters.Add(new CommandParameterCollection());
            }

            // validation has to happen before job creation
            bool?              isSuspendable = null;
            Activity           activity      = ValidateWorkflow(specification, null, ref isSuspendable);
            ContainerParentJob newJob        = GetJobManager().CreateJob(specification, activity);

            // We do not want to generate the warning message if the request is coming from
            // Server Manager
            if (PSSessionConfigurationData.IsServerManager == false)
            {
                foreach (PSWorkflowJob job in newJob.ChildJobs)
                {
                    bool?psPersistValue       = null;
                    PSWorkflowContext context = job.PSWorkflowInstance.PSWorkflowContext;
                    if (context != null && context.PSWorkflowCommonParameters != null && context.PSWorkflowCommonParameters.ContainsKey(Constants.Persist))
                    {
                        psPersistValue = context.PSWorkflowCommonParameters[Constants.Persist] as bool?;
                    }

                    // check for invocation time pspersist value if not true then there is a possibility that workflow is not suspendable.
                    if (psPersistValue == null || (psPersistValue == false))
                    {
                        // check for authoring time definition of persist activity
                        if (isSuspendable != null && isSuspendable.Value == false)
                        {
                            job.Warning.Add(new WarningRecord(Resources.WarningMessageForPersistence));
                            job.IsSuspendable = isSuspendable;
                        }
                    }
                }
            }

            StoreJobIdForReuse(newJob, true);
            _jobRepository.Add(newJob);

            return(newJob);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Workflow instance constructor for shutdown or crashed workflows.
        /// </summary>
        /// <param name="runtime"></param>
        /// <param name="instanceId"></param>
        internal PSWorkflowApplicationInstance(PSWorkflowRuntime runtime, PSWorkflowId instanceId)
        {
            Tracer.WriteMessage("Creating Workflow instance after crash and shutdown workflow.");
            InitializePSWorkflowApplicationInstance(runtime);
            this._definition = null;
            this._metadatas = null;
            this._streams = null;
            this._timers = null;
            this.id = instanceId.Guid;
            this.creationMode = WorkflowInstanceCreationMode.AfterCrashOrShutdown;

            _stores = Runtime.Configuration.CreatePSWorkflowInstanceStore(this);
            this._remoteActivityState = null;            
        }
Exemplo n.º 3
0
        /// <summary>
        /// Workflow instance constructor.
        /// </summary>
        /// <param name="runtime"></param>
        /// <param name="definition">The workflow definition.</param>
        /// <param name="metadata">The metadata which includes parameters etc.</param>
        /// <param name="pipelineInput">This is input coming from pipeline, which is added to the input stream.</param>
        /// <param name="job"></param>
        internal PSWorkflowApplicationInstance(
                                        PSWorkflowRuntime runtime, 
                                        PSWorkflowDefinition definition,
                                        PSWorkflowContext metadata,
                                        PSDataCollection<PSObject> pipelineInput,                                        
                                        PSWorkflowJob job)
        {
            Tracer.WriteMessage("Creating Workflow instance.");
            InitializePSWorkflowApplicationInstance(runtime);
            this._definition = definition;
            this._metadatas = metadata;
            this._streams = new PowerShellStreams<PSObject, PSObject>(pipelineInput);
            RegisterHandlersForDataAdding(_streams);
            this._timers = new PSWorkflowTimer(this);           
            this.creationMode = WorkflowInstanceCreationMode.Normal;

            _job = job;
            _stores = Runtime.Configuration.CreatePSWorkflowInstanceStore(this);

            this._remoteActivityState = new PSWorkflowRemoteActivityState(_stores);
        }
Exemplo n.º 4
0
        internal void LoadWorkflow(CommandParameterCollection commandParameterCollection, Activity activity, string xaml)
        {
            _tracer.WriteMessage(ClassNameTrace, "LoadWorkflow", WorkflowGuidForTraces, this, "BEGIN");
            Dbg.Assert(_workflowInstance == null, "LoadWorkflow() should only be called once by the adapter");

            // If activity hasn't been cached, we can't generate it from _definition.
            if (activity == null)
            {
                bool windowsWorkflow;
                activity = DefinitionCache.Instance.GetActivityFromCache(_definition, out windowsWorkflow);

                if (activity == null)
                {
                    // The workflow cannot be run.
                    throw new InvalidOperationException(Resources.ActivityNotCached);
                }
            }

            string workflowXaml;
            string runtimeAssemblyPath;

            if (string.IsNullOrEmpty(xaml))
            {
                workflowXaml = DefinitionCache.Instance.GetWorkflowXaml(_definition);
                runtimeAssemblyPath = DefinitionCache.Instance.GetRuntimeAssemblyPath(_definition);
            }
            else
            {
                workflowXaml = xaml;
                runtimeAssemblyPath = null;
            }

            _location = null;
            SortStartParameters(activity as DynamicActivity, commandParameterCollection);

            // Set location if ComputerName wasn't specified in the parameters.
            if (string.IsNullOrEmpty(_location)) _location = Constants.DefaultComputerName;
            if (_jobMetadata.ContainsKey(Constants.JobMetadataLocation))
                _jobMetadata.Remove(Constants.JobMetadataLocation);
            _jobMetadata.Add(Constants.JobMetadataLocation, _location);

            if (_jobMetadata.ContainsKey(Constants.WorkflowJobCreationContext))
                _jobMetadata.Remove(Constants.WorkflowJobCreationContext);
            _jobMetadata.Add(Constants.WorkflowJobCreationContext, _jobCreationContext);

            PSWorkflowDefinition definition = new PSWorkflowDefinition(activity, workflowXaml, runtimeAssemblyPath, _definition.RequiredAssemblies);
            PSWorkflowContext metadatas = new PSWorkflowContext(_workflowParameters, _psWorkflowCommonParameters, _jobMetadata, _privateMetadata);

            _workflowInstance = _runtime.Configuration.CreatePSWorkflowInstance(definition, metadatas, _inputCollection, this);
            this.ConfigureWorkflowHandlers();

            // Create a WorkflowApplication instance.
            _tracer.WriteMessage(ClassNameTrace, "LoadWorkflow", WorkflowGuidForTraces, this, "Calling instance loader");

            #if DEBUG
            try
            {
                _workflowInstance.CreateInstance();
            }
            catch (Exception e)
            {
                if (e.Message.IndexOf("Cannot create unknown type", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    // Capture environment to help diagnose: MSFT:246456
                    PSObject inputObject = new PSObject();
                    inputObject.Properties.Add(
                        new PSNoteProperty("activity", activity));
                    inputObject.Properties.Add(
                        new PSNoteProperty("workflowXaml", workflowXaml));
                    inputObject.Properties.Add(
                        new PSNoteProperty("runtimeAssemblyPath", runtimeAssemblyPath));
                    inputObject.Properties.Add(
                        new PSNoteProperty("_definition.RequiredAssemblies", _definition.RequiredAssemblies));

                    string tempPath = System.IO.Path.GetTempFileName();
                    System.Management.Automation.PowerShell.Create().AddCommand("Export-CliXml").
                        AddParameter("InputObject", inputObject).
                        AddParameter("Depth", 10).
                        AddParameter("Path", tempPath).Invoke();

                    throw new Exception("Bug MSFT:246456 detected. Please capture " + tempPath + ", open a new issue " +
                        "at https://github.com/PowerShell/PowerShell/issues/new and attach the file.");
                }
                else
                {
                    throw;
                }
            }
            #else
            _workflowInstance.CreateInstance();
            #endif

            InitializeWithWorkflow(_workflowInstance);
            WorkflowInstanceLoaded = true;
            _tracer.WriteMessage(ClassNameTrace, "LoadWorkflow", WorkflowGuidForTraces, this, "END");
        }
Exemplo n.º 5
0
		private PSWorkflowContext DeserializeWorkflowMetadataFromStore()
		{
			PSWorkflowContext pSWorkflowContext = new PSWorkflowContext();
			Guid id = base.PSWorkflowInstance.Id;
			string str = Path.Combine(this._configuration.InstanceStorePath, id.ToString(), this.Metadatas);
			if (Directory.Exists(str))
			{
				if (!this._disablePersistenceLimits)
				{
					pSWorkflowContext.WorkflowParameters = (Dictionary<string, object>)this.DeserializeObject(this.Decrypt(this.LoadFromFile(Path.Combine(str, this.Input_xml))));
					pSWorkflowContext.PSWorkflowCommonParameters = (Dictionary<string, object>)this.DeserializeObject(this.Decrypt(this.LoadFromFile(Path.Combine(str, this.PSWorkflowCommonParameters_xml))));
					pSWorkflowContext.JobMetadata = (Dictionary<string, object>)this.DeserializeObject(this.Decrypt(this.LoadFromFile(Path.Combine(str, this.JobMetadata_xml))));
					pSWorkflowContext.PrivateMetadata = (Dictionary<string, object>)this.DeserializeObject(this.Decrypt(this.LoadFromFile(Path.Combine(str, this.PrivateMetadata_xml))));
				}
				else
				{
					pSWorkflowContext.WorkflowParameters = (Dictionary<string, object>)this.LoadFromFileAndDeserialize(Path.Combine(str, this.Input_xml));
					pSWorkflowContext.PSWorkflowCommonParameters = (Dictionary<string, object>)this.LoadFromFileAndDeserialize(Path.Combine(str, this.PSWorkflowCommonParameters_xml));
					pSWorkflowContext.JobMetadata = (Dictionary<string, object>)this.LoadFromFileAndDeserialize(Path.Combine(str, this.JobMetadata_xml));
					pSWorkflowContext.PrivateMetadata = (Dictionary<string, object>)this.LoadFromFileAndDeserialize(Path.Combine(str, this.PrivateMetadata_xml));
				}
				return pSWorkflowContext;
			}
			else
			{
				return pSWorkflowContext;
			}
		}
 public virtual PSWorkflowInstance CreatePSWorkflowInstance(PSWorkflowDefinition definition, PSWorkflowContext metadata, PSDataCollection <PSObject> pipelineInput, PSWorkflowJob job)
 {
     return(new PSWorkflowApplicationInstance(this.Runtime, definition, metadata, pipelineInput, job));
 }
		internal PSWorkflowApplicationInstance(PSWorkflowRuntime runtime, PSWorkflowId instanceId)
		{
			this.Tracer = PowerShellTraceSourceFactory.GetTraceSource();
			this.wfAppNeverLoaded = true;
			this.ReactivateSync = new object();
			if (runtime != null)
			{
				this.Tracer.WriteMessage("Creating Workflow instance after crash and shutdown workflow.");
				this._definition = null;
				this._metadatas = null;
				this._streams = null;
				this._timers = null;
				this.id = instanceId.Guid;
				this.creationMode = WorkflowInstanceCreationMode.AfterCrashOrShutdown;
				this.PersistAfterNextPSActivity = false;
				this.suspendAtNextCheckpoint = false;
				base.Runtime = runtime;
				this._stores = base.Runtime.Configuration.CreatePSWorkflowInstanceStore(this);
				this.asyncExecutionCollection = new Dictionary<string, PSActivityContext>();
				base.ForceDisableStartOrEndPersistence = false;
				return;
			}
			else
			{
				throw new ArgumentNullException("runtime");
			}
		}
		internal PSWorkflowApplicationInstance(PSWorkflowRuntime runtime, PSWorkflowDefinition definition, PSWorkflowContext metadata, PSDataCollection<PSObject> pipelineInput, PSWorkflowJob job)
		{
			this.Tracer = PowerShellTraceSourceFactory.GetTraceSource();
			this.wfAppNeverLoaded = true;
			this.ReactivateSync = new object();
			if (runtime != null)
			{
				this.Tracer.WriteMessage("Creating Workflow instance.");
				this._definition = definition;
				this._metadatas = metadata;
				this._streams = new PowerShellStreams<PSObject, PSObject>(pipelineInput);
				this.RegisterHandlersForDataAdding(this._streams);
				this._timers = new PSWorkflowTimer(this);
				this.creationMode = WorkflowInstanceCreationMode.Normal;
				this.PersistAfterNextPSActivity = false;
				this.suspendAtNextCheckpoint = false;
				this._job = job;
				base.Runtime = runtime;
				this._stores = base.Runtime.Configuration.CreatePSWorkflowInstanceStore(this);
				this.asyncExecutionCollection = new Dictionary<string, PSActivityContext>();
				base.ForceDisableStartOrEndPersistence = false;
				return;
			}
			else
			{
				throw new ArgumentNullException("runtime");
			}
		}
Exemplo n.º 9
0
        public override Job2 NewJob(JobInvocationInfo specification)
        {
            bool hasValue;

            if (specification != null)
            {
                if (specification.Definition != null)
                {
                    if (specification.Definition.JobSourceAdapterType == base.GetType())
                    {
                        if (specification.Parameters.Count == 0)
                        {
                            specification.Parameters.Add(new CommandParameterCollection());
                        }
                        bool?              nullable           = null;
                        Activity           activity           = this.ValidateWorkflow(specification, null, ref nullable);
                        ContainerParentJob containerParentJob = this.GetJobManager().CreateJob(specification, activity);
                        if (!PSSessionConfigurationData.IsServerManager)
                        {
                            foreach (PSWorkflowJob childJob in containerParentJob.ChildJobs)
                            {
                                bool?item = null;
                                PSWorkflowContext pSWorkflowContext = childJob.PSWorkflowInstance.PSWorkflowContext;
                                if (pSWorkflowContext != null && pSWorkflowContext.PSWorkflowCommonParameters != null && pSWorkflowContext.PSWorkflowCommonParameters.ContainsKey("PSPersist"))
                                {
                                    item = (bool?)(pSWorkflowContext.PSWorkflowCommonParameters["PSPersist"] as bool?);
                                }
                                if (item.HasValue)
                                {
                                    bool?nullable1 = item;
                                    if (nullable1.GetValueOrDefault())
                                    {
                                        hasValue = false;
                                    }
                                    else
                                    {
                                        hasValue = nullable1.HasValue;
                                    }
                                    if (!hasValue)
                                    {
                                        continue;
                                    }
                                }
                                if (!nullable.HasValue || nullable.Value)
                                {
                                    continue;
                                }
                                childJob.Warning.Add(new WarningRecord(Resources.WarningMessageForPersistence));
                                childJob.IsSuspendable = nullable;
                            }
                        }
                        base.StoreJobIdForReuse(containerParentJob, true);
                        this._jobRepository.Add(containerParentJob);
                        return(containerParentJob);
                    }
                    else
                    {
                        throw new InvalidOperationException(Resources.NewJobWrongType);
                    }
                }
                else
                {
                    throw new ArgumentException(Resources.NewJobDefinitionNull, "specification");
                }
            }
            else
            {
                throw new ArgumentNullException("specification");
            }
        }
Exemplo n.º 10
0
		internal void LoadWorkflow(CommandParameterCollection commandParameterCollection, Activity activity, string xaml)
		{
			bool flag = false;
			string workflowXaml;
			string runtimeAssemblyPath;
			this._tracer.WriteMessage("PSWorkflowJob", "LoadWorkflow", this.WorkflowGuidForTraces, this, "BEGIN", new string[0]);
			if (activity == null)
			{
				activity = DefinitionCache.Instance.GetActivityFromCache(this._definition, out flag);
				if (activity == null)
				{
					throw new InvalidOperationException(Resources.ActivityNotCached);
				}
			}
			if (!string.IsNullOrEmpty(xaml))
			{
				workflowXaml = xaml;
				runtimeAssemblyPath = null;
			}
			else
			{
				workflowXaml = DefinitionCache.Instance.GetWorkflowXaml(this._definition);
				runtimeAssemblyPath = DefinitionCache.Instance.GetRuntimeAssemblyPath(this._definition);
			}
			this._location = null;
			this.SortStartParameters(activity as DynamicActivity, commandParameterCollection);
			if (string.IsNullOrEmpty(this._location))
			{
				this._location = "localhost";
			}
			if (this._jobMetadata.ContainsKey("Location"))
			{
				this._jobMetadata.Remove("Location");
			}
			this._jobMetadata.Add("Location", this._location);
			PSWorkflowDefinition pSWorkflowDefinition = new PSWorkflowDefinition(activity, workflowXaml, runtimeAssemblyPath);
			PSWorkflowContext pSWorkflowContext = new PSWorkflowContext(this._workflowParameters, this._psWorkflowCommonParameters, this._jobMetadata, this._privateMetadata);
			this._workflowInstance = this._runtime.Configuration.CreatePSWorkflowInstance(pSWorkflowDefinition, pSWorkflowContext, this._inputCollection, this);
			this.ConfigureWorkflowHandlers();
			this._tracer.WriteMessage("PSWorkflowJob", "LoadWorkflow", this.WorkflowGuidForTraces, this, "Calling instance loader", new string[0]);
			this._workflowInstance.CreateInstance();
			this.InitializeWithWorkflow(this._workflowInstance, false);
			this.WorkflowInstanceLoaded = true;
			this._tracer.WriteMessage("PSWorkflowJob", "LoadWorkflow", this.WorkflowGuidForTraces, this, "END", new string[0]);
		}