public async Task Handle(BackgroundTask backgroundTask)
        {
            if (activeBackgroundTaskList.Count > 0)
            {
                logger.LogDebug("Cancel background task");
                try
                {
                    activeBackgroundTaskList.CancelAll();
                }
                catch (Exception e)
                {
                    logger.LogError(e,
                                    "Failed to cancel background task");
                }
            }

            var task = ResolveTask(backgroundTask);

            if (task == null)
            {
                logger.LogError($"Unable to resolve background task '{backgroundTask.Type}'");
                return;
            }

            logger.LogDebug($"Resolved background task '{task.GetType().FullName}'");

            var handler = ResolveHandler(task);

            if (handler == null)
            {
                logger.LogError($"Unable to resolve handler background task '{backgroundTask.Type}'");
                return;
            }

            logger.LogDebug($"Resolved background task handler '{handler.GetType().FullName}'");

            await backgroundTaskQueue.QueueBackgroundWorkItemAsync(handler.Handle, task);
        }
        private IBackgroundTask ResolveTask(BackgroundTask backgroundTask)
        {
            //var task = JsonSerializer.Deserialize(backgroundTask.Payload, Type.GetType(backgroundTask.Type));

            //Console.WriteLine(task == null ? "task is null" : task.GetType().FullName);
            switch (backgroundTask.Type)
            {
            case nameof(PhysicalDriveInfoBackgroundTask):
                return(JsonSerializer.Deserialize <PhysicalDriveInfoBackgroundTask>(backgroundTask.Payload));

            case nameof(ListBackgroundTask):
                return(JsonSerializer.Deserialize <ListBackgroundTask>(backgroundTask.Payload));

            case nameof(ReadBackgroundTask):
                return(JsonSerializer.Deserialize <ReadBackgroundTask>(backgroundTask.Payload));

            case nameof(WriteBackgroundTask):
                return(JsonSerializer.Deserialize <WriteBackgroundTask>(backgroundTask.Payload));

            case nameof(PhysicalDriveVerifyBackgroundTask):
                return(JsonSerializer.Deserialize <PhysicalDriveVerifyBackgroundTask>(backgroundTask.Payload));

            case nameof(ConvertBackgroundTask):
                return(JsonSerializer.Deserialize <ConvertBackgroundTask>(backgroundTask.Payload));

            case nameof(BlankBackgroundTask):
                return(JsonSerializer.Deserialize <BlankBackgroundTask>(backgroundTask.Payload));

            case nameof(OptimizeBackgroundTask):
                return(JsonSerializer.Deserialize <OptimizeBackgroundTask>(backgroundTask.Payload));

            default:
                logger.LogError($"Background task '{backgroundTask.Type}' not supported");
                return(null);
            }
        }
Beispiel #3
0
 public static async Task RunBackgroundTask <THub>(this IHubContext <THub> hubContext,
                                                   BackgroundTask backgroundTask, CancellationToken token = default) where THub : Hub
 {
     await hubContext.Clients.All.SendAsync(Constants.HubMethodNames.RunBackgroundTask, backgroundTask, token);
 }