Пример #1
0
        protected async override Task RunInternal()
        {
            using (_importJobRepository.BeginOperation())
            {
                var          newFileStatus = FileStatus.Success;
                ImportedFile currentFile   = null;
                try
                {
                    currentFile = _importJobRepository
                                  .ImportedFilesQuery()
                                  .OrderByDescending(
                        f => f.OriginalFileName.StartsWith(_fileNameMatcher.AcctStatusReportNonEcaSqlLike))
                                  .ThenByDescending(f => f.OriginalFileName.StartsWith(_fileNameMatcher.AcctStatusReportSqlLike))
                                  .ThenByDescending(f => f.OriginalFileName.Contains(_fileNameMatcher.SytossClientInfoSqlLike))
                                  .ThenBy(f => f.FileCreateDate)
                                  .FirstOrDefault(f => f.FileState == FileState.Loaded &&
                                                  f.FileStatus == FileStatus.Success);

                    if (currentFile == null)
                    {
                        _interval *= 2;

                        if (_interval > 30000)
                        {
                            _interval = 30000;
                        }

                        return;
                    }

                    _interval = 300;

                    //retrieving steam of current file
                    var streamFile = _extractFileService.ExtractFile(currentFile.OriginalFileName);

                    if (_fileNameMatcher.IsFeesDataFile(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessFeesFileReport(currentFile, streamFile);
                    }
                    else if (_fileNameMatcher.IsAcctStatusReportNonEca(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessAcctStatusReportFileReport(currentFile,
                                                                                "AccountStatusReport",
                                                                                _fileNameMatcher.AcctStatusReportNonEcaSqlLike, streamFile);
                    }
                    else if (_fileNameMatcher.IsNavRegex(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessNavFileReport(currentFile, streamFile);
                    }
                    else if (_fileNameMatcher.IsAcctStatusReport(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessAcctStatusReportFileReport(currentFile,
                                                                                "ECAAccountStatusReport",
                                                                                _fileNameMatcher.AcctStatusReportSqlLike, streamFile);
                    }
                    else if (_fileNameMatcher.IsCashReport(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessCashReport(currentFile, streamFile);
                    }
                    else if (_fileNameMatcher.IsTradeAsReport(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessTradesAsReport(currentFile, streamFile);
                    }
                    else if (_fileNameMatcher.IsSytossClientInfo(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessSytossClientInfoReport(currentFile, streamFile);
                    }
                }
                catch (Exception ex)
                {
                    newFileStatus = FileStatus.Failed;
                    loggerException.Error(this.GetErrorLogMessage(ex));
                }
                finally
                {
                    if (currentFile != null)
                    {
                        currentFile.FileState  = FileState.Imported;
                        currentFile.FileStatus = newFileStatus;

                        _importJobRepository.UpdateImportedFile(currentFile);

                        _importJobRepository.SaveChanges();

                        if (newFileStatus == FileStatus.Failed)
                        {
                            Console.WriteLine($"ImportJob: Error while importing file {currentFile.OriginalFileName}");
                            logger.Log(LogLevel.Info,
                                       $"import$ImportJob: Error while importing file {currentFile.OriginalFileName}");

                            Thread.Sleep(1000 * 10);
                            // ReloadThread();
                        }
                        else
                        {
                            Console.WriteLine($"ImportJob: {currentFile.OriginalFileName} has been imported");
                            logger.Log(LogLevel.Info,
                                       $"import$ImportJob: {currentFile.OriginalFileName} has been imported");
                        }
                    }
                    else
                    {
                        logger.Log(LogLevel.Info,
                                   $"import$ImportJob: has not found new files for downloading. Waiting...");
                        Console.WriteLine("ImportJob: has not found new files for downloading. Waiting...");
                    }
                }
            }
        }
Пример #2
0
        protected override async Task RunInternal()
        {
            SyncerInfoService.Log(GetType().Name, JobStatus.Started.ToString());

            using (_deliveryJobRepo.BeginOperation())
            {
                try
                {
                    var filesToSend = _deliveryJobRepo.GetFilesToSendList();

                    if (!filesToSend.Any())
                    {
                        GlobalLogger.LogInfo("Job has not found new files for uploading. Waiting...", GetType().Name, true);
                    }
                    else
                    {
                        var totalCount = 0;
                        foreach (var fileUpload in filesToSend)
                        {
                            totalCount++;
                            var originalFileName = _deliveryJobRepo.GetOriginalFileName(fileUpload.Id);
                            var fileStream       = _extractFileService.ExtractFile(originalFileName);

                            try
                            {
                                var response = await _restApiService.GetResponseOfUploadAsync(originalFileName, fileStream);

                                GlobalLogger.LogInfo($@"Response upload file: {originalFileName} to ZOHO api. | Status: {response.StatusCode} | ReasonPhrase: {response.ReasonPhrase} | RequestUri: {response.RequestMessage.RequestUri}", GetType().Name, true);

                                fileUpload.IsSent = response.IsSuccessStatusCode;

                                if (!response.IsSuccessStatusCode)
                                {
                                    GlobalLogger.LogInfo($"File: {originalFileName} unsuccessfully uploaded | Status Code from ZOHO api: {response.StatusCode}", GetType().Name, true);
                                    continue;
                                }

                                fileUpload.SentDate = DateTime.UtcNow;
                                GlobalLogger.LogInfo($"File: {originalFileName} successfully uploaded to ZOHO api.", GetType().Name, true);
                            }
                            catch (Exception ex)
                            {
                                GlobalLogger.LogError($"Error while upload file: {originalFileName} to ZOHO api.", ex, GetType().Name, true);
                            }
                            finally
                            {
                                _deliveryJobRepo.UpdateFileUpload(fileUpload);
                                await _deliveryJobRepo.SaveChangesAsync();
                            }

                            GlobalLogger.LogInfo($"Amount uploaded: [{totalCount}] of [{filesToSend.Count()}].", GetType().Name, true);
                        }
                    }
                }
                catch (Exception ex)
                {
                    GlobalLogger.LogError($"Error while upload files to ZOHO api.", ex, this.GetType().Name, true);
                }
            }

            SyncerInfoService.Log(GetType().Name, JobStatus.Stopped.ToString());
        }
Пример #3
0
        private async Task TryImportFile(ImportedFile currentFile)
        {
            var newFileStatus = FileStatus.Success;

            try
            {
                if (currentFile != null)
                {
                    GlobalLogger.LogInfo($"Start importing file: {currentFile.OriginalFileName} | Status: {currentFile.FileStatus}", GetType().Name, true);

                    var streamFile = _extractFileService.ExtractFile(currentFile.OriginalFileName);

                    if (_fileNameMatcher.IsFeesDataFile(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessFeesFileReport(currentFile, streamFile);
                    }
                    else if (_fileNameMatcher.IsAcctStatusReportNonEca(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessAcctStatusReportFileReport(
                            currentFile,
                            "AccountStatusReport",
                            _fileNameMatcher.AcctStatusReportNonEcaSqlLike,
                            streamFile);
                    }
                    else if (_fileNameMatcher.IsNavRegex(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessNavFileReport(currentFile, streamFile);
                    }
                    else if (_fileNameMatcher.IsAcctStatusReport(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessAcctStatusReportFileReport(
                            currentFile,
                            "ECAAccountStatusReport",
                            _fileNameMatcher.AcctStatusReportSqlLike,
                            streamFile);
                    }
                    else if (_fileNameMatcher.IsCashReport(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessCashReport(currentFile, streamFile);
                    }
                    else if (_fileNameMatcher.IsTradeAsReport(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessTradesAsReport(currentFile, streamFile);
                    }
                    else if (_fileNameMatcher.IsSytossClientInfo(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessSytossClientInfoReport(currentFile, streamFile);
                    }
                    else if (_fileNameMatcher.IsSytossOpenPositions(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessSytossOpenPositions(currentFile, streamFile);
                    }
                    else if (_fileNameMatcher.IsTradeExeReport(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessTradesExeReport(currentFile, streamFile);
                    }
                    else if (_fileNameMatcher.IsInterestAccruaReport(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessTradeInterestAccruaReport(currentFile, streamFile);
                    }
                    else if (_fileNameMatcher.IsTradeCommissionsDetReport(currentFile.OriginalFileName))
                    {
                        newFileStatus = await ProcessTradeCommissionsReport(currentFile, streamFile);
                    }
                }

                GlobalLogger.LogInfo($"File: {currentFile.OriginalFileName} has been imported.", GetType().Name, true);
            }
            catch (Exception ex)
            {
                newFileStatus = FileStatus.Failed;
                GlobalLogger.LogError($"Error while importing file {currentFile?.OriginalFileName}.", ex, GetType().Name, true);
            }
            finally
            {
                if (currentFile != null)
                {
                    currentFile.FileState  = FileState.Imported;
                    currentFile.FileStatus = newFileStatus;
                    if (newFileStatus == FileStatus.Success)
                    {
                        currentFile.ImportedDate = DateTime.UtcNow;
                    }

                    _importJobRepository.UpdateImportedFile(currentFile);
                    _importJobRepository.SaveChanges();
                }
                else
                {
                    GlobalLogger.LogInfo("Job has not found new files for importing. Waiting...", GetType().Name, true);
                }
            }
        }