protected async Task DeleteItemIfAsync(IMessage message, Func <T, bool> predicate) { async Task DoAsync() { var lockId = GetLockId(message); await LockProvider.TryUsingAsync(lockId, async token => { var item = await LoadItemAsync(message).ConfigureAwait(false); if (item.Entity == null) { return; } if (!item.IsNew) { if (predicate(item.Entity)) { await DeleteAsync(item.Id, message, CreateIdentifierItem).ConfigureAwait(false); } } }, cancellationToken : CancellationToken.None).ConfigureAwait(false); } await LockProvider.TryUsingAsync(GetCacheLockId(), async() => await DoAsync().ConfigureAwait(false), timeUntilExpires : null, cancellationToken : CancellationToken.None).ConfigureAwait(false); }
/// <inheritdoc/> protected override SqlProvider VisitLock(LockProvider provider) { var source = Compile(provider.Source); var query = source.Request.Statement.ShallowClone(); switch (provider.LockMode.Invoke()) { case LockMode.Shared: query.Lock = SqlLockType.Shared; break; case LockMode.Exclusive: query.Lock = SqlLockType.Exclusive; break; case LockMode.Update: query.Lock = SqlLockType.Update; break; } switch (provider.LockBehavior.Invoke()) { case LockBehavior.Wait: break; case LockBehavior.ThrowIfLocked: query.Lock |= SqlLockType.ThrowIfLocked; break; case LockBehavior.Skip: query.Lock |= SqlLockType.SkipLocked; break; } return(CreateProvider(query, provider, source)); }
public async Task <bool> SuspendWorkflow(string workflowId) { if (await LockProvider.AcquireLock(workflowId, new CancellationToken())) { try { var wf = await PersistenceStore.GetWorkflowInstance(workflowId); if (wf.Status == WorkflowStatus.Runnable) { wf.Status = WorkflowStatus.Suspended; await PersistenceStore.PersistWorkflow(wf); return(true); } return(false); } finally { await LockProvider.ReleaseLock(workflowId); } } return(false); }
protected async Task LoadAndSaveOnlyExistingAsync(IMessage message, Action <T> setValues = null) { var lockId = GetLockId(message); await LockProvider.TryUsingAsync(lockId, async token => { var item = await LoadItemAsync(message).ConfigureAwait(false); var entity = item.Entity; if (item.IsNew) { Logger.LogTrace("Skipping new Item"); } else { if (entity == null) { Logger.LogTrace("No Valid Entity Loaded"); } else { setValues?.Invoke(entity); // await SaveItemAsync(item, message, state); await SaveItemAsync(item, message).ConfigureAwait(false); } } }, cancellationToken : CancellationToken.None).ConfigureAwait(false); }
public async Task <bool> ResumeWorkflow(string workflowId) { if (await LockProvider.AcquireLock(workflowId, new CancellationToken())) { bool requeue = false; try { var wf = await PersistenceStore.GetWorkflowInstance(workflowId); if (wf.Status == WorkflowStatus.Suspended) { wf.Status = WorkflowStatus.Runnable; await PersistenceStore.PersistWorkflow(wf); requeue = true; return(true); } return(false); } finally { await LockProvider.ReleaseLock(workflowId); if (requeue) { await QueueProvider.QueueWork(workflowId, QueueType.Workflow); } } } return(false); }
internal Locker(LockProvider provider, string key, object keyLock) { this.provider = provider; this.key = key; this.keyLock = keyLock; }
/// <summary> /// Worker thread body /// </summary> private void RunWorkflows() { IWorkflowExecutor workflowExecutor = _serviceProvider.GetService <IWorkflowExecutor>(); IPersistenceProvider persistenceStore = _serviceProvider.GetService <IPersistenceProvider>(); while (!_shutdown) { try { var workflowId = QueueProvider.DequeueForProcessing().Result; if (workflowId != null) { try { if (LockProvider.AcquireLock(workflowId).Result) { WorkflowInstance workflow = null; try { workflow = persistenceStore.GetWorkflowInstance(workflowId).Result; if (workflow.Status == WorkflowStatus.Runnable) { workflowExecutor.Execute(workflow, persistenceStore, Options); } } finally { LockProvider.ReleaseLock(workflowId).Wait(); if (workflow != null) { if ((workflow.Status == WorkflowStatus.Runnable) && workflow.NextExecution.HasValue && workflow.NextExecution.Value < DateTime.Now.ToUniversalTime().Ticks) { QueueProvider.QueueForProcessing(workflowId); } } } } else { Logger.LogInformation("Workflow locked {0}", workflowId); } } catch (Exception ex) { Logger.LogError(ex.Message); } } else { Thread.Sleep(Options.IdleTime); //no work } } catch (Exception ex) { Logger.LogError(ex.Message); } } }
private PersonalDataDB(IDataProvider dataProvider) { this.dataProvider = dataProvider; this.lockProvider = new LockProvider(); this.Administrator = new AdministratorService(dataProvider, lockProvider); this.User = new UserService(dataProvider, lockProvider); this.Owner = new OwnerService(dataProvider, lockProvider); }
/// <summary> /// Poll the persistence store for workflows ready to run. /// Poll the persistence store for stashed unpublished events /// </summary> private void PollRunnables(object target) { try { if (LockProvider.AcquireLock("poll runnables").Result) { try { Logger.LogInformation("Polling for runnable workflows"); IPersistenceProvider persistenceStore = _serviceProvider.GetService <IPersistenceProvider>(); var runnables = persistenceStore.GetRunnableInstances().Result; foreach (var item in runnables) { Logger.LogDebug("Got runnable instance {0}", item); QueueProvider.QueueForProcessing(item); } } finally { LockProvider.ReleaseLock("poll runnables").Wait(); } } } catch (Exception ex) { Logger.LogError(ex.Message); } try { if (LockProvider.AcquireLock("unpublished events").Result) { try { Logger.LogInformation("Polling for unpublished events"); IPersistenceProvider persistenceStore = _serviceProvider.GetService <IPersistenceProvider>(); var events = persistenceStore.GetUnpublishedEvents().Result.ToList(); foreach (var item in events) { Logger.LogDebug("Got unpublished event {0} {1}", item.EventName, item.EventKey); QueueProvider.QueueForPublishing(item).Wait(); persistenceStore.RemoveUnpublishedEvent(item.Id).Wait(); } } finally { LockProvider.ReleaseLock("unpublished events").Wait(); } } } catch (Exception ex) { Logger.LogError(ex.Message); } }
private void addBindings(IUnityContainer container) { configureNHibernate(container); container.RegisterType <IRepository, NHibernateSimpleRepository>(new PerThreadLifetimeManager()); container.RegisterType(typeof(IRuleLogicExecutionService <>), typeof(RuleLogicExecutionService <>), new TransientLifetimeManager()); container.RegisterType(typeof(IRuleQueryExecuter <>), typeof(RuleQueryExecuter <>), new TransientLifetimeManager()); container.RegisterType(typeof(ICache <,>), typeof(MultipleStopwatchCache <,>), "MultipleStopwatchCache", new ContainerControlledLifetimeManager()); container.RegisterType(typeof(ICache <,>), typeof(SingleStopwatchCache <,>), "SingleStopwatchCache", new ContainerControlledLifetimeManager()); container.RegisterType <IRuleExecutionElementFactory <EFileDocument>, EFileCERuleExecutionElementFactory>(new TransientLifetimeManager()); container.RegisterType <Func <FieldService> >(new InjectionFactory(_container => new Func <FieldService>(() => _container.Resolve <FieldService>()))); container.RegisterType <Func <RuleService> >(new InjectionFactory(_container => new Func <RuleService>(() => _container.Resolve <RuleService>()))); container.RegisterType <IRuleEvaluationRequestsConsumer, RuleEvaluationRequestsConsumer>(new TransientLifetimeManager()); container.RegisterType <IRuleEvaluationRequestsProducer, RuleEvaluationRequestsDatabaseProducer>(new TransientLifetimeManager()); container.RegisterType <IEvaluationRequestsService, EvaluationRequestDBService>(); container.RegisterType <IValuesDictionary, ValuesDictionary>(new PerThreadLifetimeManager()); container.RegisterType <Func <IEvaluationRequestsService> >( new InjectionFactory(c => new Func <IEvaluationRequestsService>(() => c.Resolve <IEvaluationRequestsService>()) )); container.RegisterType <Func <RuleServiceInstance> >( new InjectionFactory(c => new Func <RuleServiceInstance>(() => c.Resolve <RuleServiceInstance>()) )); container.RegisterType <Func <IRuleLogicExecutionService <EFileDocument> > >( new InjectionFactory(c => new Func <IRuleLogicExecutionService <EFileDocument> >(() => c.Resolve <IRuleLogicExecutionService <EFileDocument> >()) )); container.RegisterType <Func <IValuesDictionary> >( new InjectionFactory(c => new Func <IValuesDictionary>(() => c.Resolve <IValuesDictionary>()) )); container.RegisterType <ConcurrentDictionary <Tuple <string, string, string, string>, Stopwatch> >(new ContainerControlledLifetimeManager()); //container.RegisterType<Common.Logging.ILoggerFactory, Common.Logging.Log4NetLoggerFactory>(); int queueSize = int.Parse(ConfigurationManager.AppSettings["MaxQueueSize"]); BlockingCollection <RuleEvaluationRequest> blockingCollection = new BlockingCollection <RuleEvaluationRequest>(queueSize); container.RegisterInstance <BlockingCollection <RuleEvaluationRequest> >(blockingCollection); WorkQueueCounter counter = new WorkQueueCounter(); container.RegisterInstance <WorkQueueCounter>(counter); PerformanceMetricsCollector _PerformanceMetricsCollector = new PerformanceMetricsCollector(); container.RegisterInstance <PerformanceMetricsCollector>(_PerformanceMetricsCollector); Processor processor = new Processor(); container.RegisterInstance <Processor>(processor); ILockProvider lockProvider = new LockProvider(); container.RegisterInstance <ILockProvider>(lockProvider); }
public void Start() { _shutdown = false; PersistenceStore.EnsureStoreExists(); QueueProvider.Start().Wait(); LockProvider.Start().Wait(); Logger.LogInformation("Starting backgroud tasks"); foreach (var task in _backgroundTasks) { task.Start(); } }
public void Stop() { _shutdown = true; Logger.LogInformation("Stopping background tasks"); foreach (var th in _backgroundTasks) { th.Stop(); } Logger.LogInformation("Worker tasks stopped"); QueueProvider.Stop(); LockProvider.Stop(); }
public void Stop() { _shutdown = true; Logger.LogInformation("Stopping worker threads"); foreach (var th in _workers) { th.Stop(); } _workers.Clear(); Logger.LogInformation("Worker threads stopped"); QueueProvider.Stop(); LockProvider.Stop(); }
public void Stop() { _shutdown = true; Logger.LogInformation("Stopping background tasks"); foreach (var th in _backgroundTasks) { th.Stop(); } Logger.LogInformation("Worker tasks stopped"); QueueProvider.Stop().Wait(); LockProvider.Stop().Wait(); _searchIndex.Stop().Wait(); _lifeCycleEventHub.Stop().Wait(); }
private async Task SaveItemAsync(EntityDescriptor <T> item, IMessage domainEvent) { async Task DoAsync() { if (Context.IsLiveProcessing) { await SaveAsync(item, domainEvent, CreateIdentifierItem(item.Entity)).ConfigureAwait(false); } else { await SaveAsync(item, domainEvent, null).ConfigureAwait(false); } await Cache.RemoveByPrefixAsync(EntityTypeKey).ConfigureAwait(false); } await LockProvider.TryUsingAsync(GetCacheLockId(), async() => await DoAsync().ConfigureAwait(false), timeUntilExpires : null, cancellationToken : CancellationToken.None).ConfigureAwait(false); }
public async Task StopAsync(CancellationToken cancellationToken) { _shutdown = true; Logger.LogInformation("Stopping background tasks"); if (_backgroundTasks is not null) { foreach (var th in _backgroundTasks) { if (th is not null) { try { th.Stop(); } catch (Exception e) { Logger.LogError(e, "Exception thrown during tasks disposal"); } } } } // throw new Exception( // @$"Nuggets 1 //_backgroundTasks is {_backgroundTasks is null} //Logger is {Logger is null} //QueueProvider is {QueueProvider is null} //LockProvider is {LockProvider is null} //_searchIndex is {_searchIndex is null} //_lifeCycleEventHub is {_lifeCycleEventHub is null} //" // ); Logger.LogInformation("Worker tasks stopped"); await QueueProvider.Stop(); await LockProvider.Stop(); await _searchIndex.Stop(); await _lifeCycleEventHub.Stop(); }
public async Task StartAsync(CancellationToken cancellationToken) { _shutdown = false; PersistenceStore.EnsureStoreExists(); await QueueProvider.Start(); await LockProvider.Start(); await _lifeCycleEventHub.Start(); await _searchIndex.Start(); Logger.LogInformation("Starting background tasks"); foreach (var task in _backgroundTasks) { task.Start(); } }
public async Task <bool> TerminateWorkflow(string workflowId) { if (LockProvider.AcquireLock(workflowId).Result) { try { var wf = await PersistenceStore.GetWorkflowInstance(workflowId); wf.Status = WorkflowStatus.Terminated; await PersistenceStore.PersistWorkflow(wf); return(true); } finally { await LockProvider.ReleaseLock(workflowId); } } return(false); }
public async Task StopAsync(CancellationToken cancellationToken) { _shutdown = true; Logger.LogInformation("Stopping background tasks"); foreach (var th in _backgroundTasks) { th.Stop(); } Logger.LogInformation("Worker tasks stopped"); await QueueProvider.Stop(); await LockProvider.Stop(); await _searchIndex.Stop(); await _lifeCycleEventHub.Stop(); }
public void Start() { _shutdown = false; QueueProvider.Start(); LockProvider.Start(); for (int i = 0; i < Options.ThreadCount; i++) { Logger.LogInformation("Starting worker thread #{0}", i); Thread thread = new Thread(RunWorkflows); _threads.Add(thread); thread.Start(); } Logger.LogInformation("Starting publish thread"); Thread pubThread = new Thread(RunPublications); _threads.Add(pubThread); pubThread.Start(); _pollTimer = new Timer(new TimerCallback(PollRunnables), null, TimeSpan.FromSeconds(0), Options.PollInterval); }
public void Stop() { _shutdown = true; if (_pollTimer != null) { _pollTimer.Dispose(); _pollTimer = null; } var stashTask = StashUnpublishedEvents(); Logger.LogInformation("Stopping worker threads"); foreach (Thread th in _threads) { th.Join(); } Logger.LogInformation("Worker threads stopped"); stashTask.Wait(); QueueProvider.Stop(); LockProvider.Stop(); }
protected async Task DeleteItemAsync(IMessage message) { async Task DoAsync() { var item = await LoadItemAsync(message).ConfigureAwait(false); if (item.Entity == null) { return; } if (!item.IsNew) { await base.DeleteAsync(item.Id, message, CreateIdentifierItem).ConfigureAwait(false); //,state: state); } } await LockProvider.TryUsingAsync(GetCacheLockId(), async() => await DoAsync().ConfigureAwait(false), timeUntilExpires : null, cancellationToken : CancellationToken.None).ConfigureAwait(false); }
public async Task StartAsync(CancellationToken cancellationToken) { _shutdown = false; PersistenceStore.EnsureStoreExists(); await QueueProvider.Start(); await LockProvider.Start(); await _lifeCycleEventHub.Start(); await _searchIndex.Start(); // Event subscriptions are removed when stopping the event hub. // Add them when starting. AddEventSubscriptions(); Logger.LogInformation("Starting background tasks"); foreach (var task in _backgroundTasks) { task.Start(); } }
public async Task StartAsync(CancellationToken cancellationToken) { var activity = WorkflowActivity.StartHost(); try { _shutdown = false; PersistenceStore.EnsureStoreExists(); await QueueProvider.Start(); await LockProvider.Start(); await _lifeCycleEventHub.Start(); await _searchIndex.Start(); // Event subscriptions are removed when stopping the event hub. // Add them when starting. AddEventSubscriptions(); Logger.LogInformation("Starting background tasks"); foreach (var task in _backgroundTasks) { task.Start(); } } catch (Exception ex) { activity.RecordException(ex); throw; } finally { activity?.Dispose(); } }
public async Task HandleAsync(FakeEvent message, AggregateTypeInfo <FakeAggregate> info) { Logger.LogInformation("Handle FakeEvent {AggregateId}", message.AggregateId); var tasksToRun = new List <Task>(); await LockProvider.TryUsingAsync(message.AggregateId, async token => { var agg = await Repository.GetByIdAsync <FakeAggregate>(message.AggregateId); agg.DoFakeEvent2("zweite runde 2"); //await Repository.SaveAsync(agg, CancellationToken); var evnts = await Repository.SaveAsyncWithOutPush(agg, CancellationToken); foreach (var evnt in evnts) { tasksToRun.Add(Bus.PublishAsync(evnt)); } }, cancellationToken : CancellationToken); await Task.WhenAll(tasksToRun); }
public Locker(LockProvider provider, string key) { this.provider = provider; this.key = key; }
private void RunPublications() { IPersistenceProvider persistenceStore = _serviceProvider.GetService <IPersistenceProvider>(); while (!_shutdown) { try { var pub = QueueProvider.DequeueForPublishing().Result; if (pub != null) { try { if (LockProvider.AcquireLock(pub.WorkflowId).Result) { try { var workflow = persistenceStore.GetWorkflowInstance(pub.WorkflowId).Result; var pointers = workflow.ExecutionPointers.Where(p => p.EventName == pub.EventName && p.EventKey == p.EventKey && !p.EventPublished); foreach (var p in pointers) { p.EventData = pub.EventData; p.EventPublished = true; p.Active = true; } workflow.NextExecution = 0; persistenceStore.PersistWorkflow(workflow); } catch (Exception ex) { Logger.LogError(ex.Message); persistenceStore.CreateUnpublishedEvent(pub); //retry later } finally { LockProvider.ReleaseLock(pub.WorkflowId).Wait(); QueueProvider.QueueForProcessing(pub.WorkflowId); } } else { Logger.LogInformation("Workflow locked {0}", pub.WorkflowId); persistenceStore.CreateUnpublishedEvent(pub); //retry later } } catch (Exception ex) { Logger.LogError(ex.Message); } } else { Thread.Sleep(Options.IdleTime); //no work } } catch (Exception ex) { Logger.LogError(ex.Message); } } }
public void SetUp() { lockCheck = new object(); p = new LockProvider(); testFailed = false; }
public void SetCommandLockProvider(LockProvider cmdLockProvider) { _commandLockProvider = cmdLockProvider; }
/// <summary> /// Compiles <see cref="LockProvider"/>. /// </summary> /// <param name="provider">Lock provider.</param> protected abstract TResult VisitLock(LockProvider provider);