Esempio n. 1
0
 private static void LoopTryExecEvent()
 {
     while (!_cmdStoped)
     {
         try
         {
             _cmdDone = false;
             ICommand cmd;
             var      batch = new List <ICommand>();
             while (_cmdQueue.TryDequeue(out cmd) && cmd != null)
             {
                 batch.Add(cmd);
             }
             if (batch.Count > 0)
             {
                 foreach (var c in batch)
                 {
                     //try
                     //{
                     //    MemoryMessageBuss.ExecCommand(c);
                     //}
                     //catch (Exception e)
                     //{
                     //    //Consider handle error, error may enqueue or log to other db to process late
                     //    //var cmd = JsonConvert.SerializeObject(cmd)
                     //    //_cmdQueue.Enqueue(c);
                     //}
                     MemoryMessageBuss.ExecCommand(c);
                 }
             }
             _cmdDone = true;
         }
         catch (Exception ex)
         {
             _cmdDone = true;
             throw new Exception("Error when dequeue " + ex);
         }
         finally
         {
             Thread.Sleep(1);
         }
     }
 }
Esempio n. 2
0
        static void WorkerDo(Type type)
        {
            while (true)
            {
                try
                {
                    while (_stopWorker.ContainsKey(type) == false || _stopWorker[type] == false)
                    {
                        try
                        {
                            if (RedisServices.IsEnable)
                            {
                                var queueName = BuildRedisQueueName(type);

                                var cmdJson = RedisServices.RedisDatabase
                                              .ListRightPop(queueName);
                                if (cmdJson.HasValue)
                                {
                                    var cmd = JsonConvert.DeserializeObject(cmdJson, type) as ICommand;
                                    if (cmd != null)
                                    {
                                        MemoryMessageBuss.ExecCommand(cmd);
                                    }
                                }
                            }
                            else
                            {
                                if (_cmdDataQueue.TryGetValue(type, out ConcurrentQueue <ICommand> cmdQueue) &&
                                    cmdQueue != null)
                                {
                                    //in-memory queue, can be use redis queue, rabitmq ...
                                    if (cmdQueue.TryDequeue(out ICommand cmd) && cmd != null)
                                    {
                                        MemoryMessageBuss.ExecCommand(cmd);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        finally
                        {
                            Thread.Sleep(0);
                        }
                    }

                    if (!_workerCounterStoped.ContainsKey(type))
                    {
                        _workerCounterStoped[type] = 0;
                    }
                    if (_workerStoped[type] == false)
                    {
                        var counter = _workerCounterStoped[type];
                        counter++;
                        _workerCounterStoped[type] = counter;

                        lock (_locker)
                        {
                            if (_cmdWorker.TryGetValue(type, out List <Thread> listThread))
                            {
                                if (listThread.Count == counter)
                                {
                                    _workerStoped[type]        = true;
                                    _workerCounterStoped[type] = 0;
                                }
                            }
                        }
                    }
                }
                finally
                {
                    Thread.Sleep(100);
                }
            }
        }