public async Task <string> StartWorkflow <TData>(string workflowId, int?version, TData data = null, string reference = null) where TData : class, new() { var def = _registry.GetDefinition(workflowId, version); if (def == null) { throw new WorkflowNotRegisteredException(workflowId, version); } var wf = new WorkflowInstance { WorkflowDefinitionId = workflowId, Version = def.Version, Data = data, Description = def.Description, NextExecution = 0, CreateTime = DateTime.Now.ToUniversalTime(), Status = WorkflowStatus.Runnable, Reference = reference }; if ((def.DataType != null) && (data == null)) { if (typeof(TData) == def.DataType) { wf.Data = new TData(); } else { wf.Data = def.DataType.GetConstructor(new Type[0]).Invoke(new object[0]); } } wf.ExecutionPointers.Add(_pointerFactory.BuildGenesisPointer(def)); string id = await _persistenceStore.CreateNewWorkflow(wf); await _queueProvider.QueueWork(id, QueueType.Workflow); await _searchIndex.IndexWorkflow(wf); await _eventHub.PublishNotification(new WorkflowStarted() { EventTimeUtc = DateTime.UtcNow, Reference = reference, WorkflowInstanceId = id, WorkflowDefinitionId = def.Id, Version = def.Version }); return(id); }
protected SearchIndexTests() { Subject = CreateService(); Subject.Start().Wait(); foreach (var item in BuildTestData()) { Subject.IndexWorkflow(item).Wait(); } }
protected SearchIndexTests() { Subject = CreateService(); Subject.Start().Wait(); foreach (var item in BuildTestData()) { Subject.IndexWorkflow(item).Wait(); } System.Threading.Thread.Sleep(1000); }
protected override async Task ProcessItem(string itemId, CancellationToken cancellationToken) { try { var workflow = await FetchWorkflow(itemId); WorkflowActivity.Enrich(workflow, "index"); await _searchIndex.IndexWorkflow(workflow); lock (_errorCounts) { _errorCounts.Remove(itemId); } } catch (Exception e) { Logger.LogWarning(default(EventId), $"Error indexing workfow - {itemId} - {e.Message}"); var errCount = 0; lock (_errorCounts) { if (!_errorCounts.ContainsKey(itemId)) { _errorCounts.Add(itemId, 0); } _errorCounts[itemId]++; errCount = _errorCounts[itemId]; } if (errCount < 5) { await QueueProvider.QueueWork(itemId, Queue); return; } if (errCount < 20) { await Task.Delay(TimeSpan.FromSeconds(10)); await QueueProvider.QueueWork(itemId, Queue); return; } lock (_errorCounts) { _errorCounts.Remove(itemId); } Logger.LogError(default(EventId), e, $"Unable to index workfow - {itemId} - {e.Message}"); } }
protected override async Task ProcessItem(string itemId, CancellationToken cancellationToken) { if (await _lockProvider.AcquireLock(itemId, cancellationToken)) { WorkflowInstance workflow = null; WorkflowExecutorResult result = null; var persistenceStore = _persistenceStorePool.Get(); try { try { cancellationToken.ThrowIfCancellationRequested(); workflow = await persistenceStore.GetWorkflowInstance(itemId); if (workflow.Status == WorkflowStatus.Runnable) { var executor = _executorPool.Get(); try { result = await executor.Execute(workflow); } finally { _executorPool.Return(executor); await persistenceStore.PersistWorkflow(workflow); await _searchIndex.IndexWorkflow(workflow); } } } finally { await _lockProvider.ReleaseLock(itemId); if ((workflow != null) && (result != null)) { foreach (var sub in result.Subscriptions) { await SubscribeEvent(sub, persistenceStore); } await persistenceStore.PersistErrors(result.Errors); var readAheadTicks = _datetimeProvider.Now.Add(Options.PollInterval).ToUniversalTime().Ticks; if ((workflow.Status == WorkflowStatus.Runnable) && workflow.NextExecution.HasValue && workflow.NextExecution.Value < readAheadTicks) { new Task(() => FutureQueue(workflow, cancellationToken)).Start(); } } } } finally { _persistenceStorePool.Return(persistenceStore); } } else { Logger.LogInformation("Workflow locked {0}", itemId); } }