/// <summary> /// Set actor (add or replace) /// </summary> /// <param name="registration">actor registration</param> /// <returns>task</returns> public async Task Set(IWorkContext context, IActorRegistration registration) { registration.Verify(nameof(registration)).IsNotNull(); context = context.With(_tag); context.Telemetry.Verbose(context, $"Setting actor {registration.ActorKey}"); IActorRegistration?currentActorRegistration = null; var key = new RegistrationKey(registration.ActorType, registration.ActorKey.Key); lock (_lock) { if (!_actorCache.TryRemove(key, out currentActorRegistration)) { currentActorRegistration = null; } _actorCache.Set(key, registration); } if (currentActorRegistration != null) { await currentActorRegistration.Instance !.Deactivate(context).ConfigureAwait(false); currentActorRegistration.Instance.Dispose(); } await registration.Instance !.Activate(context).ConfigureAwait(false); }
/// <summary> /// Set actor (add or replace) /// </summary> /// <param name="registration">actor registration</param> /// <returns>task</returns> public async Task SetAsync(IWorkContext context, IActorRegistration registration) { Verify.IsNotNull(nameof(registration), registration); context = context.WithTag(_tag); context.EventLog.Verbose(context.WithTag(_tag), $"Setting actor {registration.ActorKey}"); IActorRegistration currentActorRegistration = null; var key = new RegistrationKey(registration.ActorType, registration.ActorKey.Key); lock (_lock) { if (!_actors.TryRemove(key, out currentActorRegistration)) { currentActorRegistration = null; } _actors.Set(key, registration); } if (currentActorRegistration != null) { await currentActorRegistration.Instance.DeactivateAsync(context); currentActorRegistration.Instance.Dispose(); } await registration.Instance.ActivateAsync(context); }
/// <summary> /// Loop through remove queue and remove actors /// </summary> /// <returns>task</returns> private async Task RetireActor(IActorRegistration actorRegistration) { var key = new RegistrationKey(actorRegistration.ActorType, actorRegistration.ActorKey.Key); _actorCache.Remove(key); await Remove(_workContext, actorRegistration.ActorType, actorRegistration.ActorKey).ConfigureAwait(false); }
/// <summary> /// Deactivate actor /// </summary> /// <typeparam name="T">actor interface</typeparam> /// <param name="context">context</param> /// <param name="actorKey">actor key</param> /// <returns>true if deactivated, false if not found</returns> public async Task <bool> DeactivateAsync <T>(IWorkContext context, ActorKey actorKey) { Verify.Assert(IsRunning, _disposedTestText); Verify.IsNotNull(nameof(context), context); Verify.IsNotNull(nameof(actorKey), actorKey); IActorRegistration actorRegistration = await _actorRepository.RemoveAsync(context, typeof(T), actorKey); if (actorRegistration == null) { return(false); } return(true); }
/// <summary> /// Create virtual actor, return current instance or create one /// </summary> /// <typeparam name="T">actor interface</typeparam> /// <param name="context">context</param> /// <param name="actorKey">actor key</param> /// <returns>actor proxy interface</returns> public async Task <T> CreateProxyAsync <T>(IWorkContext context, ActorKey actorKey) where T : IActor { Verify.Assert(IsRunning, _disposedTestText); Verify.IsNotNull(nameof(context), context); Verify.IsNotNull(nameof(context), context); Verify.IsNotNull(nameof(actorKey), actorKey); context = context.WithTag(_tag); Type actorType = typeof(T); // Lookup instance of actor (type + actorKey) IActorRegistration actorRegistration = _actorRepository.Lookup(actorType, actorKey); if (actorRegistration != null) { return(actorRegistration.GetInstance <T>()); } // Create actor IActor actorObject = _typeManager.Create <T>(context, actorKey, this); IActorBase actorBase = actorObject as IActorBase; if (actorBase == null) { var ex = new ArgumentException($"Actor {actorObject.GetType().FullName} does not implement IActorBase"); Configuration.WorkContext.EventLog.Error(context, "Cannot create", ex); throw ex; } T actorInterface = ActorProxy <T> .Create(context, actorBase, this); actorRegistration = new ActorRegistration(typeof(T), actorKey, actorBase, actorInterface); await _actorRepository.SetAsync(context, actorRegistration); // Create proxy for interface return(actorRegistration.GetInstance <T>()); }