Example #1
0
        public async Task Consume(ConsumeContext <ConversionStartRequest> context)
        {
            using (LogContext.PushProperty(nameof(context.ConversationId), context.ConversationId))
            {
                FileInfo sourceFile = null;

                try
                {
                    Log.Information("Received {CommandName} command with conversationId {ConversationId} from the bus",
                                    nameof(ConversionStartRequest), context.ConversationId);

                    var jobGuid = context.Message.JobGuid;

                    var jobInfo = sftpServer.GetJobInfo(jobGuid);

                    if (jobInfo == null)
                    {
                        Log.Error("Could not retrieve information about job with id {jobGuid}", jobGuid);
                        throw new InvalidOperationException($"Could not retrieve information about job with id {jobGuid}");
                    }

                    // Get source file
                    sourceFile = new FileInfo(Path.Combine(sftpServer.GetJobDirectory(jobGuid).FullName,
                                                           new FileInfo(jobInfo.Request.FileNameWithExtension).Name));

                    if (!sourceFile.Exists)
                    {
                        throw new FileNotFoundException(sourceFile.FullName);
                    }

                    Log.Information("Starting conversion for file {FullName} with job id {jobGuid}", sourceFile.FullName, jobGuid);

                    var result = manager.Convert(jobGuid, sourceFile, context.Message.DestinationExtension, context.Message.VideoQuality);

                    Log.Information("Finished conversion for file {FullName} with job id {jobGuid}", sourceFile.FullName, jobGuid);

                    await context.RespondAsync(new ConversionStartResult
                    {
                        ConvertedFileName = result.Name,
                        JobGuid           = jobGuid,
                        Password          = jobInfo.Result.Password,
                        Port      = jobInfo.Result.Port,
                        UploadUrl = jobInfo.Result.UploadUrl,
                        User      = jobInfo.Result.User
                    });
                }
                catch (Exception e)
                {
                    var message = e.Message;

                    try
                    {
                        // ReSharper disable once PossibleNullReferenceException
                        message += $" (File: {sourceFile.FullName})";
                    }
                    catch
                    {
                        // Hier ist nichts zu tun
                    }

                    Log.Error(e, message);

                    await context.RespondAsync(new ConversionStartResult
                    {
                        IsInvalid    = true,
                        ErrorMessage = e.Message
                    });
                }
            }
        }