private static PledgeLog CreateFileProcessPledgeLog(IConfiguration configuration, IRemoteJob job, Dictionary<string, string> loggingOptions) { string sessionId = Guid.Empty.ToString(); string fileName = null; loggingOptions?.TryGetValue(PledgeGlobal.SessionId, out sessionId); loggingOptions?.TryGetValue(PledgeGlobal.InputFileName, out fileName); PledgeLog logEntry = new PledgeLog() { ConfigId = configuration.Id.ToString(), StartTime = DateTime.UtcNow, DateCreated = DateTime.UtcNow, SessionId = sessionId, RunType = sessionId == Guid.Empty.ToString() ? RunType.ScheduledJobRun : RunType.WebsiteRun }; if (job != null) { logEntry.JobId = job.JobId.ToString(); var propertyBag = job.IngestMediumSettings; if (string.IsNullOrWhiteSpace(fileName)) { var sourceFolder = propertyBag.GetProperty(PledgeGlobal.ImportFolderKey); var filePattern = propertyBag.GetProperty(PledgeGlobal.FilePatternKey); try { fileName = Directory.GetFiles(sourceFolder, filePattern).FirstOrDefault(); } catch { fileName = $"{filePattern} (in) {sourceFolder}"; } } logEntry.Write(propertyBag, nameof(job.IngestMediumSettings)); logEntry.Write(job.EgestHandlerType.ToString(), nameof(job.EgestHandlerType)); logEntry.Write(job.IngestHandlerType.ToString(), nameof(job.IngestHandlerType)); logEntry.Write(loggingOptions, nameof(loggingOptions)); } logEntry.Subject = $"Process File: {fileName}"; return logEntry; }
/// <summary> /// Runs the job. /// </summary> /// <returns></returns> public async Task<string> RunJob(CancellationToken token) { var logEntry = new PledgeLog(); var client = new HttpClient(); try { client.BaseAddress = new Uri(ApiAddress); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //Check if the credentials provided for the remote job are vaild. var content = new StringContent($"grant_type=password&username={UserName}&password={Password}", Encoding.UTF8, "application/x-www-form-urlencoded"); var response = await client.PostAsync("Token", content, token); if (!response.IsSuccessStatusCode) { return response.ReasonPhrase; } //Once authenticated, add access token as header var login = await response.Content.ReadAsAsync<AuthenticationResponse>(token); var accessToken = login.AccessToken; client.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}"); //Get the config object attached to the JobId response = await client.GetAsync($"api/jobs/get/{JobId}", token); if (!response.IsSuccessStatusCode) { return response.ReasonPhrase; } var jobData = await response.Content.ReadAsAsync<JobData>(token); if (jobData == null) { return "Object Deserialization error"; } //Add LogEntry details logEntry.JobId = JobId; logEntry.ConfigId = jobData.PledgeConfiguration.Id.ToString(); logEntry.ConfigName = jobData.PledgeConfiguration.Name; logEntry.UserName = UserName; logEntry.Type = LogType.Regular; logEntry.Subject = $"Process Job: {JobId}"; logEntry.DateCreated = DateTime.UtcNow; logEntry.StartTime = DateTime.UtcNow; //Run pledge pipeline var activeJob = jobData.Job; var jobName = jobData.Job.JobName; var builder = new ModelBuilder(); var pipeline = builder.CreatePipeline(activeJob, jobData.PledgeConfiguration); var payload = pipeline.Run(token); //Add remaining LogEntry details on successful run of the job logEntry.EndTime = DateTime.UtcNow; logEntry.TotalRows = payload.PledgeResult.TotalRecordsProcessed; logEntry.PassedRows = payload.PledgeResult.TotalPassedRecordsCount; logEntry.FailedRows = payload.PledgeResult.TotalFailedRecordsCount; //Cleanup(activeJob, payload); return $"Execution of {jobName} complete"; } catch (Exception error) { //Add remaining LogEntry details on failure of the job logEntry.EndTime = DateTime.UtcNow; logEntry.Write(error); return error.Message; } finally { //Fire and Forget : Post log entry to the Audit (Web API) End-point await client.PostAsJsonAsync("api/audit/post/", logEntry); // Logging - END } }