/// <summary>
        /// statistic event override
        /// </summary>
        /// <param name="event"></param>
        public void Write(StatisticEvent <int> @event)
        {
            if (@event.Metric != StatisticType.WorkersLog)
            {
                return;
            }

            lock (_pipelineId)
            {
                var metric = new ActiveWorkersLogMessage
                {
                    Timestamp     = DateTime.Now,
                    PipelineId    = _pipelineId,
                    ActiveWorkers = @event.Value
                };

                _storage.Value.AddToList(new StorageKey(_pipelineId, $"log:{@event.Metric}"), metric);

                if (_dispatcherLock.IsLocked())
                {
                    return;
                }

                _dispatcherLock.Lock();

                if (_taskDispatcher == null)
                {
                    _taskDispatcher = new BackgroundTaskDispatcher(new StorageContext(_storage.Value));
                }

                _taskDispatcher.StartNew(new ActiveWorkersLogCleanupTask(_dispatcherLock, new StorageKey(_pipelineId, $"log:{@event.Metric}")));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Start processing events
        /// </summary>
        public void Start()
        {
            if (_lock.IsLocked())
            {
                return;
            }

            _lock.Lock();

            _logger.Write($"Start working on Thread {_id}", Category.Log, LogLevel.Debug, "EventBus");

            Task = Task.Factory.StartNew(() =>
            {
                try
                {
                    _worker.Execute();
                }
                catch (Exception e)
                {
                    _logger.Write(e.Message, Category.Log, LogLevel.Error, "EventProcessor");
                }
                finally
                {
                    _continuation();
                    _lock.Unlock();
                }
            }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
        }
Beispiel #3
0
        public void ActiveWorkersLogCleanupTask_Execute_Unlock()
        {
            var locker = new DispatcherLock();

            locker.Lock();
            var task = new ActiveWorkersLogCleanupTask(locker, new StorageKey("test"));

            task.Execute(new StorageContext(new Mock <IStorage>().Object));

            Assert.IsFalse(locker.IsLocked());
        }
Beispiel #4
0
        /// <summary>
        /// Waits for all processors to end the work
        /// </summary>
        public void WaitAll()
        {
            while (_cleanupLock.IsLocked())
            {
                System.Diagnostics.Trace.WriteLine("Wait for processors to cleanup");
                WaitOne(50);
            }

            var cnt = 0;

            while (_queue.Count > 0)
            {
                var queueSize = _queue.Count;
                System.Diagnostics.Trace.WriteLine($"Wait for {queueSize} Events in the queue to be processed");
                WaitOne(500);
                if (cnt > 5)
                {
                    break;
                }

                // we only wait for max 5 turns if the queue size does not change
                // that could mean that there are no porcessors that are working on the queue
                cnt = _queue.Count == queueSize ? cnt + 1 : 0;
            }

            var tasks = _processors.GetTasks();

            while (tasks.Any())
            {
                System.Diagnostics.Trace.WriteLine($"Wait for {_queue.Count} Events to be processed");
                Task.WaitAll(tasks, 1000, CancellationToken.None);
                tasks = _processors.GetTasks();
            }

            CleanupProcessors();
        }