You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB
C#

2 months ago
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Quartz;
namespace SlnMesnac.Controllers
{
/// <summary>
/// 任务调度
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class JobController
{
private readonly IScheduler _scheduler;
/// <summary>
///
/// </summary>
/// <param name="scheduler"></param>
public JobController(IScheduler scheduler)
{
_scheduler = scheduler;
}
/// <summary>
/// 启动任务
/// </summary>
/// <param name="jobName">任务名称</param>
/// <param name="groupName">任务分组</param>
/// <returns></returns>
[HttpPost("start")]
public async Task<string> StartJob(string jobName, string groupName)
{
// 检查调度器是否已经启动
if (!_scheduler.IsStarted)
{
await _scheduler.Start();
}
var myJobKey = new JobKey(jobName, groupName);
await _scheduler.ResumeJob(myJobKey);
return "Job started successfully.";
}
/// <summary>
/// 停止任务
/// </summary>
/// <param name="jobName">任务名称</param>
/// <param name="groupName">任务分组</param>
/// <returns></returns>
[HttpPost("stop")]
public async Task<string> StopJob(string jobName, string groupName)
{
var myJobKey = new JobKey(jobName, groupName);
await _scheduler.PauseJob(myJobKey);
return "Job stopped successfully.";
}
}
}