Exemple #1
0
        public void Report(IUserIdentifier userIdentifier, int processed, int total)
        {
            var message = new ProgressMessage
            {
                UserIdentifier = userIdentifier,
                Processed      = processed,
                Total          = total
            };

            _messageSender.Send(message, Constants.PhotoMapApi);
        }
        public override Task HandleAsync(EventBase @event, CancellationToken cancellationToken)
        {
            if (@event is PauseProcessingEvent pauseProcessingCommand)
            {
                _downloadManager.Remove(pauseProcessingCommand.UserIdentifier);

                var startedNotification = new Notification
                {
                    UserIdentifier = pauseProcessingCommand.UserIdentifier,
                    Status         = ProcessingStatus.NotRunning
                };

                _messageSender.Send(startedNotification, Constants.PhotoMapApi);
            }

            return(Task.CompletedTask);
        }
        public override Task HandleAsync(EventBase @event, CancellationToken cancellationToken)
        {
            if (@event is ConvertImageEvent convertImageCommand)
            {
                var imageProcessor    = new ImageProcessor(convertImageCommand.FileContents);
                var convertImageBytes = imageProcessor.GetImageBytes();

                var imageConverted = new ImageConverted
                {
                    Id           = convertImageCommand.Id,
                    FileContents = convertImageBytes
                };

                _logger.LogInformation("Image for {Id} converted", convertImageCommand.Id);
                _messageSender.Send(imageConverted, Constants.PhotoMapApi);
            }

            return(Task.CompletedTask);
        }
        public override async Task HandleAsync(EventBase @event, CancellationToken cancellationToken)
        {
            if (@event is StartProcessingEvent startProcessingCommand)
            {
                using var scope = _serviceScopeFactory.CreateScope();

                var userIdentifier = startProcessingCommand.UserIdentifier;

                var stoppingAction = new StoppingAction();
                _downloadManager.Add(userIdentifier, stoppingAction);

                var startedNotification = CreateNotification(userIdentifier, ProcessingStatus.Running);
                _messageSender.Send(startedNotification, Constants.PhotoMapApi);

                if (userIdentifier is YandexDiskUserIdentifier)
                {
                    var yandexDiskDownloadService = scope.ServiceProvider.GetService <IYandexDiskDownloadService>();

                    try
                    {
                        await foreach (var file in yandexDiskDownloadService.DownloadFilesAsync(userIdentifier,
                                                                                                startProcessingCommand.Token, cancellationToken, stoppingAction))
                        {
                            var processedDownloadedFile = await _imageProcessingService.ProcessImageAsync(file);

                            var imageProcessedEvent = CreateResultsCommand(startProcessingCommand.UserIdentifier, processedDownloadedFile);
                            _messageSender.Send(imageProcessedEvent, Constants.PhotoMapApi);
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e.Message);

                        var stoppedNotification =
                            CreateNotification(userIdentifier, ProcessingStatus.NotRunning, true, e.Message);
                        _messageSender.Send(stoppedNotification, Constants.PhotoMapApi);
                    }
                    finally
                    {
                        _downloadManager.Remove(userIdentifier);

                        var finishedNotification = CreateNotification(userIdentifier, ProcessingStatus.NotRunning);
                        _messageSender.Send(finishedNotification, Constants.PhotoMapApi);

                        _logger.LogInformation("Processing finished");
                    }
                }
                else if (userIdentifier is DropboxUserIdentifier)
                {
                    var dropboxDownloadService = scope.ServiceProvider.GetService <IDropboxDownloadService>();

                    try
                    {
                        await foreach (var file in dropboxDownloadService.DownloadAsync(userIdentifier,
                                                                                        startProcessingCommand.Token, stoppingAction, cancellationToken))
                        {
                            var processedDownloadedFile = await _imageProcessingService.ProcessImageAsync(file);

                            var imageProcessedEvent = CreateResultsCommand(startProcessingCommand.UserIdentifier, processedDownloadedFile);
                            _messageSender.Send(imageProcessedEvent, Constants.PhotoMapApi);
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e.Message);

                        var stoppedNotification =
                            CreateNotification(userIdentifier, ProcessingStatus.NotRunning, true, e.Message);
                        _messageSender.Send(stoppedNotification, Constants.PhotoMapApi);
                    }
                    finally
                    {
                        _downloadManager.Remove(userIdentifier);

                        var finishedNotification = CreateNotification(userIdentifier, ProcessingStatus.NotRunning);
                        _messageSender.Send(finishedNotification, Constants.PhotoMapApi);

                        _logger.LogInformation("Processing finished.");
                    }
                }
            }
        }