Example #1
0
        /// <summary>
        ///     Runs the job and all actions
        /// </summary>
        public async Task RunJob(Job job, IOutputFileMover outputFileMover) //todo: Store OutputFileMover somewhere workflow dependant
        {
            _logger.Trace("Starting job");

            _logger.Debug("Output filename template is: {0}", job.OutputFileTemplate);
            _logger.Debug("Output format is: {0}", job.Profile.OutputFormat);
            _logger.Info("Converting " + job.OutputFileTemplate);

            SetTempFolders(job);

            try
            {
                // TODO: Use async/await
                _actionExecutor.CallPreConversionActions(job);

                _processingHelper.ApplyFormatRestrictionsToProfile(job);

                var converter      = _converterFactory.GetConverter(job.JobInfo.JobType);
                var reportProgress = new EventHandler <ConversionProgressChangedEventArgs>((sender, args) => job.ReportProgress(args.Progress));
                converter.OnReportProgress += reportProgress;

                var isPdf = job.Profile.OutputFormat.IsPdf();
                var isProcessingRequired = _processingHelper.IsProcessingRequired(job);
                converter.Init(isPdf, isProcessingRequired);

                converter.FirstConversionStep(job);

                _actionExecutor.CallConversionActions(job);

                converter.SecondConversionStep(job);

                converter.OnReportProgress -= reportProgress;

                await outputFileMover.MoveOutputFiles(job);

                if (job.OutputFiles.Count == 0)
                {
                    _logger.Error("No output files were created for unknown reason");
                    throw new ProcessingException("No output files were created for unknown reason", ErrorCode.Conversion_UnknownError);
                }

                LogOutputFiles(job);

                job.TokenReplacer = _tokenReplacerFactory.BuildTokenReplacerWithOutputfiles(job);

                _actionExecutor.CallPostConversionActions(job);

                CleanUp(job);

                job.IsSuccessful = true;
                _logger.Trace("Job finished successfully");
            }
            catch (Exception ex)
            {
                if (ex is ProcessingException processingException)
                {
                    _logger.Error($"The job failed: {processingException.Message} ({processingException.ErrorCode})");
                }
                else
                {
                    _logger.Error(ex, $"The job failed: {ex.Message}");
                }

                if (job.CleanUpOnError)
                {
                    CleanUp(job);
                }

                throw;
            }
            finally
            {
                _logger.Trace("Calling job completed event");
                job.CallJobCompleted();
            }
        }