/// <summary>
 /// Конструктор
 /// </summary>
 /// <param name="parameters"></param>
 public OnlineParserWebQueue(ParserWebQueueParameters parameters)
 {
     WEBSERVICE_URL = parameters.WebServiceUrl;
     _method        = parameters.Method;
     _timeout       = parameters.Timeout;
     _contentType   = parameters.ContentType;
 }
Пример #2
0
        public static int Main(string[] args)
        {
            if (AppDomain.CurrentDomain.IsDefaultAppDomain())
            {
                // RazorEngine cannot clean up from the default appdomain...
                Console.WriteLine("Switching to second AppDomain, for RazorEngine...");
                AppDomainSetup adSetup = new AppDomainSetup();
                adSetup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                var current = AppDomain.CurrentDomain;
                // You only need to add strongnames when your appdomain is not a full trust environment.
                var strongNames = new StrongName[0];

                var domain = AppDomain.CreateDomain(
                    "MyMainDomain", null,
                    current.SetupInformation, new PermissionSet(PermissionState.Unrestricted),
                    strongNames);
                var exitCode = domain.ExecuteAssembly(Assembly.GetExecutingAssembly().Location);
                // RazorEngine will cleanup.
                AppDomain.Unload(domain);
                return(exitCode);
            }

            Console.WriteLine("-- ParserQueueRunner, ver 0.2 --");
            Console.WriteLine("Starting work.");

            // Проверка работы Excel Addin "Parser"
            //string res = CheckExcelAddin();

            string EXE         = Assembly.GetExecutingAssembly().GetName().Name;
            string startupPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
            string iniFullPath = Path.Combine(startupPath, EXE + ".ini");

            ParserWebQueueParameters parserWebQueueParameters = new ParserWebQueueParameters();
            EmailSenderConfig        senderConfig             = new EmailSenderConfig();
            WebParserConfig          parserConfig             = new WebParserConfig();
            int nElementsToProcess = 0;

            try
            {
                // Считывание настроек программы
                IniReader iniReader = new IniReader(iniFullPath);
                string    testValue = iniReader.GetValue("test", "section", "10");
                Console.WriteLine($"Read config file: {iniFullPath}");

                // Program params
                nElementsToProcess = iniReader.GetIntValue("NumberElementsForProcessing", "Program", 0);

                // WebService parameters
                parserWebQueueParameters.WebServiceUrl = iniReader.GetValue("WebServiceUrl", "QueueWebService");
                parserWebQueueParameters.Method        = "Get";
                parserWebQueueParameters.Timeout       = iniReader.GetIntValue("Timeout", "QueueWebService", 20000);
                parserWebQueueParameters.ContentType   = "application/json";

                // Email parameters
                senderConfig.host          = iniReader.GetValue("Host", "EmailSender");
                senderConfig.port          = iniReader.GetIntValue("Port", "EmailSender");
                senderConfig.enableSsl     = iniReader.GetBoolValue("EnableSsl", "EmailSender");
                senderConfig.username      = iniReader.GetValue("UserName", "EmailSender");
                senderConfig.password      = iniReader.GetValue("Password", "EmailSender");
                senderConfig.usernameAlias = iniReader.GetValue("UserNameAlias", "EmailSender");

                // Excel Addin Parser
                parserConfig.AddinConfigName             = iniReader.GetValue("ParserConfigName", "ExcelAddinParser");
                parserConfig.DealNumberColumn            = iniReader.GetValue("DealNumberColumn", "ExcelAddinParser");
                parserConfig.IsTrackColumn               = iniReader.GetValue("IsTrackColumn", "ExcelAddinParser");
                parserConfig.StartRowNumber              = iniReader.GetIntValue("StartRowNumber", "ExcelAddinParser", 2);
                parserConfig.ResultNumberColumn          = iniReader.GetValue("ResultNumberColumn", "ExcelAddinParser");
                parserConfig.DealNumberHyperlinkColumn   = iniReader.GetValue("DealNumberHyperlinkColumn", "ExcelAddinParser");
                parserConfig.DocumentPdfFolderNameColumn = iniReader.GetValue("DocumentPdfFolderNameColumn", "ExcelAddinParser");
                parserConfig.DocumentPdfUrlColumn        = iniReader.GetValue("DocumentPdfUrlColumn", "ExcelAddinParser");
                parserConfig.LastDealDateColumn          = iniReader.GetValue("LastDealDateColumn", "ExcelAddinParser");
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine($"Can't find ini file at {iniFullPath}.");
                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error readind ini file: {ex.GetType()}");
                return(0);
            }

            while (nElementsToProcess-- > 0)
            {
                int cnt = processQueue(parserWebQueueParameters, senderConfig, parserConfig);
                if (cnt == 0)
                {
                    break;
                }
            }

            Console.WriteLine("Work finished. Press any key.");
            //Console.ReadKey();

            return(0);
        }
Пример #3
0
        /// <summary>
        /// Обработка элемента очереди ParserQueue
        /// </summary>
        /// <returns>Кол-во обработанных элементов очереди</returns>
        public static int processQueue(ParserWebQueueParameters parserWebQueueParameters, EmailSenderConfig emailSenderConfig,
                                       WebParserConfig webParserConfig)
        {
            // Создание обработчика очереди
            IParserWebQueue    parserWebQueue = new OnlineParserWebQueue(parserWebQueueParameters);
            ParserQueueElement elt            = null;

            try
            {
                // Получение элемента очереди
                elt = parserWebQueue.GetNewElement();

                if (elt == null)
                {
                    Console.WriteLine("No new elements to process.");
                    return(0);
                }

                // Пометка статусом "Взят в обработку"
                parserWebQueue.SetQueueElementStatus(elt.ParserQueueId, 2);

                // Запустить web-parser
                Console.WriteLine("Start parsing website for Document Number: {0}", elt.ClientDocNum);
                WebParserResult parserResult = ParseDocument(webParserConfig, elt.ClientDocNum);

                Console.WriteLine("parser result: {0}. Last date: {1}, pdf link: {2}",
                                  parserResult.ParserStatus,
                                  parserResult.LastDealDate,
                                  parserResult.CardUrl);

                if (parserResult.ParserStatus != "Ok")
                {
                    Console.WriteLine("Error: {0}", parserResult.ParserError);
                }

                // Отправка Email
                string   fileName    = parserResult.DocumentPfdPath;
                string   emailTo     = elt.ClientEmail;
                string   docNumber   = elt.ClientDocNum;
                DateTime requestDate = elt.CreatedTimeUtc;
                sendEmailByNewInterface(emailSenderConfig, fileName, emailTo, docNumber, requestDate, parserResult);

                printParserQueueElement(elt);

                // Пометка элемента очереди как успешно обработанный
                parserWebQueue.SetQueueElementStatus(elt.ParserQueueId, 3);

                return(1);
            }
            catch (Exception exc)
            {
                Console.WriteLine("Error: {0}", exc.Message);

                // Установить статус "Ошибка обработки"
                if (elt != null)
                {
                    parserWebQueue.SetQueueElementStatus(elt.ParserQueueId, 4, exc.Message);
                }
            }
            return(0);
        }