diff --git a/server/AyaNova/Controllers/JobOperationsController.cs b/server/AyaNova/Controllers/JobOperationsController.cs index 5ce3f3cd..2a1ec939 100644 --- a/server/AyaNova/Controllers/JobOperationsController.cs +++ b/server/AyaNova/Controllers/JobOperationsController.cs @@ -134,7 +134,24 @@ namespace AyaNova.Api.Controllers } - + /// + /// Trigger a test job that simulates a (30 second) long running operation for testing and ops confirmation + /// + /// Job id + [HttpPost("test-job")] + public async Task TestWidgetJob() + { + if (!serverState.IsOpen) + return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); + if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.ServerJob)) + return StatusCode(403, new ApiNotAuthorizedResponse()); + OpsJob j = new OpsJob(); + j.Name = "TestJob"; + j.JobType = JobType.TestJob; + await JobsBiz.AddJobAsync(j, ct); + await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.ServerJob, AyaEvent.Created, $"{j.JobType} {j.Name}"), ct); + return Accepted(new { JobId = j.GId });//202 accepted + } //------------ diff --git a/server/AyaNova/Controllers/WidgetController.cs b/server/AyaNova/Controllers/WidgetController.cs index 8a067bab..b9cd8716 100644 --- a/server/AyaNova/Controllers/WidgetController.cs +++ b/server/AyaNova/Controllers/WidgetController.cs @@ -212,29 +212,7 @@ namespace AyaNova.Api.Controllers } - /// - /// Get route that submits a simulated long running operation job for testing - /// - /// Nothing - [HttpGet("testwidgetjob")] - public async Task TestWidgetJob() - { - if (!serverState.IsOpen) - return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); - - if (!Authorized.HasModifyRole(HttpContext.Items, AyaType.ServerJob)) - return StatusCode(403, new ApiNotAuthorizedResponse()); - - //Create the job here - OpsJob j = new OpsJob(); - j.Name = "TestWidgetJob"; - j.JobType = JobType.TestWidgetJob; - await JobsBiz.AddJobAsync(j, ct); - //Log - await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserIdFromContext.Id(HttpContext.Items), 0, AyaType.ServerJob, AyaEvent.Created, $"{j.JobType} {j.Name}"), ct); - - return Accepted(new { JobId = j.GId });//202 accepted - } + //------------ diff --git a/server/AyaNova/biz/JobOperationsBiz.cs b/server/AyaNova/biz/JobOperationsBiz.cs index d97da3ba..bf279655 100644 --- a/server/AyaNova/biz/JobOperationsBiz.cs +++ b/server/AyaNova/biz/JobOperationsBiz.cs @@ -10,7 +10,7 @@ namespace AyaNova.Biz { - internal class JobOperationsBiz : BizObject + internal class JobOperationsBiz : BizObject, IJobObject { internal JobOperationsBiz(AyContext dbcontext, long currentUserId, AuthorizationRoles userRoles) @@ -18,17 +18,14 @@ namespace AyaNova.Biz ct = dbcontext; UserId = currentUserId; CurrentUserRoles = userRoles; - BizType=AyaType.ServerJob; + BizType = AyaType.ServerJob; } - #region CONTROLLER ROUTES SUPPORT - //////////////////////////////////////////////////////////////////////////////////////////////// - /// GET - - //Get list of jobs + // GET + // internal async Task> GetJobListAsync() { List ret = new List(); @@ -86,27 +83,57 @@ namespace AyaNova.Biz return ret; } - //Get list of logs for job + + /////////////////////////////////////////////////////////////////////////////// + //GET LOG OF ALL JOBS FOR CLIENT + // internal async Task> GetAllJobsLogsListAsync() { List ret = new List(); - - var l = await ct.OpsJobLog - .OrderByDescending(z => z.Created) + var l = await ct.OpsJobLog + .OrderByDescending(z => z.Created) .ToListAsync(); - foreach (OpsJobLog i in l) { - JobOperationsLogInfoItem o = new JobOperationsLogInfoItem(); o.Created = i.Created; o.StatusText = i.StatusText; ret.Add(o); } - return ret; } + + + //////////////////////////////////////////////////////////////////////////////////////////////// + //JOB / OPERATIONS + // + public async Task HandleJobAsync(OpsJob job) + { + //Hand off the particular job to the corresponding processing code + //NOTE: If this code throws an exception the caller (JobsBiz::ProcessJobsAsync) will automatically set the job to failed and log the exeption so + //basically any error condition during job processing should throw up an exception if it can't be handled + switch (job.JobType) + { + + case JobType.TestJob: + await ProcessTestJobAsync(job); + break; + default: + throw new System.ArgumentOutOfRangeException($"JobOperationsBiz.HandleJob-> Invalid job type{job.JobType.ToString()}"); + } + } + + private async Task ProcessTestJobAsync(OpsJob job) + { + var sleepTime = 30 * 1000; + await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Running, ct); + await JobsBiz.LogJobAsync(job.GId, $"ProcessTestJob started, sleeping for {sleepTime} seconds...", ct); + await Task.Delay(sleepTime); + await JobsBiz.LogJobAsync(job.GId, "ProcessTestJob done sleeping setting job to finished", ct); + await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Completed, ct); + } + #endregion controller routes diff --git a/server/AyaNova/biz/JobType.cs b/server/AyaNova/biz/JobType.cs index 848bb14e..1a02f375 100644 --- a/server/AyaNova/biz/JobType.cs +++ b/server/AyaNova/biz/JobType.cs @@ -8,7 +8,7 @@ namespace AyaNova.Biz public enum JobType : int { NotSet = 0, - TestWidgetJob = 1,//test job for unit testing + TestJob = 1,//test job for unit and OPS admin testing CoreJobSweeper = 2, SeedTestData = 4, BulkCoreBizObjectOperation = 5, diff --git a/server/AyaNova/biz/JobsBiz.cs b/server/AyaNova/biz/JobsBiz.cs index a612b012..db202394 100644 --- a/server/AyaNova/biz/JobsBiz.cs +++ b/server/AyaNova/biz/JobsBiz.cs @@ -412,8 +412,8 @@ namespace AyaNova.Biz await CoreJobBackup.DoWorkAsync(ct, true); await UpdateJobStatusAsync(job.GId, JobStatus.Completed, ct); break; - case JobType.TestWidgetJob: - o = (IJobObject)BizObjectFactory.GetBizObject(AyaType.Widget, ct); + case JobType.TestJob: + o = (IJobObject)BizObjectFactory.GetBizObject(AyaType.ServerJob, ct); break; case JobType.SeedTestData: o = (IJobObject)BizObjectFactory.GetBizObject(AyaType.TrialSeeder, ct); diff --git a/server/AyaNova/biz/WidgetBiz.cs b/server/AyaNova/biz/WidgetBiz.cs index 27acb939..98e0de81 100644 --- a/server/AyaNova/biz/WidgetBiz.cs +++ b/server/AyaNova/biz/WidgetBiz.cs @@ -293,33 +293,12 @@ namespace AyaNova.Biz case JobType.BulkCoreBizObjectOperation: await ProcessBulkJobAsync(job); break; - case JobType.TestWidgetJob: - await ProcessTestJobAsync(job); - break; default: throw new System.ArgumentOutOfRangeException($"WidgetBiz.HandleJob-> Invalid job type{job.JobType.ToString()}"); } } - /// - /// /// Handle the test job - /// - /// - private async Task ProcessTestJobAsync(OpsJob job) - { - var sleepTime = 30 * 1000; - //Simulate a long running job here - await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Running, ct); - await JobsBiz.LogJobAsync(job.GId, $"Widget ProcessTestJob started, sleeping for {sleepTime} seconds...", ct); - //Uncomment this to test if the job prevents other routes from running - //result is NO it doesn't prevent other requests, so we are a-ok for now - await Task.Delay(sleepTime); - await JobsBiz.LogJobAsync(job.GId, "Widget ProcessTestJob done sleeping setting job to finished", ct); - await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Completed, ct); - - } - private async Task ProcessBulkJobAsync(OpsJob job) {