Ejemplo n.º 1
0
 public static void Send(ICommand cmd, string tokenSession = "", bool asyncExec = true)
 {
     if (tokenSession != "")
     {
         cmd.TokenSession = tokenSession;
     }
     CommandsAndEventsRegisterEngine.PushCommand(cmd, asyncExec);
 }
 public static void Send(ICommand cmd)
 {
     if (cmd.PublishedCommandId == null || cmd.PublishedCommandId == Guid.Empty)
     {
         cmd.PublishedCommandId = Guid.NewGuid();
     }
     CommandsAndEventsRegisterEngine.PushCommand(cmd, RedisServices.IsEnable);
     Console.WriteLine("Sent");
     Console.WriteLine(JsonConvert.SerializeObject(cmd));
 }
 public static void Send(IEvent evt)
 {
     if (evt.PublishedEventId == null || evt.PublishedEventId == Guid.Empty)
     {
         evt.PublishedEventId = Guid.NewGuid();
     }
     CommandsAndEventsRegisterEngine.PushEvent(evt, RedisServices.IsEnable);
     Console.WriteLine("Sent");
     Console.WriteLine(JsonConvert.SerializeObject(evt));
 }
Ejemplo n.º 4
0
        public static void Subscribe(string typeEvent, Action <object> handle, string subscriberName)
        {
            if (RedisServices.IsEnable == false)
            {
                Console.WriteLine("Should use redis for pubsub function");
                return;
            }

            var channel = BuildRedisChannelName(typeEvent);

            if (RedisServices.RedisDatabase.HashGet(channel, subscriberName).HasValue)
            {
                Unsubscribe(typeEvent, subscriberName);
                //return;
            }

            RedisServices.RedisDatabase.HashSet(channel, subscriberName, subscriberName);

            var topic = BuildRedisTopicName(channel, subscriberName);

            Redis.RedisServices.RedisSubscriber.Subscribe(topic, (c, evtJson) =>
            {
                try
                {
                    var typeRegistered = CommandsAndEventsRegisterEngine.FindTypeOfCommandOrEvent(typeEvent);

                    if (evtJson.HasValue)
                    {
                        var evt = JsonConvert.DeserializeObject(evtJson, typeRegistered) as IEvent;
                        if (evt != null)
                        {
                            handle(evt);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error subscriber {subscriberName} at channel {c} {ex.Message}");
                }
            });

            Console.WriteLine($"Subscribe for topic: {topic}");
        }
Ejemplo n.º 5
0
        public CommandResponse Post(CommandRequest cmd)
        {
            try
            {
                var jobj = JsonConvert.DeserializeObject(cmd.CommandDataJson) as Newtonsoft.Json.Linq.JObject;

                var objectType = CommandsAndEventsRegisterEngine.FindTypeOfCommandOrEvent(cmd.CommandTypeFullName);
                if (objectType == null ||
                    jobj == null)
                {
                    return(new CommandResponse()
                    {
                        Success = false,
                        StatusCode = HttpStatusCode.NotImplemented,
                        Message = "Not found command type",
                        CommandId = Guid.Empty
                    });
                }

                var ocmd = (ICommand)jobj.ToObject(objectType);

                CommandEventSender.Send(ocmd);

                return(new CommandResponse()
                {
                    Success = true,
                    CommandId = ocmd.PublishedCommandId.Value,
                    Message = "Success",
                    StatusCode = HttpStatusCode.OK
                });
            }
            catch (Exception ex)
            {
                return(new CommandResponse()
                {
                    CommandId = Guid.Empty,
                    Message = ex.GetAllMessages(),
                    StatusCode = HttpStatusCode.BadGateway,
                    Success = false
                });
            }
        }
Ejemplo n.º 6
0
        static void WorkerDo(string type)
        {
            while (true)
            {
                try
                {
                    while (_stopWorker.ContainsKey(type) == false || _stopWorker[type] == false)
                    {
                        try
                        {
                            if (!CommandsAndEventsRegisterEngine.EventWorkerCanDequeue(type))
                            {
                                continue;
                            }
                            if (RedisServices.IsEnable)
                            {
                                var queueName      = BuildRedisQueueName(type);
                                var typeRegistered = CommandsAndEventsRegisterEngine.FindTypeOfCommandOrEvent(type);
                                var evtJson        = RedisServices.RedisDatabase
                                                     .ListRightPop(queueName);
                                if (evtJson.HasValue)
                                {
                                    var evt = JsonConvert.DeserializeObject(evtJson, typeRegistered) as IEvent;
                                    if (evt != null)
                                    {
                                        try
                                        {
                                            CommandsAndEventsRegisterEngine.ExecEvent(evt);
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.WriteLine(ex.Message);
                                            RedisServices.RedisDatabase
                                            .ListLeftPush(queueName, evtJson);
                                        }
                                    }
                                    else
                                    {
                                        RedisServices.RedisDatabase
                                        .ListLeftPush(queueName, evtJson);
                                    }
                                }
                            }
                            else
                            {
                                if (_cmdDataQueue.TryGetValue(type, out ConcurrentQueue <IEvent> cmdQueue) &&
                                    cmdQueue != null)
                                {
                                    //in-memory queue, can be use redis queue, rabitmq ...
                                    if (cmdQueue.TryDequeue(out IEvent evt) && evt != null)
                                    {
                                        CommandsAndEventsRegisterEngine.ExecEvent(evt);
                                    }
                                }
                            }
                        }
                        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);
                }
            }
        }
Ejemplo n.º 7
0
        static void WorkerDo(string type)
        {
            while (true)
            {
                try
                {
                    while (_stopWorker.ContainsKey(type) == false || _stopWorker[type] == false)
                    {
                        try
                        {
                            if (!CommandsAndEventsRegisterEngine.EventWorkerCanDequeue(type))
                            {
                                Thread.Sleep(100);
                                continue;
                            }
                            if (RedisServices.IsEnable)
                            {
                                var channel = BuildRedisChannelName(type);

                                var allSubscribe = RedisServices.RedisDatabase.HashGetAll(channel);

                                if (allSubscribe.Count() <= 0)
                                {
                                    Console.WriteLine("No consummer to process event data");
                                    Thread.Sleep(100);
                                    continue;
                                }

                                var queueName      = BuildRedisQueueName(type);
                                var typeRegistered = CommandsAndEventsRegisterEngine.FindTypeOfCommandOrEvent(type);
                                var evtJson        = RedisServices.RedisDatabase.ListRightPop(queueName);
                                if (evtJson.HasValue)
                                {
                                    try
                                    {
                                        var evt = JsonConvert.DeserializeObject(evtJson, typeRegistered) as IEvent;
                                        if (evt != null)
                                        {
                                            foreach (var subscriber in allSubscribe)
                                            {
                                                var topic = BuildRedisTopicName(channel, subscriber.Name);

                                                RedisServices.RedisSubscriber.Publish(topic, evtJson);
                                            }
                                        }
                                        else
                                        {
                                            RedisServices.RedisDatabase.ListLeftPush(queueName, evtJson);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        RedisServices.RedisDatabase.ListLeftPush(queueName, evtJson);

                                        Console.WriteLine(ex.Message);
                                    }
                                }
                            }
                            else
                            {
                                if (_evtDataQueue.TryGetValue(type, out ConcurrentQueue <IEvent> evtQueue) &&
                                    evtQueue != null)
                                {
                                    //in-memory queue, can be use redis queue, rabitmq ...
                                    if (evtQueue.TryDequeue(out IEvent evt) && evt != null)
                                    {
                                        CommandsAndEventsRegisterEngine.ExecEvent(evt);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        finally
                        {
                            Thread.Sleep(100);
                        }
                    }

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

                        lock (_locker)
                        {
                            if (_evtWorker.TryGetValue(type, out List <Thread> listThread))
                            {
                                if (listThread.Count == counter)
                                {
                                    _workerStoped[type]        = true;
                                    _workerCounterStoped[type] = 0;
                                }
                            }
                        }
                    }
                }
                finally
                {
                    Thread.Sleep(100);
                }
            }
        }
Ejemplo n.º 8
0
 public static void Send(IEvent evt, bool asyncExec = true)
 {
     CommandsAndEventsRegisterEngine.PushEvent(evt, asyncExec);
 }