예제 #1
0
        public void RegisterWorkflow(IWorkflow workflow)
        {
            if (_registry.Any(x => x.Item1 == workflow.Id && x.Item2 == workflow.Version))
            {
                throw new InvalidOperationException($"Workflow {workflow.Id} version {workflow.Version} is already registered");
            }

            var builder = _serviceProvider.GetService <IWorkflowBuilder>().UseData <object>();

            workflow.Build(builder);
            var def = builder.Build(workflow.Id, workflow.Version);

            _registry.Add(Tuple.Create(workflow.Id, workflow.Version, def));
        }
예제 #2
0
        private void QueueUpdateManual(IArkUpdateableContext context, string type, string key)
        {
            if (_updateQueue.Any(x => x.Item1 == context))
            {
                return;
            }

            if (_currentContext == context)
            {
                _currentCts?.Cancel();
            }

            _progress.Report($"{type} ({key}): Update queued manually ({DateTime.Now:HH:mm:ss.ffff})");
            _updateQueue.Add(new Tuple <IArkUpdateableContext, bool>(context, true));
        }
예제 #3
0
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }

            lock (_disposingContext)
            {
                if (_disposed)
                {
                    return;
                }

                _disposed = true;
            }

            _dmaOperations.CompleteAdding();

            var timeout = Task.Delay(Timeout * 10);

            while (_dmaOperations.Any())
            {
                var iteration     = Task.Delay(100);
                var completedTask = Task.WhenAny(timeout, iteration).Result;

                if (completedTask == timeout)
                {
                    throw new Exception("Cannot dispose");
                }
            }

            _dmaOperations.Dispose();
        }
 public static void Clear <T>(this BlockingCollection <T> collection)
 {
     while (collection.Any())
     {
         collection.TryTake(out _);
     }
 }
예제 #5
0
        public T Map <T>(DbDataReader reader, T entity)
        {
            if (reader is null)
            {
                throw new ArgumentNullException(nameof(reader));
            }
            if (entity is null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            Type entityType = typeof(T);

            if (SqlTypeMap.Types.ContainsKey(entityType))
            {
                return((T)reader[0]);
            }

            if (cachedSchemas.Any(schema => schema.EntityType == entityType))
            {
                return(MapFromCache(reader, entity));
            }

            cachedSchemas.Add(CreateSchema(entityType));

            return(MapFromCache(reader, entity));
        }
예제 #6
0
        public bool IsPrimeParallel_StorePrimes(int i)
        {
            if (i == 1)
            {
                return(false);
            }
            if (i == 2 || i == 3)
            {
                return(true);
            }

            if (_primeBlockingCollection.Any(p => i % p == 0))
            {
                return(true);
            }

            int  limit   = (int)Math.Sqrt(i) + 1;
            bool isPrime = false;

            Parallel.For(5, limit, (j, loopState) =>
            {
                if (i % j == 0)
                {
                    loopState.Stop();
                    isPrime = true;
                }
            });
            if (isPrime)
            {
                _primeBlockingCollection.Add(i);
            }
            return(isPrime);
        }
예제 #7
0
 /// <summary>
 /// Takes the next item from the queue. If no item is queued, the Thread blocks until a item is available
 /// </summary>
 /// <returns></returns>
 public T Take()
 {
     lock (takeItemLock)
     {
         while (true)                
         {
             if (highPriorityItems.Any())
             {
                 lock(countLock)
                     return highPriorityItems.Take();
             }
             else if (normalPriorityItems.Any())
             {
                 lock (countLock)
                     return normalPriorityItems.Take();
             }
             else if (lowPriorityItems.Any())
             {
                 lock (countLock)
                     return lowPriorityItems.Take();
             }
             else
             {
                 itemWaitHandle.WaitOne();
             }
         }
     }
 }
예제 #8
0
        /// <summary>
        /// Сохраняет в БД результаты парсинга
        /// </summary>
        /// <param name="resourceItemsBc">resourceItemsBc</param>
        /// <param name="releasesBc">releasesBc</param>
        private void SaveResults(BlockingCollection <ResourceItemEntity> resourceItemsBc,
                                 BlockingCollection <AlbumInfoRelease> releasesBc)
        {
            if (!resourceItemsBc.Any() && !releasesBc.Any())
            {
                throw new AlbumInfoReleaseException(Helper.EmptyReleaseException);
            }

            var resourceItems = resourceItemsBc.ToList();

            foreach (var resourceItem in resourceItems)
            {
                //находим соответствующий релиз
                var release = releasesBc.FirstOrDefault(r => r.ResourceInternalId == resourceItem.ResourceInternalId);

                if (release != null)
                {
                    var music   = MapAlbumInfoReleaseToMusic(release);
                    var musicId = SaveRelease(music);
                    resourceItem.MusicId = musicId;

                    SaveMusicTrack(release.TrackList, musicId);

                    SaveResourceItem(resourceItem);
                }
                else
                {
                    throw new AlbumInfoReleaseException(
                              string.Format(Helper.ParsReleaseException, resourceItem.ResourceInternalId));
                }
            }
        }
 private void EnqueueIfNotExists(string partitionKey)
 {
     if (!_enqueuedKeys.Any(partitionKey.Equals))
     {
         // if the key is not already in the queue, add it. No need to add it if it's already there, as
         // when the partition is processed, it will already try to send all events.
         _enqueuedKeys.Add(partitionKey);
     }
 }
예제 #10
0
        public void Add(T tm, Func <T, T, bool> compare)
        {
            var isActiveItemOk = (typeof(T).IsValueType ? default(T).Equals(_activeItem) : _activeItem == null) || !compare(tm, _activeItem);

            if (!LoadRatesQueue.Any(t => compare(t, tm)) && isActiveItemOk)
            {
                AddCore(tm);
            }
        }
예제 #11
0
        public void ProcessQueue_Performs_Operation_On_All_Elements_From_InQueue_To_OutQueue()
        {
            new ZippingPool(_inQueue, _outQueue, _nullifyingProcess)
            .ProcessQueue(Config.ThreadsCount);

            var hasNonEmptyChunks = _outQueue.Any(c => c.Content.Length > 0);

            Assert.False(hasNonEmptyChunks);
        }
예제 #12
0
        void EnqueueEvent(IEvent e)
        {
            if (uncommittedEvents.Any(item => item.GetType() == e.GetType()))
            {
                throw new ArgumentException($"无法重复添加类型相同的事件。 [AggregateRootType = {GetType()}, AggregateRootId = {Id}, EventType = {e.GetType()}]", nameof(e));
            }

            uncommittedEvents.TryAdd(e);
        }
예제 #13
0
파일: Log.cs 프로젝트: nerai/Nibbler
 private static void WriteThread()
 {
     for (; ;)
     {
         var task  = _WriteQueue.Take();
         var flush = !_WriteQueue.Any();
         DoWrite(task.S, flush);
         task.Done.Set();
     }
 }
예제 #14
0
        public static async Task GetRandomFights([NotNull] Player player, [NotNull] Player opp, [NotNull] RefreshBB refresh, int builds = 100, int positions = 200, int cores = 8)
        {
            if (Running == true)
            {
                return;
            }

            Running            = true;
            _refreshBB         = refresh;
            _refreshBB.Bplayer = player.GetString();
            _refreshBB.Bopp    = opp.GetString();

            source   = new CancellationTokenSource();
            token    = source.Token;
            _empty   = new ManualResetEvent(false);
            CORES    = cores;
            MaxValue = 0;

            BUILDS                = builds;
            POSITIONS             = positions;
            _refreshBB.TOTAL_DONE = 0;
            _refreshBB.TOTAL      = positions;

            START = DateTime.UtcNow;
            END   = DateTime.MinValue;

            _jobs_random = new BlockingCollection <int>();

            for (int i = 0; i < 40000; i++)
            {
                _jobs_random.Add(i);
            }

            for (int i = 0; i < 8; i++)
            {
                Thread thread = new Thread(OnHandlerStartRandom)
                {
                    IsBackground = true
                };                      //Mark 'false' if you want to prevent program exit until jobs finish
                thread.Start();
            }

            while (!_empty.WaitOne(1000))
            {
                Console.WriteLine(_jobs_random.Count());
                _refreshBB.Update = !_refreshBB.Update;
                if (!_jobs_random.Any())
                {
                    break;
                }
            }
            END               = DateTime.UtcNow;
            Running           = false;
            _refreshBB.Update = !_refreshBB.Update;
        }
예제 #15
0
파일: EventQueue.cs 프로젝트: azalac/sq3ms
 /// <summary>
 /// Waits until the given event is handled, if it is already queued.
 /// </summary>
 /// <param name="e"></param>
 private void WaitUntilHandled(IEventExecutor e)
 {
     // if the event is queued, wait until it is handled
     if (event_queue.Any(evt => e.Equals(evt)))
     {
         lock (evt_locks[e])
         {
             Monitor.Wait(evt_locks[e]);
         }
     }
 }
예제 #16
0
 public Job Dequeue(CancellationToken token)
 {
     if (_tasks.Any())
     {
         return(_tasks.Take(token));
     }
     else
     {
         return(null);
     }
 }
예제 #17
0
 public void PollNow()
 {
     if (!_isStarted)
     {
         return;
     }
     if (!_commandList.Any(c => c.Command == QueueCommands.Poll))
     {
         _commandList.Add(CommandData.Poll());
     }
 }
예제 #18
0
        private void DrainActivities <T>(ConcurrentQueue <T> currentActivites, BlockingCollection <T> activitiesToProcess)
        {
            currentActivites.Clear();

            if (activitiesToProcess.Any())
            {
                while (activitiesToProcess.TryTake(out T item))
                {
                    currentActivites.Enqueue(item);
                }
            }
        }
예제 #19
0
        public Task StopAsync()
        {
            StopPublishing();
            StopMaintainingConnection();

            while (_messageQueue.Any())
            {
                _messageQueue.Take();
            }

            return(Task.FromResult(0));
        }
예제 #20
0
        public Task StopAsync()
        {
            _connectionCancellationToken?.Cancel(false);
            _connectionCancellationToken = null;

            while (_messageQueue.Any())
            {
                _messageQueue.Take();
            }

            return(Task.FromResult(0));
        }
예제 #21
0
        private Task StartRoleTask(CancellationToken token,
                                   OnChangeActions onChangeActions,
                                   BlockingCollection <ClientEvent> clientEvents)
        {
            return(Task.Run(async() =>
            {
                while (!token.IsCancellationRequested && !clientEvents.IsAddingCompleted)
                {
                    try
                    {
                        // take the most recent event, if multiple are queued up then we only need the latest
                        ClientEvent clientEvent = null;
                        while (clientEvents.Any())
                        {
                            try
                            {
                                clientEvent = clientEvents.Take(token);
                            }
                            catch (OperationCanceledException) { }
                        }

                        // if there was an event then call the appropriate role beahvaiour
                        if (clientEvent != null)
                        {
                            if (clientEvent.EventType == EventType.Coordinator)
                            {
                                //await this.coordinator.ExecuteCoordinatorRoleAsync(this.clientId,
                                //        clientEvent,
                                //        onChangeActions,
                                //        token);
                            }
                            else
                            {
                                //await this.follower.ExecuteFollowerRoleAsync(this.clientId,
                                //        clientEvent,
                                //        onChangeActions,
                                //        token);
                            }
                        }
                        else
                        {
                            await WaitFor(TimeSpan.FromSeconds(1), token);
                        }
                    }
                    catch (Exception ex)
                    {
                        this.logger.Error(ex);
                        await WaitFor(TimeSpan.FromSeconds(1), token);
                    }
                }
            }));
        }
예제 #22
0
        public IEnumerable <JournalRecord> GetRecords(long fromRecord = 0)
        {
            using (var queue = new BlockingCollection <JournalRecord>())
            {
                async Task MessageReceived(IStreamSubscription subscription, StreamMessage message,
                                           CancellationToken cancellationToken)
                {
                    var json = await message.GetJsonData(cancellationToken);

                    var command       = (Command)_serializer.FromString(json);
                    var journalRecord = new JournalRecord(message.StreamVersion, message.CreatedUtc, command);

                    queue.Add(journalRecord);
                }

                // pass null to subscribe from the beginning
                //or the version of the previous record
                int?version = null;
                if (fromRecord > 0)
                {
                    version = (int)fromRecord - 1;
                }

                var caughtUp = false;

                using (
                    var sub = _streamStore.SubscribeToStream(
                        _streamId,
                        version,
                        MessageReceived,
                        SubscriptionDropped,
                        hasCaughtUp => caughtUp = hasCaughtUp))
                {
                    sub.MaxCountPerRead = 100;

                    while (!caughtUp || queue.Any())
                    {
                        if (queue.TryTake(out var journalRecord))
                        {
                            yield return(journalRecord);
                        }
                        else if (!caughtUp)
                        {
                            Thread.Sleep(100);
                        }
                    }
                }
            }
        }
예제 #23
0
        public void Run()
        {
            while (_exception == null && _sheduled.Any(x => !x.Value))
            {
                _sheduled.Where(x => !x.Value && _ready(x.Key)).ForEach(x => Enqueue(x.Key));
                _wake.WaitOne();
            }

            while (_exception == null && _tasks.Any())
            {
                _wake.WaitOne();
            }

            Cancel();
        }
        public void Dispose()
        {
            _source?.Cancel();

            if (_queue != null)
            {
                _queue.CompleteAdding();
                while (_queue.Any())
                {
                    _queue.TryTake(out _);
                }

                _queue.Dispose();
            }
        }
예제 #25
0
        private void GenerateChunk(CancellationToken token)
        {
            var chunkGenerator = _chunkGeneratorFactory.Invoke();

            while (!token.IsCancellationRequested || _generateQueue.Any())
            {
                if (!_generateQueue.TryTake(out var chunkSize, 1000))
                {
                    continue;
                }

                var chunk = chunkGenerator.GenerateNext(chunkSize);
                _writeQueue.Add(chunk);
            }
        }
예제 #26
0
        private void ProcessTasks()
        {
            Status = WorkflowStatus.Running;

            ProduceQueue();

            var sleepTime = 1;

            while (Status != WorkflowStatus.Completed && Status != WorkflowStatus.Error)
            {
                bool threadStart;
                int  threadCount;
                lock (_lockThreadCounter)
                {
                    threadCount = _threadCount;
                    threadStart = _threadCount < MaxThreads && _queue.Any();
                    if (threadStart)
                    {
                        _threadCount++;
                    }
                }

                if (threadStart)
                {
                    Task.Factory.StartNew(() =>
                    {
                        var task = _queue.GetConsumingEnumerable().First();
                        task.Run(_state);
                        while (task.Status != WorkflowStatus.Completed && task.Status != WorkflowStatus.Error)
                        {
                            Thread.Sleep(50);
                        }
                        lock (_lockThreadCounter)
                            _threadCount--;
                    });
                }

                if (_tasks.All(t => t.Status == WorkflowStatus.Completed))
                {
                    Status = WorkflowStatus.Completed;
                }

                lock (_lockThreadCounter)
                    sleepTime = threadCount == _threadCount ? sleepTime + 1 : 1;

                Thread.Sleep(sleepTime);
            }
        }
예제 #27
0
        static void Main(string[] args)
        {
            Console.Title = "Statistics";

            var producerCts = new CancellationTokenSource();

            var oneSecondGroupMessages = new BlockingCollection <DecodedMessage>();
            var oneSecondProducer      = new Producer();

            oneSecondProducer.StartProducing(producerCts.Token, TimeSpan.FromSeconds(1), "one-second-stats", oneSecondGroupMessages);

            var thirtySecondGroupMessages = new BlockingCollection <DecodedMessage>();
            var thirtySecondProducer      = new Producer();

            thirtySecondProducer.StartProducing(producerCts.Token, TimeSpan.FromSeconds(30), "thirty-second-stats", thirtySecondGroupMessages);

            var consumerCts       = new CancellationTokenSource();
            var oneSecondConsumer = new Consumer();

            oneSecondConsumer.StartConsuming(consumerCts.Token, "one-second-group", oneSecondGroupMessages);

            var thirtySecondConsumer = new Consumer();

            thirtySecondConsumer.StartConsuming(consumerCts.Token, "thirty-second-group", thirtySecondGroupMessages);

            Console.WriteLine("Press any key to shutdown");
            Console.ReadKey();

            consumerCts.Cancel();

            // perform a shutdown, allowing for 30 seconds to process current items
            // this is just an example, it could end with message loss
            var sw = new Stopwatch();

            sw.Start();
            while (oneSecondGroupMessages.Any())
            {
                if (sw.ElapsedMilliseconds > 60000)
                {
                    producerCts.Cancel(); // force close (could lose messages)
                }
                else
                {
                    Thread.Sleep(100);
                }
            }
        }
예제 #28
0
        public void Stop()
        {
            if (_messagesWaitingForPersistence.Any())
            {
                _logger.WarnFormat("Stopping PersistenceTransport with messages waiting for persistence to come back online!");
            }

            _innerTransport.Stop();

            _pendingReceives.CompleteAdding();
            if (_receptionThread != null && !_receptionThread.Join(30.Second()))
            {
                _logger.WarnFormat("Unable to stop reception thread");
            }

            SetInitialPhase();
        }
예제 #29
0
        private void WriteToFile(CancellationToken token)
        {
            var fileName = _fileNameProvider.GetPath();

            Logger.Info("File name is {fileName}", fileName);

            using var writer = _fileWriterFactory.Invoke(fileName);

            while (!token.IsCancellationRequested || _writeQueue.Any())
            {
                if (!_writeQueue.TryTake(out var nextChunk, 1000))
                {
                    continue;
                }
                writer.Write(nextChunk);
            }
        }
예제 #30
0
        public static BlockingCollection <T> Move <T>(this BlockingCollection <T> values, int sourceIndex, int targetIndex)
        {
            if (values == null || !values.Any())
            {
                return(values);
            }

            var list = values.ToList();

            list.Move(sourceIndex, targetIndex);

            while (values.TryTake(out _))
            {
            }
            list.ForEach(values.Add);
            return(values);
        }
예제 #31
0
 public static async Task LoadImageNames(ObservableCollection<string> resultCollection)
 {
     var buffer = new BlockingCollection<string>();
     buffer.Add("turtles");
     foreach (var image in buffer.GetConsumingEnumerable())
     {
         var nextBatch = await LoadNextImages(resultCollection, image);
         foreach (var nextImage in nextBatch)
         {
             buffer.Add(nextImage);
         }
         if (!buffer.Any())
         {
             buffer.CompleteAdding();
         }
     }
     //await LoadNextImages(resultCollection, new[] { "turtles" });
 }
 private bool UpdatePackageInCommandAlreadyQueued(Command command, BlockingCollection<Command> commandQueue)
 {
     return commandQueue.Any(a => a.PackageName == command.PackageName);
 }