public async Task SubscribeEvent(string workflowId, int stepId, string eventName, string eventKey) { _logger.LogDebug("Subscribing to event {0} {1} for workflow {2} step {3}", eventName, eventKey, workflowId, stepId); EventSubscription subscription = new EventSubscription(); subscription.WorkflowId = workflowId; subscription.StepId = stepId; subscription.EventName = eventName; subscription.EventKey = eventKey; await _persistenceStore.CreateEventSubscription(subscription); }
private async Task SubscribeEvent(EventSubscription subscription, IPersistenceProvider persistenceStore, CancellationToken cancellationToken) { //TODO: move to own class Logger.LogDebug("Subscribing to event {0} {1} for workflow {2} step {3}", subscription.EventName, subscription.EventKey, subscription.WorkflowId, subscription.StepId); await persistenceStore.CreateEventSubscription(subscription, cancellationToken); if (subscription.EventName != Event.EventTypeActivity) { var events = await persistenceStore.GetEvents(subscription.EventName, subscription.EventKey, subscription.SubscribeAsOf, cancellationToken); foreach (var evt in events) { var eventKey = $"evt:{evt}"; bool acquiredLock = false; try { acquiredLock = await _lockProvider.AcquireLock(eventKey, cancellationToken); int attempt = 0; while (!acquiredLock && attempt < 10) { await Task.Delay(Options.IdleTime, cancellationToken); acquiredLock = await _lockProvider.AcquireLock(eventKey, cancellationToken); attempt++; } if (!acquiredLock) { Logger.LogWarning($"Failed to lock {evt}"); } else { _greylist.Remove(eventKey); await persistenceStore.MarkEventUnprocessed(evt, cancellationToken); await QueueProvider.QueueWork(evt, QueueType.Event); } } finally { if (acquiredLock) { await _lockProvider.ReleaseLock(eventKey); } } } } }
private async Task SubscribeEvent(EventSubscription subscription, IPersistenceProvider persistenceStore) { //TODO: move to own class Logger.LogDebug("Subscribing to event {0} {1} for workflow {2} step {3}", subscription.EventName, subscription.EventKey, subscription.WorkflowId, subscription.StepId); await persistenceStore.CreateEventSubscription(subscription); var events = await persistenceStore.GetEvents(subscription.EventName, subscription.EventKey, subscription.SubscribeAsOf); foreach (var evt in events) { await persistenceStore.MarkEventUnprocessed(evt); await QueueProvider.QueueWork(evt, QueueType.Event); } }