Ejemplo n.º 1
0
        public async Task <IReadOnlyList <byte> > Download(IWorkContext context, string path)
        {
            context.Verify(nameof(context)).IsNotNull();
            path.Verify(nameof(path)).IsNotEmpty();

            BlobClient       blobClient = _containerClient.GetBlobClient(path);
            BlobDownloadInfo download   = await blobClient.DownloadAsync();

            using MemoryStream memory = new MemoryStream();
            using var writer          = new StreamWriter(memory);

            await download.Content.CopyToAsync(memory);

            writer.Flush();
            memory.Position = 0;

            return(memory.ToArray());
        }
        /// <summary>
        /// Run receiver
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="context"></param>
        /// <param name="taskCount"></param>
        /// <param name="receiver"></param>
        /// <returns></returns>
        public Task Run(IWorkContext context, int taskCount, Func <Message, Task> receiver)
        {
            context.Verify(nameof(context)).IsNotNull();
            context.Container.Verify(nameof(context.Container)).IsNotNull();
            taskCount.Verify(nameof(taskCount)).Assert(x => x >= 1, "Number of task must greater or equal to 1");
            receiver.Verify(nameof(receiver)).IsNotNull();

            var tasks = Enumerable.Range(0, taskCount)
                        .Select(x =>
            {
                IMessageProcessor messageProcessor = context.Container !.Resolve <IMessageProcessor>();
                _messageProcessors.Add(messageProcessor);
                context.Telemetry.Info(context, "Starting receiver");
                return(messageProcessor.Register(context, receiver));
            })
                        .ToList();

            return(Task.WhenAll(tasks));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Execute Set the work item(s) in the plan
        /// </summary>
        /// <param name="context">work context</param>
        /// <returns>true if passed, false if not</returns>
        public async Task <bool> Set(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 testResult = await item.Test(context).ConfigureAwait(false);

                    context.CancellationToken.ThrowIfCancellationRequested();
                    context.Telemetry.Verbose(context, $"Executed state plan 'Test' for item {item.Name} with {testResult} result");

                    if (!testResult)
                    {
                        context.Telemetry.Verbose(context, $"Executing state plan 'Set' for item {item.Name}");

                        testResult = await item.Set(context).ConfigureAwait(false);

                        context.CancellationToken.ThrowIfCancellationRequested();
                        context.Telemetry.Verbose(context, $"Executed state plan 'Set' for item {item.Name} with {testResult} result");

                        if (!testResult && !item.IgnoreError)
                        {
                            context.Telemetry.Verbose(context, $"State plan did not completed successfully, name={item.Name}");
                            return(false);
                        }
                    }
                }

                context.Telemetry.Verbose(context, "State plan completed successfully");
                return(true);
            }
            finally
            {
                context.Telemetry.Verbose(context, "State plan exited");
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create transparent proxy for instance of actor class
        /// </summary>
        /// <param name="context">work context</param>
        /// <param name="instance">instance of actor class</param>
        /// <param name="manager">actor manager</param>
        /// <returns>proxy</returns>
        public static T Create(IWorkContext context, IActorBase instance, IActorManager manager)
        {
            context.Verify(nameof(context)).IsNotNull();
            instance.Verify(nameof(instance)).IsNotNull();
            manager.Verify(nameof(manager)).IsNotNull();

            object proxyObject = Create <T, ActorProxy <T> >();

            ActorProxy <T> proxy = (ActorProxy <T>)proxyObject;

            proxy._instance = instance;
            proxy._manager  = manager;

            proxy._workContext = new WorkContextBuilder(context)
                                 .Set(_tag)
                                 .Set(_cv)
                                 .Build();

            return((T)proxyObject);
        }
Ejemplo n.º 5
0
        /// <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();
            }
        }
Ejemplo n.º 6
0
        public async Task Run(IWorkContext context)
        {
            context.Verify(nameof(context)).IsNotNull();
            context = context
                      .WithCreateLogger(nameof(SendEvents))
                      .With(_tag);

            context.Telemetry.Info(context, $"Sending events, Task Count:{_option.TaskCount}, ...");

            try
            {
                await SendMessagesToEventHub(context);
            }
            finally
            {
                await _sendEvent.Value.CloseAsync(context);

                _sendEvent.Dispose();
            }

            context.Telemetry.Info(context, "Completed sending events...");
        }
Ejemplo n.º 7
0
        public async Task <string?> Get(IWorkContext context, string path)
        {
            context.Verify(nameof(context)).IsNotNull();
            path.Verify(nameof(path)).IsNotEmpty();

            BlobClient       blobClient = _containerClient.GetBlobClient(path);
            BlobDownloadInfo download   = await blobClient.DownloadAsync();

            using (MemoryStream memory = new MemoryStream())
                using (var writer = new StreamWriter(memory))
                {
                    await download.Content.CopyToAsync(memory);

                    writer.Flush();
                    memory.Position = 0;

                    using (StreamReader reader = new StreamReader(memory))
                    {
                        return(reader.ReadToEnd());
                    }
                }
        }
Ejemplo n.º 8
0
        /// <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);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Start orchestrator host, jobs will be started based on their directed graph edges
        /// </summary>
        /// <param name="context">context</param>
        /// <returns>this</returns>
        public OrchestratorHost <TKey, TNode, TEdge> Start(IWorkContext context)
        {
            context.Verify(nameof(context)).IsNotNull();

            int running = Interlocked.CompareExchange(ref _running, 1, 0);

            if (running == 1)
            {
                return(this);
            }

            _processedDict.Clear();
            _runningKeys.Clear();
            _graphContext = new GraphTopologicalContext <TKey, TEdge>(maxLevels: 1, equalityComparer: _graph.KeyCompare);

            context.Telemetry.Verbose(context, "Starting Orchestrator Host");

            RunningTask = Task.Run(() => RunJobGraph(context))
                          .ContinueWith(x => _running = 0);

            context.Telemetry.Verbose(context, "Started Orchestrator Host");
            return(this);
        }
Ejemplo n.º 10
0
        public TelemetryService AddErrorReplay(IWorkContext context, TelemetryType console, ITelemetryQuery logger, ITelemetryService telemetryService)
        {
            context.Verify(nameof(context)).IsNotNull();
            logger.Verify(nameof(logger)).IsNotNull();
            telemetryService.Verify(nameof(telemetryService)).IsNotNull();

            void action(TelemetryMessage x)
            {
                IReadOnlyList <TelemetryMessage> loggedMessages = logger.Query(y => x.WorkContext.Cv == y.WorkContext.Cv, 30, 100);

                loggedMessages.ForEach(y => telemetryService.Write(y.WithReplay()));
            }

            bool filter(TelemetryMessage x) =>
            (console != TelemetryType.Verbose) &&
            (!x.TelemetryType.IsReplay()) &&
            (x.TelemetryType.IsEvent()) &&
            (x.TelemetryType.IsErrorOrCritical()) &&
            (x?.WorkContext?.Cv != null);

            _pipeline.DoAction(action, filter);

            return(this);
        }
Ejemplo n.º 11
0
        public static async Task <RestResponse <T> > GetResponseAsync <T>(this HttpResponseMessage message, IWorkContext context)
            where T : class
        {
            context.Verify(nameof(context)).IsNotNull();
            context = context.WithMethodName();

            try
            {
                string json = await message.Content.ReadAsStringAsync();

                if (typeof(T) == typeof(string))
                {
                    return(new RestResponse <T>(message, (T)(object)json));
                }

                T returnType = JsonConvert.DeserializeObject <T>(json);
                return(new RestResponse <T>(message, returnType));
            }
            catch (Exception ex)
            {
                context.Telemetry.Error(context, $"{nameof(GetResponseAsync)} error '{ex.Message}", ex);
                throw;
            }
        }
Ejemplo n.º 12
0
        public Task DeleteContainer(IWorkContext context)
        {
            context.Verify(nameof(context)).IsNotNull();

            return(_containerClient.DeleteIfExistsAsync(cancellationToken: context.CancellationToken));
        }