/// <summary> /// Message came in to create a new workflow. /// </summary> private void NewWorkflowInstance(object msg, CancellationToken ct, IMessageAcknowledge ack) { var wfc = (WorkflowCreate)msg; Debug.Assert(!string.IsNullOrEmpty(wfc.TemplateName) || !string.IsNullOrEmpty(wfc.TemplateContent)); try { _log.InfoFormat("Hosting new workflow instance of type {0}", wfc.TemplateName); string templateData = !string.IsNullOrEmpty(wfc.TemplateContent) ? wfc.TemplateContent : LoadWorkflowTemplate(wfc.TemplateName); var agent = new WorkflowAgent(this, wfc.Id, _ct, wfc.InitialData, templateData, _workflowDataRepositoryKey, _workflowMessagingKey, _workflowWorkspaceKey); _agents.TryAdd(wfc.Id, agent); agent.Start(); ack.MessageAcknowledged(); } catch (Exception ex) { var es = string.Format("Problem while trying to host new workflow instance for type {0}: {1}", wfc.TemplateName, ex); _log.Error(es); var on = Catalog.Factory.Resolve <IApplicationAlert>(); on.RaiseAlert(ApplicationAlertKind.Unknown, es); if (_agents.ContainsKey(wfc.Id)) { WorkflowAgent _; _agents.TryRemove(wfc.Id, out _); } ack.MessageRejected(); } }
/// <summary> /// Try to lock and host a pre-existing workflow. /// </summary> /// <param name="workflowId"> </param> /// <returns> </returns> private WorkflowAgent TryAcquireWorkflow(string workflowId) { WorkflowAgent acquired = null; var wfMutex = Catalog.Preconfigure() .Add(DistributedMutexLocalConfig.Name, WorkflowShared.WorkflowInstanceLockName(workflowId)) .ConfiguredResolve <IDistributedMutex>(); if (wfMutex.Open()) { try { var wftemp = GetWorkflowTemplate(workflowId); if (!string.IsNullOrWhiteSpace(wftemp)) { acquired = WorkflowAgent.AcquireSleepingOnLocked(wfMutex, this, workflowId, _ct, wftemp, _workflowDataRepositoryKey, _workflowMessagingKey, _workflowWorkspaceKey); if (null != acquired) { if (_agents.TryAdd(workflowId, acquired)) { acquired.Start(); } else { _dblog.WarnFormat("Strange: locked workflow {0} but couldn't add it", workflowId); } } } } catch (SynchronizationLockException) { _dblog.InfoFormat("Synchronization lock exception while trying to acquire {0}", workflowId); wfMutex.Release(); } catch (WorkflowTemplateException wfex) { var es = string.Format("Error loading template for workflow! {0}: {1}", workflowId, wfex); _log.Error(es); var on = Catalog.Factory.Resolve <IApplicationAlert>(); on.RaiseAlert(ApplicationAlertKind.Unknown, es); wfMutex.Release(); } catch (Exception ex) { var es = string.Format("Error while acquiring workflow, {0}", ex); _log.Error(es); var on = Catalog.Factory.Resolve <IApplicationAlert>(); on.RaiseAlert(ApplicationAlertKind.Unknown, es); wfMutex.Release(); } } else { _dblog.InfoFormat("Could not lock {0}", workflowId); } return(acquired); }