//sample that shows how to perform Live INstance Management //in this example, we want to migrate all active instances of a workflow to the latest version //Note: take due care when migrating active process instances since not all migration scenarios are supported public void LiveInstanceManagementSample() { //establish the connection WorkflowManagementServer K2Mgmt = new WorkflowManagementServer(); K2Mgmt.CreateConnection(); K2Mgmt.Connection.Open("connectionstring"); //get the proc set ID of the workflow so that we can get the latest version of the proc set SourceCode.Workflow.Management.Criteria.ProcessCriteriaFilter filter = new SourceCode.Workflow.Management.Criteria.ProcessCriteriaFilter(); Processes procs = null; filter.AddRegularFilter(ProcessFields.ProcessFullName, Comparison.Equals, "processFullName"); procs = K2Mgmt.GetProcesses(filter); int procSetId = procs[0].ProcSetID; //now get the latest version of the procset int latestVersion = 0; int latestVersionId = 0; Processes procVersions = K2Mgmt.GetProcessVersions(procSetId); foreach (Process procVersion in procVersions) { if (procVersion.VersionNumber > latestVersion) { latestVersion = procVersion.VersionNumber; latestVersionId = procVersion.ProcID; } } //now that we have the latest version of the workflow, //get the ID's of all the process instances of the workflow ProcessInstances procInstances = K2Mgmt.GetProcessInstancesAll("processFullName", "", ""); //leaving parameters blank effectively ignores that parameter foreach (ProcessInstance procInst in procInstances) { //no need to migrate ones that are already on this version if (procInst.ExecutingProcID != latestVersionId) { //must stop a non-stopped instance before attempting migration if (procInst.Status != "Stopped") { K2Mgmt.StopProcessInstances(procInst.ID); } //now migrate the instance to the latest version K2Mgmt.SetProcessInstanceVersion(procInst.ID, latestVersion); //restart the process K2Mgmt.StartProcessInstances(procInst.ID); } } //close the connection K2Mgmt.Connection.Close(); }
private void RunGotoActivity(DataTable results = null) { int processInstanceId = base.GetIntProperty(Constants.SOProperties.ProcessInstanceManagement.ProcessInstanceId); bool ignoreProcessInstanceNotExists = base.GetBoolProperty(Constants.SOProperties.ProcessInstanceManagement.IgnoreProcessInstanceNotExists); string activityName = base.GetStringProperty(Constants.SOProperties.ProcessInstanceManagement.ActivityName); string activitySystemName = String.Empty; bool gotoActivityResult = false; WorkflowManagementServer mngServer = this.ServiceBroker.K2Connection.GetConnection <WorkflowManagementServer>(); using (mngServer.Connection) { ProcessInstanceCriteriaFilter filter = new ProcessInstanceCriteriaFilter(); filter.AddRegularFilter(ProcessInstanceFields.ProcInstID, Comparison.Equals, processInstanceId); ProcessInstances procInsts = mngServer.GetProcessInstancesAll(filter); if (!ignoreProcessInstanceNotExists & procInsts.Count == 0) { throw new ApplicationException(String.Format(Resources.ProcessInstanceNotFound, processInstanceId)); } if (procInsts.Count > 0) { int procId = procInsts[0].ProcID; processInstanceId = procInsts[0].ID; Activities procActivities = mngServer.GetProcActivities(procId); foreach (Activity act in procActivities) { if (act.DisplayName == activityName || act.Name == activityName) { activitySystemName = act.Name; } } if (string.IsNullOrEmpty(activitySystemName)) { throw new ApplicationException(String.Format(Resources.RequiredPropertyNotFound, activityName)); } gotoActivityResult = mngServer.GotoActivity(processInstanceId, activitySystemName); } } if (results != null) { DataRow row = results.NewRow(); row[Constants.SOProperties.ProcessInstanceManagement.ActivityName] = activityName + " (" + activitySystemName + ")"; row[Constants.SOProperties.ProcessInstanceManagement.ProcessInstanceId] = processInstanceId; row[Constants.SOProperties.ProcessInstanceManagement.GoToActivityResult] = gotoActivityResult; results.Rows.Add(row); } }
private void RetryProcess() { bool newVersion = base.GetBoolProperty(Constants.SOProperties.ErrorLog.TryNewVersion); int procInstId = base.GetIntProperty(Constants.SOProperties.ErrorLog.ProcessInstanceId, true); base.ServiceBroker.Service.ServiceObjects[0].Properties.InitResultTable(); DataTable results = base.ServiceBroker.ServicePackage.ResultTable; WorkflowManagementServer mngServer = new WorkflowManagementServer(); using (mngServer.CreateConnection()) { mngServer.Open(BaseAPIConnectionString); ErrorProfile all = mngServer.GetErrorProfiles()[0]; ErrorLogCriteriaFilter errorfilter = new ErrorLogCriteriaFilter(); errorfilter.AddRegularFilter(ErrorLogFields.ProcInstID, Comparison.Equals, procInstId); ErrorLogs errors = mngServer.GetErrorLogs(all.ID, errorfilter); if (errors.Count != 1) { throw new ApplicationException(string.Format("Could not retrieve process (with id: {0}). Got {1} results.", procInstId, errors.Count)); } int errorId = errors[0].ID; if (newVersion) { int newVersionNumber = 0; ProcessInstanceCriteriaFilter procFilter = new ProcessInstanceCriteriaFilter(); procFilter.AddRegularFilter(ProcessInstanceFields.ProcInstID, Comparison.Equals, procInstId); ProcessInstances procs = mngServer.GetProcessInstancesAll(procFilter); Processes procesVersions = mngServer.GetProcessVersions(procs[0].ProcSetID); foreach (Process proc in procesVersions) { if (proc.VersionNumber > newVersionNumber) { newVersionNumber = proc.VersionNumber; } } mngServer.SetProcessInstanceVersion(procInstId, newVersionNumber); } mngServer.RetryError(procInstId, errorId, string.Format("Process Retry using {0}", base.ServiceBroker.Service.ServiceObjects[0].Name)); } }
private void GotoActivity() { int processInstanceId = base.GetIntProperty(Constants.SOProperties.ProcessInstanceManagement.ProcessInstanceId); string activityName = base.GetStringProperty(Constants.SOProperties.ProcessInstanceManagement.ActivityName); WorkflowManagementServer mngServer = this.ServiceBroker.K2Connection.GetConnection <WorkflowManagementServer>(); using (mngServer.Connection) { ProcessInstanceCriteriaFilter filter = new ProcessInstanceCriteriaFilter(); filter.AddRegularFilter(ProcessInstanceFields.ProcInstID, Comparison.Equals, processInstanceId); ProcessInstances procInsts = mngServer.GetProcessInstancesAll(filter); if (procInsts.Count == 0) { throw new ApplicationException(String.Format(Resources.ProcessInstanceNotFound, processInstanceId)); } mngServer.GotoActivity(procInsts[0].ID, activityName); } }
private void ListActivities() { int processInstanceId = base.GetIntProperty(Constants.SOProperties.ProcessInstanceManagement.ProcessInstanceId); bool includeStartActivity = base.GetBoolProperty(Constants.SOProperties.ProcessInstanceManagement.IncludeStartActivity); ServiceObject serviceObject = base.ServiceBroker.Service.ServiceObjects[0]; serviceObject.Properties.InitResultTable(); DataTable results = base.ServiceBroker.ServicePackage.ResultTable; WorkflowManagementServer mngServer = new WorkflowManagementServer(); using (mngServer.CreateConnection()) { mngServer.Open(BaseAPIConnectionString); ProcessInstanceCriteriaFilter filter = new ProcessInstanceCriteriaFilter(); filter.AddRegularFilter(ProcessInstanceFields.ProcInstID, Comparison.Equals, processInstanceId); ProcessInstances procInsts = mngServer.GetProcessInstancesAll(filter); if (procInsts.Count == 0) { throw new ApplicationException(String.Format("Sorry, process instance with id {0} not found.", processInstanceId)); } foreach (Activity actvt in mngServer.GetProcActivities(procInsts[0].ProcID)) { if (actvt.IsStart && !includeStartActivity) { continue; } DataRow row = results.NewRow(); row[Constants.SOProperties.ProcessInstanceManagement.ActivityID] = actvt.ID; row[Constants.SOProperties.ProcessInstanceManagement.ActivityName] = actvt.Name; row[Constants.SOProperties.ProcessInstanceManagement.ActivityDescription] = actvt.Description; row[Constants.SOProperties.ProcessInstanceManagement.ActivityExpectedDuration] = actvt.ExpectedDuration; row[Constants.SOProperties.ProcessInstanceManagement.IsStartActivity] = actvt.IsStart; row[Constants.SOProperties.ProcessInstanceManagement.ActivityMetaData] = actvt.MetaData; row[Constants.SOProperties.ProcessInstanceManagement.ActivityPriority] = actvt.Priority; results.Rows.Add(row); } } }
private void GotoActivity() { int processInstanceId = base.GetIntProperty(Constants.SOProperties.ProcessInstanceManagement.ProcessInstanceId); string activityName = base.GetStringProperty(Constants.SOProperties.ProcessInstanceManagement.ActivityName); WorkflowManagementServer mngServer = new WorkflowManagementServer(); using (mngServer.CreateConnection()) { mngServer.Open(BaseAPIConnectionString); ProcessInstanceCriteriaFilter filter = new ProcessInstanceCriteriaFilter(); filter.AddRegularFilter(ProcessInstanceFields.ProcInstID, Comparison.Equals, processInstanceId); ProcessInstances procInsts = mngServer.GetProcessInstancesAll(filter); if (procInsts.Count == 0) { throw new ApplicationException(String.Format("Sorry, process instance with id {0} not found.", processInstanceId)); } mngServer.GotoActivity(procInsts[0].ID, activityName); } }
//sample that shows how to manage process instances //in this sample, we want to stop all instances of a particular workflow public void ManageProcessInstances() { //establish the connection WorkflowManagementServer K2Mgmt = new WorkflowManagementServer(); K2Mgmt.CreateConnection(); K2Mgmt.Connection.Open("connectionstring"); //first, get the ID's of all the process instances of the workflow ProcessInstances procInstances = K2Mgmt.GetProcessInstancesAll("workflowfullname", "", ""); //leaving parameters blank effectively ignores that parameter foreach (ProcessInstance procInst in procInstances) { if (procInst.Status == "Active") { K2Mgmt.StopProcessInstances(procInst.ID); } } //close the connection K2Mgmt.Connection.Close(); }
private void GotoActivity() { int processInstanceId = base.GetIntProperty(Constants.SOProperties.ProcessInstanceManagement.ProcessInstanceId); string activityName = base.GetStringProperty(Constants.SOProperties.ProcessInstanceManagement.ActivityName); string activitySystemName = String.Empty; WorkflowManagementServer mngServer = this.ServiceBroker.K2Connection.GetConnection <WorkflowManagementServer>(); using (mngServer.Connection) { ProcessInstanceCriteriaFilter filter = new ProcessInstanceCriteriaFilter(); filter.AddRegularFilter(ProcessInstanceFields.ProcInstID, Comparison.Equals, processInstanceId); ProcessInstances procInsts = mngServer.GetProcessInstancesAll(filter); if (procInsts.Count == 0) { throw new ApplicationException(String.Format(Resources.ProcessInstanceNotFound, processInstanceId)); } int procId = procInsts[0].ProcID; Activities procActivities = mngServer.GetProcActivities(procId); foreach (Activity act in procActivities) { if (act.DisplayName == activityName || act.Name == activityName) { activitySystemName = act.Name; } } if (string.IsNullOrEmpty(activitySystemName)) { throw new ApplicationException(String.Format(Resources.RequiredPropertyNotFound, activityName)); } mngServer.GotoActivity(procInsts[0].ID, activitySystemName); } }
new string[] { "ProcessInstanceId", "ProcessSetId", "ProcessId", "ExecutingProcessId", "FullName", "Folio", "Originator", "Status", "ExpectedDuraction", "StartDate", "FinishDate", "Priority", "ResultStatus", "ResultMessage" })] // , "IsDefaultVersion", "VersionDate", "VersionDescription", "VersionLabel", "VersionNumber" public LIM.ProcessInstance GetProcessInstance() { WorkflowManagementServer svr = new WorkflowManagementServer("localhost", 5555); try { svr.Open(); ProcessInstances instances = null; SourceCode.Workflow.Management.Criteria.ProcessInstanceCriteriaFilter filter = new SourceCode.Workflow.Management.Criteria.ProcessInstanceCriteriaFilter(); filter.AddRegularFilter(ProcessInstanceFields.ProcInstID, SourceCode.Workflow.Management.Criteria.Comparison.Equals, this.ProcessInstanceId); instances = svr.GetProcessInstancesAll(filter); if (instances != null & instances.Count > 0) { this.ExecutingProcessId = instances[0].ExecutingProcID; this.ExpectedDuration = instances[0].ExpectedDuration; this.FinishDate = instances[0].FinishDate; this.Folio = instances[0].Folio; this.FullName = instances[0].ProcSetFullName; //this.IsDefaultVersion = instances[0].Process.DefaultVersion; //this.Name = instances[0].Process.FullName; this.Originator = instances[0].Originator; this.Priority = instances[0].Priority; this.ProcessId = instances[0].ProcID; this.ProcessInstanceId = instances[0].ID; this.ProcessSetId = instances[0].ProcSetID; this.StartDate = instances[0].StartDate; this.Status = instances[0].Status; //this.VersionDescription = instances[0].Process.VersionDesc; //this.VersionDate = instances[0].Process.VersionDate; //this.VersionLabel = instances[0].Process.VersionLabel; //this.VersionNumber = instances[0].Process.VersionNumber; } } catch (Exception ex) { this.ResultStatus = "Exception"; this.ResultMessage = ex.GetBaseException().Message; return this; } finally { try { svr.Connection.Close(); svr.Connection.Dispose(); } catch { } svr = null; } return this; }
new string[] { "ProcessInstanceId", "ProcessSetId", "ProcessId", "ExecutingProcessId", "FullName", "Name", "Folio", "Originator", "Status", "ExpectedDuraction", "StartDate", "FinishDate", "Priority" })] // , "IsDefaultVersion", "VersionDate", "VersionDescription", "VersionLabel", "VersionNumber" public List<LIM.ProcessInstance> GetRunningProcessInstanceByVersion() { List<LIM.ProcessInstance> results = new List<ProcessInstance>(); WorkflowManagementServer svr = new WorkflowManagementServer("localhost", 5555); try { svr.Open(); ProcessInstances instances = null; SourceCode.Workflow.Management.Criteria.ProcessInstanceCriteriaFilter filter = new SourceCode.Workflow.Management.Criteria.ProcessInstanceCriteriaFilter(); filter.AddRegularFilter(ProcessInstanceFields.ProcID, SourceCode.Workflow.Management.Criteria.Comparison.Equals, this.ProcessId); instances = svr.GetProcessInstancesAll(filter); for (int i = 0; i < instances.Count; i++) { LIM.ProcessInstance pi = new ProcessInstance(); pi.ExecutingProcessId = instances[i].ExecutingProcID; pi.ExpectedDuration = instances[i].ExpectedDuration; pi.FinishDate = instances[i].FinishDate; pi.Folio = instances[i].Folio; pi.FullName = instances[i].ProcSetFullName; //pi.IsDefaultVersion = instances[i].Process.DefaultVersion; //pi.Name = instances[i].Process.FullName; pi.Originator = instances[i].Originator; pi.Priority = instances[i].Priority; pi.ProcessId = instances[i].ProcID; pi.ProcessInstanceId = instances[i].ID; pi.ProcessSetId = instances[i].ProcSetID; pi.StartDate = instances[i].StartDate; pi.Status = instances[i].Status; //pi.VersionDescription = instances[i].Process.VersionDesc; //pi.VersionDate = instances[i].Process.VersionDate; //pi.VersionLabel = instances[i].Process.VersionLabel; //pi.VersionNumber = instances[i].Process.VersionNumber; results.Add(pi); } } catch (Exception ex) { //this.ResultStatus = "Exception"; //this.ResultMessage = ex.GetBaseException().Message; //return this; } finally { try { svr.Connection.Close(); svr.Connection.Dispose(); } catch { } svr = null; } return results; }
private void RetryProcess() { bool newVersion = base.GetBoolProperty(Constants.SOProperties.ErrorLog.TryNewVersion); int procInstId = base.GetIntProperty(Constants.SOProperties.ErrorLog.ProcessInstanceId, true); base.ServiceBroker.Service.ServiceObjects[0].Properties.InitResultTable(); DataTable results = base.ServiceBroker.ServicePackage.ResultTable; WorkflowManagementServer mngServer = new WorkflowManagementServer(); using (mngServer.CreateConnection()) { mngServer.Open(BaseAPIConnectionString); ErrorProfile all = mngServer.GetErrorProfiles()[0]; ErrorLogCriteriaFilter errorfilter = new ErrorLogCriteriaFilter(); errorfilter.AddRegularFilter(ErrorLogFields.ProcInstID, Comparison.Equals, procInstId); ErrorLogs errors = mngServer.GetErrorLogs(all.ID, errorfilter); if (errors.Count != 1) { throw new ApplicationException(string.Format("Could not retrieve process (with id: {0}). Got {1} results.", procInstId, errors.Count)); } int errorId = errors[0].ID; if (newVersion) { int newVersionNumber = 0; ProcessInstanceCriteriaFilter procFilter = new ProcessInstanceCriteriaFilter(); procFilter.AddRegularFilter(ProcessInstanceFields.ProcInstID, Comparison.Equals, procInstId); ProcessInstances procs = mngServer.GetProcessInstancesAll(procFilter); Processes procesVersions = mngServer.GetProcessVersions(procs[0].ProcSetID); foreach (Process proc in procesVersions) { if (proc.VersionNumber > newVersionNumber) newVersionNumber = proc.VersionNumber; } mngServer.SetProcessInstanceVersion(procInstId, newVersionNumber); } mngServer.RetryError(procInstId, errorId, string.Format("Process Retry using {0}", base.ServiceBroker.Service.ServiceObjects[0].Name)); } }
private void GotoActivity() { int processInstanceId = base.GetIntProperty(Constants.SOProperties.ProcessInstanceManagement.ProcessInstanceId); string activityName = base.GetStringProperty(Constants.SOProperties.ProcessInstanceManagement.ActivityName); WorkflowManagementServer mngServer = new WorkflowManagementServer(); using (mngServer.CreateConnection()) { mngServer.Open(BaseAPIConnectionString); ProcessInstanceCriteriaFilter filter = new ProcessInstanceCriteriaFilter(); filter.AddRegularFilter(ProcessInstanceFields.ProcInstID, Comparison.Equals, processInstanceId); ProcessInstances procInsts = mngServer.GetProcessInstancesAll(filter); if (procInsts.Count == 0) { throw new ApplicationException(String.Format("Sorry, process instance with id {0} not found.", processInstanceId)); } mngServer.GotoActivity(procInsts[0].ID, activityName); } }
private void ListActivities() { int processInstanceId = base.GetIntProperty(Constants.SOProperties.ProcessInstanceManagement.ProcessInstanceId); bool includeStartActivity = base.GetBoolProperty(Constants.SOProperties.ProcessInstanceManagement.IncludeStartActivity); ServiceObject serviceObject = base.ServiceBroker.Service.ServiceObjects[0]; serviceObject.Properties.InitResultTable(); DataTable results = base.ServiceBroker.ServicePackage.ResultTable; WorkflowManagementServer mngServer = new WorkflowManagementServer(); using (mngServer.CreateConnection()) { mngServer.Open(BaseAPIConnectionString); ProcessInstanceCriteriaFilter filter = new ProcessInstanceCriteriaFilter(); filter.AddRegularFilter(ProcessInstanceFields.ProcInstID, Comparison.Equals, processInstanceId); ProcessInstances procInsts = mngServer.GetProcessInstancesAll(filter); if (procInsts.Count == 0) { throw new ApplicationException(String.Format("Sorry, process instance with id {0} not found.", processInstanceId)); } foreach (Activity actvt in mngServer.GetProcActivities(procInsts[0].ProcID)) { if (actvt.IsStart && !includeStartActivity) continue; DataRow row = results.NewRow(); row[Constants.SOProperties.ProcessInstanceManagement.ActivityID] = actvt.ID; row[Constants.SOProperties.ProcessInstanceManagement.ActivityName] = actvt.Name; row[Constants.SOProperties.ProcessInstanceManagement.ActivityDescription] = actvt.Description; row[Constants.SOProperties.ProcessInstanceManagement.ActivityExpectedDuration] = actvt.ExpectedDuration; row[Constants.SOProperties.ProcessInstanceManagement.IsStartActivity] = actvt.IsStart; row[Constants.SOProperties.ProcessInstanceManagement.ActivityMetaData] = actvt.MetaData; row[Constants.SOProperties.ProcessInstanceManagement.ActivityPriority] = actvt.Priority; results.Rows.Add(row); } } }