| | |
| | | [Route("api/[controller]")] |
| | | public class TaskBackGroundController : Controller |
| | | { |
| | | //定义全局信息返回变量 |
| | | ToMessage mes = new ToMessage(); |
| | | private readonly ISchedulerFactory _schedulerFactory; |
| | | private readonly IJobFactory _jobFactory; |
| | | public TaskBackGroundController(ISchedulerFactory schedulerFactory, IJobFactory jobFactory) |
| | | private readonly QuartzRepository _quartzRepo; |
| | | |
| | | public TaskBackGroundController(ISchedulerFactory schedulerFactory, |
| | | IJobFactory jobFactory, |
| | | QuartzRepository quartzRepo) |
| | | { |
| | | this._jobFactory = jobFactory; |
| | | this._schedulerFactory = schedulerFactory; |
| | | _schedulerFactory = schedulerFactory; |
| | | _jobFactory = jobFactory; |
| | | _quartzRepo = quartzRepo; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取所有的作业 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | [Route(template: "GetJobs")] |
| | | [Route("GetJobs")] |
| | | [HttpGet] |
| | | public async Task<IActionResult> GetJobs() |
| | | { |
| | | try |
| | | { |
| | | mes.code = "200"; |
| | | mes.message = "查询成功!"; |
| | | mes.data = await _schedulerFactory.GetJobs(); |
| | | mes.data = await _schedulerFactory.GetJobs(_quartzRepo); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | mes.code = "300"; |
| | | mes.message = "查询失败:" + ex.Message; |
| | | mes.data = null; |
| | | } |
| | | return Json(mes); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取作业运行日志 |
| | | /// </summary> |
| | | /// <param name="taskName"></param> |
| | | /// <param name="groupName"></param> |
| | | /// <param name="page"></param> |
| | | /// <returns></returns> |
| | | [Route(template: "GetRunLog")] |
| | | [Route("GetRunLog")] |
| | | [HttpGet] |
| | | public IActionResult GetRunLog(string taskName, string groupName, int page = 1) |
| | | public async Task<IActionResult> GetRunLog(string taskName, string groupName, int page = 1) |
| | | { |
| | | // 入参校验 |
| | | if (string.IsNullOrEmpty(taskName) || string.IsNullOrEmpty(groupName)) |
| | | { |
| | | mes.code = "300"; |
| | | mes.message = "任务名和分组名不能为空!"; |
| | | mes.data = null; |
| | | return Json(mes); |
| | | } |
| | | try |
| | | { |
| | | mes.code = "200"; |
| | | mes.data = FileQuartz.GetJobRunLog(taskName, groupName, page); |
| | | mes.message = "查询成功!"; |
| | | mes.data = await _quartzRepo.GetJobRunLogAsync(taskName, groupName, page); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | mes.code = "300"; |
| | | mes.message = "查询失败:" + ex.Message; |
| | | mes.data = null; |
| | | } |
| | | return Json(mes); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 添加任务 |
| | | /// </summary> |
| | | /// <param name="taskOptions"></param> |
| | | /// <returns></returns> |
| | | [Route(template: "Add")] |
| | | [Route("Add")] |
| | | [HttpPost] |
| | | //[TaskAuthor] |
| | | public async Task<IActionResult> Add(TaskOptions taskOptions) |
| | | { |
| | | return Json(await taskOptions.AddJob(_schedulerFactory, jobFactory: _jobFactory)); |
| | | if (string.IsNullOrEmpty(taskOptions.TaskName) || string.IsNullOrEmpty(taskOptions.GroupName)) |
| | | { |
| | | mes.code = "300"; |
| | | mes.message = "任务名和分组名不能为空!"; |
| | | return Json(mes); |
| | | } |
| | | return Json(await taskOptions.AddJob(_schedulerFactory, false, _jobFactory, _quartzRepo)); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 删除任务 |
| | | /// </summary> |
| | | /// <param name="taskOptions"></param> |
| | | /// <returns></returns> |
| | | [Route(template: "Remove")] |
| | | [Route("Remove")] |
| | | [HttpPost] |
| | | //[TaskAuthor] |
| | | public async Task<IActionResult> Remove(TaskOptions taskOptions) |
| | | { |
| | | return Json(await _schedulerFactory.Remove(taskOptions)); |
| | | if (string.IsNullOrEmpty(taskOptions.TaskName) || string.IsNullOrEmpty(taskOptions.GroupName)) |
| | | { |
| | | mes.code = "300"; |
| | | mes.message = "任务名和分组名不能为空!"; |
| | | return Json(mes); |
| | | } |
| | | return Json(await _schedulerFactory.Remove(taskOptions, _quartzRepo)); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 修改任务 |
| | | /// </summary> |
| | | /// <param name="taskOptions"></param> |
| | | /// <returns></returns> |
| | | [Route(template: "Update")] |
| | | [Route("Update")] |
| | | [HttpPost] |
| | | //[TaskAuthor] |
| | | public async Task<IActionResult> Update(TaskOptions taskOptions) |
| | | { |
| | | return Json(await _schedulerFactory.Update(taskOptions)); |
| | | if (string.IsNullOrEmpty(taskOptions.TaskName) || string.IsNullOrEmpty(taskOptions.GroupName)) |
| | | { |
| | | mes.code = "300"; |
| | | mes.message = "任务名和分组名不能为空!"; |
| | | return Json(mes); |
| | | } |
| | | return Json(await _schedulerFactory.Update(taskOptions, _quartzRepo)); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 暂停任务 |
| | | /// </summary> |
| | | /// <param name="taskOptions"></param> |
| | | /// <returns></returns> |
| | | [Route(template: "Pause")] |
| | | [Route("Pause")] |
| | | [HttpPost] |
| | | //[TaskAuthor] |
| | | public async Task<IActionResult> Pause(TaskOptions taskOptions) |
| | | { |
| | | return Json(await _schedulerFactory.Pause(taskOptions)); |
| | | if (string.IsNullOrEmpty(taskOptions.TaskName) || string.IsNullOrEmpty(taskOptions.GroupName)) |
| | | { |
| | | mes.code = "300"; |
| | | mes.message = "任务名和分组名不能为空!"; |
| | | return Json(mes); |
| | | } |
| | | // 校验任务是否存在 |
| | | if (!await _quartzRepo.TaskExistsAsync(taskOptions.TaskName, taskOptions.GroupName)) |
| | | { |
| | | mes.code = "300"; |
| | | mes.message = "任务不存在!"; |
| | | return Json(mes); |
| | | } |
| | | return Json(await _schedulerFactory.Pause(taskOptions, _quartzRepo)); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 开启任务 |
| | | /// </summary> |
| | | /// <param name="taskOptions"></param> |
| | | /// <returns></returns> |
| | | [Route(template: "Start")] |
| | | [Route("Start")] |
| | | [HttpPost] |
| | | //[TaskAuthor] |
| | | public async Task<IActionResult> Start(TaskOptions taskOptions) |
| | | { |
| | | return Json(await _schedulerFactory.Start(taskOptions)); |
| | | if (string.IsNullOrEmpty(taskOptions.TaskName) || string.IsNullOrEmpty(taskOptions.GroupName)) |
| | | { |
| | | mes.code = "300"; |
| | | mes.message = "任务名和分组名不能为空!"; |
| | | return Json(mes); |
| | | } |
| | | if (!await _quartzRepo.TaskExistsAsync(taskOptions.TaskName, taskOptions.GroupName)) |
| | | { |
| | | mes.code = "300"; |
| | | mes.message = "任务不存在!"; |
| | | return Json(mes); |
| | | } |
| | | return Json(await _schedulerFactory.Start(taskOptions, _quartzRepo)); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 立即执行 |
| | | /// </summary> |
| | | /// <param name="taskOptions"></param> |
| | | /// <returns></returns> |
| | | [Route(template: "Run")] |
| | | [Route("Run")] |
| | | [HttpPost] |
| | | //[TaskAuthor] |
| | | public async Task<IActionResult> Run(TaskOptions taskOptions) |
| | | { |
| | | return Json(await _schedulerFactory.Run(taskOptions)); |
| | | if (string.IsNullOrEmpty(taskOptions.TaskName) || string.IsNullOrEmpty(taskOptions.GroupName)) |
| | | { |
| | | mes.code = "300"; |
| | | mes.message = "任务名和分组名不能为空!"; |
| | | return Json(mes); |
| | | } |
| | | if (!await _quartzRepo.TaskExistsAsync(taskOptions.TaskName, taskOptions.GroupName)) |
| | | { |
| | | mes.code = "300"; |
| | | mes.message = "任务不存在!"; |
| | | return Json(mes); |
| | | } |
| | | return Json(await _schedulerFactory.Run(taskOptions, _quartzRepo)); |
| | | } |
| | | } |
| | | } |