using Host.Common;
using Host.Entity;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Quartz;
using System.Collections.Generic;
using System.Threading.Tasks;
using Talk.Extensions;
namespace Host.Controllers
{
///
/// 任务调度
///
[Route("api/[controller]/[Action]")]
[EnableCors("AllowSameDomain")] //允许跨域
public class JobController : Controller
{
private SchedulerCenter scheduler;
///
/// 任务调度对象
///
///
public JobController(SchedulerCenter schedulerCenter)
{
scheduler = schedulerCenter;
}
///
/// 添加任务
///
///
///
[HttpPost]
public async Task AddJob([FromBody] ScheduleEntity entity)
{
if (ConfigurationManager.GetTryConfig("EnvironmentalRestrictions", "false") == "true")
{
if (entity.TriggerType == TriggerTypeEnum.Simple &&
entity.IntervalSecond.HasValue &&
entity.IntervalSecond <= 10)
{
return new BaseResult()
{
Code = 403,
Msg = "当前环境不允许低于10秒内循环执行任务!"
};
}
else if (entity.TriggerType == TriggerTypeEnum.Cron &&
entity.Cron == "* * * * * ?")
{
return new BaseResult()
{
Code = 403,
Msg = "当前环境不允许过频繁执行任务!"
};
}
}
return await scheduler.AddScheduleJobAsync(entity);
}
///
/// 暂停任务
///
///
[HttpPost]
public async Task StopJob([FromBody] JobKey job)
{
return await scheduler.StopOrDelScheduleJobAsync(job.Group, job.Name);
}
///
/// 删除任务
///
///
[HttpPost]
public async Task RemoveJob([FromBody] JobKey job)
{
return await scheduler.StopOrDelScheduleJobAsync(job.Group, job.Name, true);
}
///
/// 恢复运行暂停的任务
///
///
[HttpPost]
public async Task ResumeJob([FromBody] JobKey job)
{
return await scheduler.ResumeJobAsync(job.Group, job.Name);
}
///
/// 查询任务
///
///
[HttpPost]
public async Task QueryJob([FromBody] JobKey job)
{
return await scheduler.QueryJobAsync(job.Group, job.Name);
}
///
/// 修改
///
///
///
[HttpPost]
public async Task ModifyJob([FromBody] ModifyJobInput entity)
{
if (ConfigurationManager.GetTryConfig("EnvironmentalRestrictions", "false") == "true")
{
if (entity.NewScheduleEntity.TriggerType == TriggerTypeEnum.Simple &&
entity.NewScheduleEntity.IntervalSecond.HasValue &&
entity.NewScheduleEntity.IntervalSecond <= 10)
{
return new BaseResult()
{
Code = 403,
Msg = "当前环境不允许低于10秒内循环执行任务!"
};
}
else if (entity.NewScheduleEntity.TriggerType == TriggerTypeEnum.Cron &&
entity.NewScheduleEntity.Cron == "* * * * * ?")
{
return new BaseResult()
{
Code = 403,
Msg = "当前环境不允许过频繁执行任务!"
};
}
}
var jobKey = new JobKey(entity.OldScheduleEntity.JobName, entity.OldScheduleEntity.JobGroup);
var runNumber = await scheduler.GetRunNumberAsync(jobKey);
await scheduler.StopOrDelScheduleJobAsync(entity.OldScheduleEntity.JobGroup, entity.OldScheduleEntity.JobName, true);
await scheduler.AddScheduleJobAsync(entity.NewScheduleEntity, runNumber);
return new BaseResult() { Msg = "修改计划任务成功!" };
}
///
/// 立即执行
///
///
///
[HttpPost]
public async Task TriggerJob([FromBody] JobKey job)
{
await scheduler.TriggerJobAsync(job);
return true;
}
///
/// 获取job日志
///
///
///
[HttpPost]
public async Task> GetJobLogs([FromBody] JobKey jobKey)
{
var logs = await scheduler.GetJobLogsAsync(jobKey);
logs?.Reverse();
return logs;
}
///
/// 启动调度
///
///
[HttpGet]
public async Task StartSchedule()
{
return await scheduler.StartScheduleAsync();
}
///
/// 停止调度
///
///
[HttpGet]
public async Task StopSchedule()
{
return await scheduler.StopScheduleAsync();
}
///
/// 获取所有任务
///
///
[HttpGet]
public async Task> GetAllJob()
{
return await scheduler.GetAllJobAsync();
}
///
/// 获取所有Job信息(简要信息 - 刷新数据的时候使用)
///
///
[HttpGet]
public async Task> GetAllJobBriefInfo()
{
return await scheduler.GetAllJobBriefInfoAsync();
}
///
/// 移除异常信息
///
///
///
[HttpPost]
public async Task RemoveErrLog([FromBody] JobKey jobKey)
{
return await scheduler.RemoveErrLog(jobKey.Group, jobKey.Name);
}
}
}