コード例 #1
0
ファイル: Invoker.cs プロジェクト: tuvoksg1/RulesEngine
        /// <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
            }
        }