protected async override Task RunInternal() { SyncerInfoService.Log(GetType().Name, JobStatus.Started.ToString()); using (_importJobRepository.BeginOperation()) { await ProcessImportFileQuery(_importJobRepository.LoadedFileQuery()); await ProcessImportFileQuery(_importJobRepository.ImportedFailedFileQuery()); } SyncerInfoService.Log(GetType().Name, JobStatus.Stopped.ToString()); }
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()); }
protected override async Task RunInternal() { SyncerInfoService.Log(GetType().Name, JobStatus.Started.ToString()); using (_ftpJobRepository.BeginOperation()) { var ftpCredentials = _ftpJobRepository.GetFtpCredentialList(); if (!ftpCredentials.Any()) { GlobalLogger.LogInfo("Job has not found any ftp credentials. Waiting...", GetType().Name, true); } else { var existingFiles = _ftpJobRepository.ImportedFileList(); var existingFileNames = existingFiles.Select(ef => ef.OriginalFileName); foreach (var cred in ftpCredentials) { var totalCount = 0; List <(string fileName, string accountName, DateTime creationDate)> newFiles; var accountNameList = cred.MasterAccounts.Select(m => m.AccountName).ToList(); try { newFiles = (await _ftpLoader.LoadFilesAsync(cred)) .Where(f => f.creationDate >= _fromDate && accountNameList.Contains(f.accountName) && !existingFileNames.Contains(f.fileName)) // order is important for file processing .OrderByDescending(f => _fileNameMatcher.IsAcctStatusReportNonEca(f.fileName)) .ThenByDescending(f => _fileNameMatcher.IsAcctStatusReport(f.fileName)) .ThenByDescending(f => f.creationDate) .ToList(); } catch (Exception ex) { GlobalLogger.LogError($"Error while trying to load files from ftp: [{cred.FtpName}]", ex, GetType().Name, true); return; } FilterFilesByMaxCreateDate(newFiles, existingFiles, _fileNameMatcher.AcctStatusReportNonEcaSqlLike, cred.MasterAccounts); FilterFilesByMaxCreateDate(newFiles, existingFiles, _fileNameMatcher.AcctStatusReportSqlLike, cred.MasterAccounts); FilterFilesByMaxCreateDate(newFiles, existingFiles, _fileNameMatcher.SytossClientInfoSqlLike, cred.MasterAccounts); if (!newFiles.Any()) { GlobalLogger.LogInfo($"Job has not found new files in [{cred.FtpName}]. Waiting...", GetType().Name, true); } else { GlobalLogger.LogInfo($"Job has found new: [{newFiles.Count()}] files on ftp: [{cred.FtpName}].", GetType().Name, true); var transitFiles = _ftpJobRepository.GetTransitFileList(); foreach (var file in newFiles) { if (!existingFiles.All(f => f.OriginalFileName != file.fileName)) { continue; } var newFile = new ImportedFile() { CreatedDate = DateTime.UtcNow, ModifiedDate = DateTime.UtcNow, OriginalFileName = file.fileName, FileCreateDate = file.creationDate, MasterAccount = cred.MasterAccounts.FirstOrDefault(a => a.AccountName == file.accountName), FtpCredential = cred, FileState = FileState.Registered, FileStatus = FileStatus.Success, RegisteredDate = DateTime.UtcNow }; if (transitFiles.Any(f => f.AccountName == file.accountName && file.fileName.Contains(f.OriginalFileName))) { newFile.FileUpload = new FileUpload(); } _ftpJobRepository.AddImportedFile(newFile); totalCount++; GlobalLogger.LogInfo($"New file: {newFile.OriginalFileName} registered from ftp: [{cred.FtpName}].", GetType().Name, true); } try { _ftpJobRepository.SaveChanges(); GlobalLogger.LogInfo($"Amount registered: [{totalCount}] files in DB from ftp: [{cred.FtpName}].", GetType().Name, true); } catch (Exception ex) { GlobalLogger.LogError($"Error while trying to save registered files from ftp: [{cred.FtpName}]", ex, GetType().Name, true); return; } } } } } SyncerInfoService.Log(GetType().Name, JobStatus.Stopped.ToString()); }
protected async override Task RunInternal() { SyncerInfoService.Log(GetType().Name, JobStatus.Started.ToString()); using (_copyJobRepository.BeginOperation()) { try { var registeredFiles = _copyJobRepository .RegisteredFilesQuery() // order is important for file processing .OrderByDescending(f => f.OriginalFileName.Contains(_fileNameMatcher.AcctStatusReportNonEcaSqlLike)) .ThenByDescending(f => f.OriginalFileName.Contains(_fileNameMatcher.AcctStatusReportSqlLike)) .ThenByDescending(f => f.OriginalFileName.Contains(_fileNameMatcher.SytossClientInfoSqlLike)) .ThenByDescending(f => f.FileCreateDate) .ToList(); if (!registeredFiles.Any()) { GlobalLogger.LogInfo("Job has not found new files for downloading. Waiting...", GetType().Name, true); } else { GlobalLogger.LogInfo($"Job has found new files: [{registeredFiles.Count()}] for downloading.", GetType().Name, true); var totalCount = 0; foreach (var file in registeredFiles) { totalCount++; await TryLoadFileAsync(file); GlobalLogger.LogInfo($"Amount copied: [{totalCount}] of [{registeredFiles.Count()}].", GetType().Name, true); } } var loadedFailedFiles = _copyJobRepository.LoadedFailedFileList(); if (!loadedFailedFiles.Any()) { GlobalLogger.LogInfo("Job has not found LOAD FAILED FILES. Waiting...", GetType().Name, true); } else { GlobalLogger.LogInfo($"Job has found LOAD FAILED FILES: [{loadedFailedFiles.Count()}].", GetType().Name, true); var failedCount = 0; foreach (var file in loadedFailedFiles) { failedCount++; await TryLoadFileAsync(file); GlobalLogger.LogInfo($"Amount of recopied (failed files): [{failedCount}] of [{loadedFailedFiles.Count()}].", GetType().Name, true); } } } catch (Exception ex) { GlobalLogger.LogError($"Error while copying files from ftp.", ex, GetType().Name, true); } } SyncerInfoService.Log(GetType().Name, JobStatus.Stopped.ToString()); }