completed background job scheduler
This commit is contained in:
		
							
								
								
									
										105
									
								
								Oqtane.Server/Controllers/JobController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										105
									
								
								Oqtane.Server/Controllers/JobController.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,105 @@ | ||||
| using System.Collections.Generic; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Oqtane.Repository; | ||||
| using Oqtane.Models; | ||||
| using Oqtane.Shared; | ||||
| using Oqtane.Infrastructure; | ||||
| using System; | ||||
| using Microsoft.Extensions.Hosting; | ||||
| using Microsoft.Extensions.DependencyInjection; | ||||
|  | ||||
| namespace Oqtane.Controllers | ||||
| { | ||||
|     [Route("{site}/api/[controller]")] | ||||
|     public class JobController : Controller | ||||
|     { | ||||
|         private readonly IJobRepository Jobs; | ||||
|         private readonly ILogManager logger; | ||||
|         private readonly IServiceProvider ServiceProvider; | ||||
|  | ||||
|         public JobController(IJobRepository Jobs, ILogManager logger, IServiceProvider ServiceProvider) | ||||
|         { | ||||
|             this.Jobs = Jobs; | ||||
|             this.logger = logger; | ||||
|             this.ServiceProvider = ServiceProvider; | ||||
|         } | ||||
|  | ||||
|         // GET: api/<controller> | ||||
|         [HttpGet] | ||||
|         public IEnumerable<Job> Get() | ||||
|         { | ||||
|             return Jobs.GetJobs(); | ||||
|         } | ||||
|  | ||||
|         // GET api/<controller>/5 | ||||
|         [HttpGet("{id}")] | ||||
|         public Job Get(int id) | ||||
|         { | ||||
|             return Jobs.GetJob(id); | ||||
|         } | ||||
|  | ||||
|         // POST api/<controller> | ||||
|         [HttpPost] | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public Job Post([FromBody] Job Job) | ||||
|         { | ||||
|             if (ModelState.IsValid) | ||||
|             { | ||||
|                 Job = Jobs.AddJob(Job); | ||||
|                 logger.Log(LogLevel.Information, this, LogFunction.Create, "Job Added {Job}", Job); | ||||
|             } | ||||
|             return Job; | ||||
|         } | ||||
|  | ||||
|         // PUT api/<controller>/5 | ||||
|         [HttpPut("{id}")] | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public Job Put(int id, [FromBody] Job Job) | ||||
|         { | ||||
|             if (ModelState.IsValid) | ||||
|             { | ||||
|                 Job = Jobs.UpdateJob(Job); | ||||
|                 logger.Log(LogLevel.Information, this, LogFunction.Update, "Job Updated {Job}", Job); | ||||
|             } | ||||
|             return Job; | ||||
|         } | ||||
|  | ||||
|         // DELETE api/<controller>/5 | ||||
|         [HttpDelete("{id}")] | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public void Delete(int id) | ||||
|         { | ||||
|             Jobs.DeleteJob(id); | ||||
|             logger.Log(LogLevel.Information, this, LogFunction.Delete, "Job Deleted {JobId}", id); | ||||
|         } | ||||
|  | ||||
|         // GET api/<controller>/start | ||||
|         [HttpGet("start/{id}")] | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public void Start(int id) | ||||
|         { | ||||
|             Job job = Jobs.GetJob(id); | ||||
|             Type jobtype = Type.GetType(job.JobType); | ||||
|             if (jobtype != null) | ||||
|             { | ||||
|                 var jobobject = ActivatorUtilities.CreateInstance(ServiceProvider, jobtype); | ||||
|                 ((IHostedService)jobobject).StartAsync(new System.Threading.CancellationToken()); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         // GET api/<controller>/stop | ||||
|         [HttpGet("stop/{id}")] | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public void Stop(int id) | ||||
|         { | ||||
|             Job job = Jobs.GetJob(id); | ||||
|             Type jobtype = Type.GetType(job.JobType); | ||||
|             if (jobtype != null) | ||||
|             { | ||||
|                 var jobobject = ActivatorUtilities.CreateInstance(ServiceProvider, jobtype); | ||||
|                 ((IHostedService)jobobject).StopAsync(new System.Threading.CancellationToken()); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -9,55 +9,57 @@ using Oqtane.Infrastructure; | ||||
| namespace Oqtane.Controllers | ||||
| { | ||||
|     [Route("{site}/api/[controller]")]
 | ||||
|     public class ScheduleController : Controller | ||||
|     public class JobLogController : Controller | ||||
|     { | ||||
|         private readonly IScheduleRepository Schedules; | ||||
|         private readonly IJobLogRepository JobLogs; | ||||
|         private readonly ILogManager logger; | ||||
| 
 | ||||
|         public ScheduleController(IScheduleRepository Schedules, ILogManager logger) | ||||
|         public JobLogController(IJobLogRepository JobLogs, ILogManager logger) | ||||
|         { | ||||
|             this.Schedules = Schedules; | ||||
|             this.JobLogs = JobLogs; | ||||
|             this.logger = logger; | ||||
|         } | ||||
| 
 | ||||
|         // GET: api/<controller> | ||||
|         [HttpGet] | ||||
|         public IEnumerable<Schedule> Get() | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public IEnumerable<JobLog> Get() | ||||
|         { | ||||
|             return Schedules.GetSchedules(); | ||||
|             return JobLogs.GetJobLogs(); | ||||
|         } | ||||
| 
 | ||||
|         // GET api/<controller>/5 | ||||
|         [HttpGet("{id}")] | ||||
|         public Schedule Get(int id) | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public JobLog Get(int id) | ||||
|         { | ||||
|             return Schedules.GetSchedule(id); | ||||
|             return JobLogs.GetJobLog(id); | ||||
|         } | ||||
| 
 | ||||
|         // POST api/<controller> | ||||
|         [HttpPost] | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public Schedule Post([FromBody] Schedule Schedule) | ||||
|         public JobLog Post([FromBody] JobLog JobLog) | ||||
|         { | ||||
|             if (ModelState.IsValid) | ||||
|             { | ||||
|                 Schedule = Schedules.AddSchedule(Schedule); | ||||
|                 logger.Log(LogLevel.Information, this, LogFunction.Create, "Schedule Added {Schedule}", Schedule); | ||||
|                 JobLog = JobLogs.AddJobLog(JobLog); | ||||
|                 logger.Log(LogLevel.Information, this, LogFunction.Create, "Job Log Added {JobLog}", JobLog); | ||||
|             } | ||||
|             return Schedule; | ||||
|             return JobLog; | ||||
|         } | ||||
| 
 | ||||
|         // PUT api/<controller>/5 | ||||
|         [HttpPut("{id}")] | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public Schedule Put(int id, [FromBody] Schedule Schedule) | ||||
|         public JobLog Put(int id, [FromBody] JobLog JobLog) | ||||
|         { | ||||
|             if (ModelState.IsValid) | ||||
|             { | ||||
|                 Schedule = Schedules.UpdateSchedule(Schedule); | ||||
|                 logger.Log(LogLevel.Information, this, LogFunction.Update, "Schedule Updated {Schedule}", Schedule); | ||||
|                 JobLog = JobLogs.UpdateJobLog(JobLog); | ||||
|                 logger.Log(LogLevel.Information, this, LogFunction.Update, "Job Log Updated {JobLog}", JobLog); | ||||
|             } | ||||
|             return Schedule; | ||||
|             return JobLog; | ||||
|         } | ||||
| 
 | ||||
|         // DELETE api/<controller>/5 | ||||
| @ -65,8 +67,8 @@ namespace Oqtane.Controllers | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public void Delete(int id) | ||||
|         { | ||||
|             Schedules.DeleteSchedule(id); | ||||
|             logger.Log(LogLevel.Information, this, LogFunction.Delete, "Schedule Deleted {ScheduleId}", id); | ||||
|             JobLogs.DeleteJobLog(id); | ||||
|             logger.Log(LogLevel.Information, this, LogFunction.Delete, "Job Log Deleted {JobLogId}", id); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -1,74 +0,0 @@ | ||||
| using System.Collections.Generic; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Oqtane.Repository; | ||||
| using Oqtane.Models; | ||||
| using Oqtane.Shared; | ||||
| using Oqtane.Infrastructure; | ||||
|  | ||||
| namespace Oqtane.Controllers | ||||
| { | ||||
|     [Route("{site}/api/[controller]")] | ||||
|     public class ScheduleLogController : Controller | ||||
|     { | ||||
|         private readonly IScheduleLogRepository ScheduleLogs; | ||||
|         private readonly ILogManager logger; | ||||
|  | ||||
|         public ScheduleLogController(IScheduleLogRepository ScheduleLogs, ILogManager logger) | ||||
|         { | ||||
|             this.ScheduleLogs = ScheduleLogs; | ||||
|             this.logger = logger; | ||||
|         } | ||||
|  | ||||
|         // GET: api/<controller> | ||||
|         [HttpGet] | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public IEnumerable<ScheduleLog> Get() | ||||
|         { | ||||
|             return ScheduleLogs.GetScheduleLogs(); | ||||
|         } | ||||
|  | ||||
|         // GET api/<controller>/5 | ||||
|         [HttpGet("{id}")] | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public ScheduleLog Get(int id) | ||||
|         { | ||||
|             return ScheduleLogs.GetScheduleLog(id); | ||||
|         } | ||||
|  | ||||
|         // POST api/<controller> | ||||
|         [HttpPost] | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public ScheduleLog Post([FromBody] ScheduleLog ScheduleLog) | ||||
|         { | ||||
|             if (ModelState.IsValid) | ||||
|             { | ||||
|                 ScheduleLog = ScheduleLogs.AddScheduleLog(ScheduleLog); | ||||
|                 logger.Log(LogLevel.Information, this, LogFunction.Create, "Schedule Log Added {ScheduleLog}", ScheduleLog); | ||||
|             } | ||||
|             return ScheduleLog; | ||||
|         } | ||||
|  | ||||
|         // PUT api/<controller>/5 | ||||
|         [HttpPut("{id}")] | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public ScheduleLog Put(int id, [FromBody] ScheduleLog ScheduleLog) | ||||
|         { | ||||
|             if (ModelState.IsValid) | ||||
|             { | ||||
|                 ScheduleLog = ScheduleLogs.UpdateScheduleLog(ScheduleLog); | ||||
|                 logger.Log(LogLevel.Information, this, LogFunction.Update, "Schedule Log Updated {ScheduleLog}", ScheduleLog); | ||||
|             } | ||||
|             return ScheduleLog; | ||||
|         } | ||||
|  | ||||
|         // DELETE api/<controller>/5 | ||||
|         [HttpDelete("{id}")] | ||||
|         [Authorize(Roles = Constants.HostRole)] | ||||
|         public void Delete(int id) | ||||
|         { | ||||
|             ScheduleLogs.DeleteScheduleLog(id); | ||||
|             logger.Log(LogLevel.Information, this, LogFunction.Delete, "Schedule Log Deleted {ScheduleLogId}", id); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Shaun Walker
					Shaun Walker