public void CompleteDocument(DocumentTarget documentTarget)
 {
     try
     {
         File.Delete(documentTarget.InputFile);
     }
     catch (Exception e)
     {
         Log.Warning("Unable to complete document {0}:{1} - {2}", documentTarget.Id, documentTarget.InputFile, e.Message);
     }
 }
        public DocumentTarget NextDocument()
        {
            DocumentTarget documentTarget = null;

            try
            {
                string[] files = Directory.GetFiles(_watchPath, "*.docx");
                if (files.Count() > 0)
                {
                    documentTarget = new DocumentTarget()
                    {
                        Id         = _documentIndex++,
                        InputFile  = files[0],
                        OutputFile = "",
                        ResultCode = 0
                    };
                }
            }
            catch (Exception e)
            {
                Log.Warning("Unable to read source directory {0} - {1}", _watchPath, e.Message);
            }
            return(documentTarget);
        }
Exemple #3
0
 public void CompleteDocument(DocumentTarget documentTarget)
 {
     _connection.Execute($"UPDATE {_tableName} SET ResultCode=@ResultCode, InputFile=@InputFile, OutputFile=@OutputFile WHERE ID=@Id", documentTarget);
 }
        public void ProcessDocuments()
        {
            ConvertService convertService = null;

            if (_processLock)
            {
                Log.Debug("Re-entry into service process, still busy.");
                return;
            }

            _processLock = true;

            if (!String.IsNullOrEmpty(_heartbeatEndpoint))
            {
                // synchronous use of async. reading repsonse string as async synchronizes the call
                using (var client = new HttpClient())
                {
                    var response = client.GetAsync(_heartbeatEndpoint).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        var responseContent = response.Content;
                        responseContent.ReadAsStringAsync();
                        Log.Debug("UptimeRobot heartbeat link touched");
                    }
                }
            }

            try
            {
                Log.Debug("Processing pending documents.");
                foreach (IDocumentQueue documentQueue in _documentQueues)
                {
                    while (documentQueue.Count() > 0)
                    {
                        if (convertService == null)
                        {
                            convertService = new ConvertService();
                            convertService.Initialize();
                        }
                        DocumentTarget documentTarget = documentQueue.NextDocument();
                        Log.Information($"Generating PDF for {documentQueue.SourceName()}: {documentTarget.Id}: {documentTarget.InputFile}");
                        try
                        {
                            string outFile = documentTarget.OutputFile;
                            documentTarget.ResultCode = convertService.Convert(documentTarget.InputFile, ref outFile);
                            documentTarget.OutputFile = outFile;
                        }
                        catch (Exception e)
                        {
                            Log.Warning("Conversion exception {0}", e.Message);
                            documentTarget.ResultCode = (int)ExitCode.InternalError;
                        }
                        Log.Information($"Generated PDF for {documentTarget.Id} as {documentTarget.OutputFile} with Result {documentTarget.ResultCode}");
                        documentQueue.CompleteDocument(documentTarget);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error("Unhandled exception in ProcessDocuments {0}", e);
            }
            _processLock = false;
        }