Example #1
0
        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);
        }
Example #2
0
        /// <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));
        }
Example #3
0
        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);
        }
Example #4
0
        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);
        }
Example #5
0
        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;
 }
Example #7
0
        /// <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);
                }
            }
        }
Example #8
0
        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);
        }
Example #9
0
        /// <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);
            }
        }
Example #10
0
        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);
        }
Example #11
0
        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();
            }
        }
Example #12
0
        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();
        }
Example #13
0
        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();
        }
Example #14
0
        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();
        }
Example #15
0
        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);
        }
Example #16
0
        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();
            }
        }
Example #18
0
        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);
        }
Example #19
0
        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();
        }
Example #20
0
        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);
        }
Example #21
0
        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();
        }
Example #22
0
        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);
        }
Example #23
0
        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();
            }
        }
Example #25
0
        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);
        }
Example #26
0
 public Locker(LockProvider provider, string key)
 {
     this.provider = provider;
     this.key      = key;
 }
Example #27
0
        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);
                }
            }
        }
Example #28
0
 public void SetUp()
 {
     lockCheck = new object();
     p = new LockProvider();
     testFailed = false;
 }
Example #29
0
 public void SetUp()
 {
     lockCheck  = new object();
     p          = new LockProvider();
     testFailed = false;
 }
Example #30
0
 public void SetCommandLockProvider(LockProvider cmdLockProvider)
 {
     _commandLockProvider = cmdLockProvider;
 }
Example #31
0
 /// <summary>
 /// Compiles <see cref="LockProvider"/>.
 /// </summary>
 /// <param name="provider">Lock provider.</param>
 protected abstract TResult VisitLock(LockProvider provider);