/// <summary> /// Remove actor from container /// </summary> /// <param name="actorType">actor type</param> /// <param name="actorKey">actor key</param> /// <returns>actor registration or null if not exist</returns> public async Task <IActorRegistration?> Remove(IWorkContext context, Type actorType, ActorKey actorKey) { actorType.Verify(nameof(actorType)).IsNotNull(); actorKey.Verify(nameof(actorKey)).IsNotNull(); context = context.With(_tag); context.Telemetry.Verbose(context.With(_tag), $"Removing actor {actorKey}"); IActorRegistration registration; lock (_lock) { var key = new RegistrationKey(actorType, actorKey.Key); if (!_actorCache.TryRemove(key, out registration)) { return(null); } } try { await registration.Instance.Deactivate(context).ConfigureAwait(false); } finally { registration.Instance.Dispose(); } return(registration); }
/// <summary> /// Register type based /// </summary> /// <param name="context">context</param> /// <param name="actorTypeRegistration">actor type registration</param> /// <returns></returns> public ActorTypeManager Register(IWorkContext context, ActorTypeRegistration actorTypeRegistration) { context.Verify(nameof(context)).IsNotNull(); actorTypeRegistration.Verify(nameof(actorTypeRegistration)).IsNotNull(); actorTypeRegistration.InterfaceType.IsInterface.Verify().Assert(x => x = true, $"{actorTypeRegistration.InterfaceType.FullName} must be an interface"); context = context.With(_tag); _actorRegistration.AddOrUpdate(actorTypeRegistration.InterfaceType, actorTypeRegistration, (_, __) => actorTypeRegistration); context.Telemetry.Verbose(context.With(_tag), $"lambda registered for type:{actorTypeRegistration.InterfaceType.Name}"); return(this); }
/// <summary> /// Register actor for lambda creation /// </summary> /// <typeparam name="T">actor interface</typeparam> /// <param name="context">context</param> /// <param name="createImplementation">creation lambda</param> /// <returns>this</returns> public ActorTypeManager Register <T>(IWorkContext context, Func <IWorkContext, T> createImplementation) where T : IActor { context.Verify(nameof(context)).IsNotNull(); typeof(T).IsInterface.Verify().Assert(x => x = true, $"{typeof(T).FullName} must be an interface"); context = context.With(_tag); IActor create(IWorkContext c) => createImplementation(c); _actorRegistration.AddOrUpdate(typeof(T), x => new ActorTypeRegistration(x, create), (x, r) => r); context.Telemetry.Verbose(context.With(_tag), $"lambda registered for type:{typeof(T)}"); return(this); }
/// <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); }
public Task CloseAsync(IWorkContext context) { context = context.With(_tag); context.Telemetry.Verbose(context, $"Closing event hub client for {_conntectionString.EntityPath}"); return(_client.CloseAsync()); }
/// <summary> /// Test state of items /// </summary> /// <param name="context">work context</param> /// <returns>true if all pass, false if not</returns> public async Task <bool> Test(IWorkContext context) { context.Verify(nameof(context)).IsNotNull(); context = context.With(_tag); try { context.Telemetry.Verbose(context, "Running state plan"); foreach (var item in StateItems) { context.Telemetry.Verbose(context, $"Executing state plan 'Test' for item {item.Name}"); bool result = await item.Test(context).ConfigureAwait(false); context.CancellationToken.ThrowIfCancellationRequested(); context.Telemetry.Verbose(context, $"Executed state plan 'Test' for item {item.Name} with {result} result"); if (!result) { return(false); } } context.Telemetry.Verbose(context, "State plan completed successfully"); return(true); } finally { context.Telemetry.Verbose(context, "State plan exited"); } }
/// <summary> /// Find certificate by thumbprint. Certificates that have expired will not /// be returned and if "throwOnNotFound" is specified, an exception will be /// thrown. /// </summary> /// <param name="tag">tag</param> /// <param name="context">work context</param> /// <param name="throwOnNotFound">if true, throw exception if not found</param> /// <exception cref="ProgramExitException">Certificate is not found</exception> /// <returns>X509 certificate</returns> /// <exception cref="CertificateNotFoundException">when certificate valid certificate was not found</exception> public X509Certificate2 GetCertificate(IWorkContext context, bool?throwOnNotFound = null) { context = context.With(_tag); X509Certificate2 certificate; Exception?saveException = null; throwOnNotFound = throwOnNotFound ?? LocalCertificateKey.RequirePrivateKey; lock (_lock) { if (_cachedCertificate.TryGetValue(out certificate)) { return(certificate); } using (X509Store store = new X509Store(LocalCertificateKey.StoreName, LocalCertificateKey.StoreLocation)) { context.Telemetry.Verbose(context, $"Looking for certificate for {this}"); try { store.Open(OpenFlags.ReadOnly); X509Certificate2Collection certificateList = store.Certificates.Find(X509FindType.FindByThumbprint, LocalCertificateKey.Thumbprint, validOnly: false); if (certificateList?.Count != 0) { _cachedCertificate.Set( certificateList .OfType <X509Certificate2>() .Where(x => !LocalCertificateKey.RequirePrivateKey || x.HasPrivateKey) .Where(x => DateTime.Now <= x.NotAfter) .FirstOrDefault() ); } } catch (Exception ex) { context.Telemetry.Warning(context, $"Exception: {ex}"); _cachedCertificate.Clear(); saveException = ex; } } context.Telemetry.Verbose(context, $"{(_cachedCertificate != null ? "Found" : "Not found")} certificate for {this}"); if (!_cachedCertificate !.TryGetValue(out certificate) && throwOnNotFound == true) { throw new CertificateNotFoundException($"Certificate not found: {LocalCertificateKey.ToString()}"); } return(certificate); } }
public void WorkWithPropertyTest() { IWorkContext context = WorkContext.Empty; IWorkContext nextContext = context.With("Key", "Value"); context.Properties.ContainsKey("Key").Should().BeFalse(); TestContextProperties(context); nextContext.Properties.ContainsKey("Key").Should().BeTrue(); TestContextProperties(nextContext); }
/// <summary> /// Create actor from either lambda or activator /// </summary> /// <typeparam name="T">actor interface</typeparam> /// <param name="context">context</param> /// <param name="actorKey">actor key</param> /// <param name="manager">actor manager</param> /// <returns>instance of actor implementation</returns> public T Create <T>(IWorkContext context, ActorKey actorKey, IActorManager manager) where T : IActor { context.Verify(nameof(context)).IsNotNull(); actorKey.Verify(nameof(actorKey)).IsNotNull(); manager.Verify(nameof(manager)).IsNotNull(); typeof(T).IsInterface.Verify().Assert(x => x == true, $"{typeof(T)} must be an interface"); context = context.With(_tag); Type actorType = typeof(T); ActorTypeRegistration?typeRegistration = GetTypeRegistration(actorType) ?? GetTypeFromDi(context, actorType); if (typeRegistration == null) { var ex = new KeyNotFoundException($"Registration for {actorType.FullName} was not found"); context.Telemetry.Error(context.With(_tag), "create failure", ex); throw ex; } IActor actorObject = typeRegistration.CreateImplementation(context); // Set actor key and manager ActorBase?actorBase = actorObject as ActorBase; if (actorBase == null) { string failureMsg = $"Created actor type {actorObject.GetType()} does not derive from ActorBase"; context.Telemetry.Error(context.With(_tag), failureMsg); throw new InvalidOperationException(failureMsg); } actorBase.ActorKey = actorKey; actorBase.ActorManager = manager; actorBase.ActorType = actorType; return((T)actorObject); }
public static IWorkContext WithCreateLogger(this IWorkContext context, string eventSourceName) { context.Verify(nameof(context)).IsNotNull(); eventSourceName.Verify(nameof(eventSourceName)).IsNotEmpty(); if (context.Container == null) { return(context); } ITelemetry telementry = context.Container.Resolve <ITelemetryService>() .CreateLogger(eventSourceName); return(context.With(telementry)); }
/// <summary> /// Deactivate actor /// </summary> /// <param name="context">context</param> /// <returns>task</returns> public async Task Deactivate(IWorkContext context) { context.Verify(nameof(context)).IsNotNull(); context = context.With(_tag); int currentValue = Interlocked.CompareExchange(ref _running, 0, 1); if (currentValue != 1) { return; } ActorManager.Configuration.ActorDeactivateEvent(context, ActorKey, this.GetType()); StopTimer(); await OnDeactivate(context).ConfigureAwait(false); }
public void WorkWithPropertyClassTest() { IWorkContext context = WorkContext.Empty; var custom = new Test("Value1"); IWorkContext nextContext = context.With(custom); TestContextProperties(nextContext); context.Properties.ContainsKey("Test").Should().BeFalse(); nextContext.Properties.ContainsKey("Value1"); var test = nextContext.Properties.Get <Test>(); test.Should().NotBeNull(); test.Value.Should().Be(custom.Value); }
/// <summary> /// Clear all actors from the system. Each active actor will be deactivated /// </summary> /// <param name="context">context</param> /// <returns></returns> public async Task Clear(IWorkContext context) { context.Verify(nameof(context)).IsNotNull(); context = context.With(_tag); context.Telemetry.Verbose(context, "Clearing actor container"); List <IActorRegistration> list; lock (_lock) { list = new List <IActorRegistration>(_actorCache.GetValues()); _actorCache.Clear(); } foreach (var item in list) { await item.Instance.Deactivate(context).ConfigureAwait(false); item.Instance.Dispose(); } }
public EventProcessor(IWorkContext context, MetricSampler sampler) { _context = context.With(_tag); _sampler = sampler; }