public async Task <object> GetActor <T>(string actorid) { object controller; if (mActorsCollection.TryGetValue(typeof(T), out ActorCollection actors)) { var item = actors.Get(actorid); if (item == null) { item = new ActorCollection.ActorItem(); item.ActorID = actorid; item.Actor = CreateController(actors.ServiceType); item.ServiceName = actors.ServiceName; item.Interface = actors.InterfaceType; item.TimeOut = EventCenter.Watch.ElapsedMilliseconds + ActorFreeTime * 1000; item = actors.Set(actorid, item, out bool add); controller = item.Actor; await item.Initialize(); string actorPath = $"/{actors.ServiceName}/{actorid}"; if (controller is IActorState state) { state.ActorID = actorid; state.ActorPath = actorPath; state.EventCenter = this; if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"{actors.ServiceType.Name}@{actorPath} actor initialized"); } return(state); } } } return(null); }
public EventActionHandlerContext(EventCenter server, IEventInput input, EventActionHandler handler, object controller, NextQueue nextQueue, ActorCollection.ActorItem actorItem) { Input = input; EventCenter = server; Handler = handler; Controller = controller; NextQueue = nextQueue; ActorItem = actorItem; }
private async void OnExecute(IEventInput input, EventOutput output, EventActionHandler handler, IEventCompleted callBackEvent) { try { string actorID = null; input.Properties?.TryGetValue(ACTOR_TAG, out actorID); string actorPath = null; object controller = null; NextQueue nextQueue = null; ActorCollection.ActorItem item = null; if (string.IsNullOrEmpty(actorID)) { nextQueue = this.InputNextQueue.Next(this.NextQueueWaits); if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"[{input.ID}]{input.Token} Process event {input.EventPath}"); } controller = handler.Controller; if (handler.ThreadType == ThreadType.SingleQueue) { nextQueue = handler.GetNextQueue(input.Data); } if (!handler.SingleInstance) { controller = CreateController(handler.ControllerType); } } else { item = handler.Actors.Get(actorID); if (item == null) { actorPath = "/" + handler.ServiceName + "/" + actorID; if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"[{input.ID}]{input.Token} {handler.ControllerType.Name}@{actorPath} create actor"); } item = new ActorCollection.ActorItem(); item.ActorID = actorID; item.Actor = CreateController(handler.Actors.ServiceType); item.ServiceName = handler.ServiceName; item.Interface = handler.Interface; item.TimeOut = EventCenter.Watch.ElapsedMilliseconds + ActorFreeTime * 1000; item = handler.Actors.Set(actorID, item, out bool add); controller = item.Actor; if (controller is IActorState state) { state.ActorID = actorID; state.ActorPath = actorPath; state.EventCenter = this; if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"[{input.ID}]{input.Token} {handler.ControllerType.Name}@{actorPath} actor initialized"); } } } else { item.TimeOut = EventCenter.Watch.ElapsedMilliseconds + ActorFreeTime * 1000; controller = item.Actor; } nextQueue = item.NextQueue; if (EnabledLog(LogType.Debug)) { Log(LogType.Debug, $"[{input.ID}]{input.Token} Process event {input.EventPath} in /{item.ServiceName}/{item.ActorID} actor"); } await item.Initialize(); } EventActionHandlerContext context = new EventActionHandlerContext(this, input, handler, controller, nextQueue, item); context.Execute(output, callBackEvent); } catch (Exception e_) { output.EventError = EventError.InnerError; output.Data = new object[] { $"Process event {input.EventPath} error {e_.Message}" }; if (EnabledLog(LogType.Error)) { Log(LogType.Error, $"[{input.ID}]{input.Token} process event {input.EventPath} error {e_.Message}@{e_.StackTrace}"); } callBackEvent.Completed(output); } }