public async Task <BackgroundJobStatus> ActivityBackgroundJobWorker(
            [ActivityTrigger] BackgroundJobStatus job,
            [SignalR(HubName = SignalRFunctions.HubName)] IAsyncCollector <SignalRMessage> signalr,
            ILogger log)
        {
            var sw       = Stopwatch.StartNew();
            var progress = job.Progress;

            while (progress >= job.Progress)
            {
                // Use the inject worker (processor) to perform the work
                await _worker.Process(job);

                // Update stats
                job.CompletedTaskCount += 1;
                job.ElapsedMiliseconds += sw.ElapsedMilliseconds;
                job.Message             = job.Progress < 100 ? "Processing" : "Complete";
            }
            log.LogWarning($"[{job.Id}] Completed {job.CompletedTaskCount} of {job.TaskCount} [{job.Progress}%]");

            // Broadcast progress updates to subscribes
            await signalr.AddAsync(SignalRFunctions.CreateMessage(job));

            sw.Stop();

            // Return updated status
            return(job);
        }
Beispiel #2
0
        public static SignalRMessage CreateMessage(BackgroundJobStatus payload)
        {
            var message = new SignalRMessage
            {
                Target    = HubStatusUpdateTarget,
                Arguments = new[] { payload }
            };

            return(message);
        }
        public async Task <BackgroundJobStatus> BackgroundJobOrchestrator(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            ILogger log)
        {
            // Get input and setup background job
            var job = context.GetInput <BackgroundJob>();

            // Create a custom status object to track progress
            var status = new BackgroundJobStatus {
                BatchSize     = job.BatchSize,
                CreatedAt     = job.CreatedAt,
                Id            = context.InstanceId,
                MaxTaskLength = job.MaxTaskLength,
                MinTaskLength = job.MinTaskLength,
                Name          = job.Name,
                TaskCount     = job.TaskCount
            };

            if (!context.IsReplaying)
            {
                log.LogWarning($"[{job.Id}] Starting new background job with {job.TaskCount} tasks");
                status.Message = "Initializing...";
            }

            // The orchestrator cannot do any task related work, in fact any attempt to await in here, other then
            // launching an activity will throw an exception
            while (status.Progress < 100)
            {
                status = await context.CallActivityAsync <BackgroundJobStatus>(nameof(ActivityBackgroundJobWorker), status);

                context.SetCustomStatus(status);
            }

            // Clear out the status in the orchestrator because the work is done and will be returned in the output
            context.SetCustomStatus(null);

            return(status);
        }