예제 #1
0
        public async Task <bool> BatchRunAsync(CancellationToken cancellationToken = default)
        {
            m_JournalWriter.WriteLine($"Batch macro running started");

            var batchStartTime = DateTime.Now;

            TimeSpan?timeout = null;

            if (m_Job.Timeout > 0)
            {
                timeout = TimeSpan.FromSeconds(m_Job.Timeout);
            }

            var worker = m_WorkerFact.Invoke(timeout);

            worker.Retry   += OnRetry;
            worker.Timeout += OnTimeout;

            m_CurrentContext = new BatchJobContext()
            {
                Job = m_Job
            };

            var jobResult = false;

            try
            {
                await TaskEx.Run(() =>
                {
                    m_JournalWriter.WriteLine($"Collecting files for processing");

                    var app = EnsureApplication(m_CurrentContext, cancellationToken);

                    var allFiles = PrepareJobScope(app, m_Job.Input, m_Job.Filters, m_Job.Macros);

                    m_JournalWriter.WriteLine($"Running batch processing for {allFiles.Length} file(s)");

                    m_ProgressHandler.SetJobScope(allFiles, batchStartTime);

                    if (!allFiles.Any())
                    {
                        throw new UserException("Empty job. No files matching specified filter");
                    }

                    var curBatchSize = 0;

                    for (int i = 0; i < allFiles.Length; i++)
                    {
                        var curAppPrc = m_CurrentContext.CurrentApplicationProcess?.Id;

                        m_CurrentContext.CurrentJobItem = allFiles[i];
                        var res = TryProcessFile(m_CurrentContext, worker, cancellationToken);

                        TryCloseDocument(m_CurrentContext.CurrentDocument);

                        m_CurrentContext.CurrentDocument = null;

                        m_ProgressHandler?.ReportProgress(m_CurrentContext.CurrentJobItem, res);

                        if (!res && !m_Job.ContinueOnError)
                        {
                            throw new UserException("Cancelling the job. Set 'Continue On Error' option to continue job if file failed");
                        }

                        if (m_CurrentContext.CurrentApplicationProcess?.Id != curAppPrc)
                        {
                            curBatchSize = 1;
                        }
                        else
                        {
                            curBatchSize++;
                        }

                        if (m_Job.BatchSize > 0 && curBatchSize >= m_Job.BatchSize)
                        {
                            m_JournalWriter.WriteLine("Closing application as batch size reached the limit");
                            TryShutDownApplication(m_CurrentContext.CurrentApplicationProcess);
                            curBatchSize = 0;
                        }
                    }
                }, new StaTaskScheduler(m_Logger)).ConfigureAwait(false);

                jobResult = true;
            }
            catch (OperationCanceledException)
            {
                throw new JobCancelledException();
            }
            finally
            {
                TryShutDownApplication(m_CurrentContext.CurrentApplicationProcess);
            }

            var duration = DateTime.Now.Subtract(batchStartTime);

            m_ProgressHandler.ReportCompleted(duration);

            m_JournalWriter.WriteLine($"Batch running completed in {duration.ToString(@"hh\:mm\:ss")}");

            return(jobResult);
        }
예제 #2
0
        public async Task <bool> BatchRun(BatchJob opts, CancellationToken cancellationToken = default)
        {
            m_UserLogger.WriteLine($"Batch macro running started");

            var batchStartTime = DateTime.Now;

            var allFiles = PrepareJobScope(opts.Input, opts.Filters, opts.Macros).ToArray();

            m_UserLogger.WriteLine($"Running batch processing for {allFiles.Length} file(s)");

            m_ProgressHandler.SetJobScope(allFiles, batchStartTime);

            if (!allFiles.Any())
            {
                throw new UserMessageException("Empty job. No files matching specified filter");
            }

            TimeSpan timeout = default;

            if (opts.Timeout > 0)
            {
                timeout = TimeSpan.FromSeconds(opts.Timeout);
            }

            IXApplication app    = null;
            Process       appPrc = null;

            if (cancellationToken != default)
            {
                cancellationToken.Register(() =>
                {
                    m_UserLogger.WriteLine($"Cancelled by the user");
                    TryShutDownApplication(appPrc);
                });
            }

            var jobResult = false;

            try
            {
                await Task.Run(() =>
                {
                    var curBatchSize = 0;

                    for (int i = 0; i < allFiles.Length; i++)
                    {
                        var curAppPrc = appPrc?.Id;

                        var curFile = allFiles[i];
                        var res     = AttemptProcessFile(ref app, ref appPrc, curFile, opts, cancellationToken);
                        m_ProgressHandler?.ReportProgress(curFile, res);

                        if (!res && !opts.ContinueOnError)
                        {
                            throw new UserMessageException("Cancelling the job. Set 'Continue On Error' option to continue job if file failed");
                        }

                        if (appPrc?.Id != curAppPrc)
                        {
                            curBatchSize = 1;
                        }
                        else
                        {
                            curBatchSize++;
                        }

                        if (opts.BatchSize > 0 && curBatchSize >= opts.BatchSize && !cancellationToken.IsCancellationRequested)
                        {
                            m_UserLogger.WriteLine("Closing application as batch size reached the limit");
                            TryShutDownApplication(appPrc);
                            curBatchSize = 0;
                        }
                    }
                }).ConfigureAwait(false);

                jobResult = true;
            }
            finally
            {
                TryShutDownApplication(appPrc);
            }

            var duration = DateTime.Now.Subtract(batchStartTime);

            m_ProgressHandler.ReportCompleted(duration);

            m_UserLogger.WriteLine($"Batch running completed in {duration.ToString(@"hh\:mm\:ss")}");

            return(jobResult);
        }