113/03/12
This commit is contained in:
76
HangfireExample.WebService/Controllers/JobController.cs
Normal file
76
HangfireExample.WebService/Controllers/JobController.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using Hangfire;
|
||||
using HangfireExample.WebService.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace HangfireExample.WebService.Controllers
|
||||
{
|
||||
[Route("Job")]
|
||||
[ApiController]
|
||||
public class JobController : ControllerBase, IJobService
|
||||
{
|
||||
public JobController() { }
|
||||
|
||||
[HttpPost("Continuation", Name = "Continuation"), Produces("application/json")]
|
||||
public string Continuation(string message, string jobId)
|
||||
{
|
||||
return BackgroundJob.ContinueJobWith(jobId, () => ContinuationJob(message, jobId));
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
[DisplayName("Continuation, Job Id = {1}")]
|
||||
public static void ContinuationJob(string message, string jobId)
|
||||
{
|
||||
Console.WriteLine("{0}: {1}", DateTimeOffset.Now, message);
|
||||
}
|
||||
|
||||
[HttpPost("Delay", Name = "Delay"), Produces("application/json")]
|
||||
public string Delay(string message, int seconds, int minutes = 0, int hours = 0, int days = 0)
|
||||
{
|
||||
TimeSpan delay =
|
||||
seconds > 0 ? TimeSpan.FromSeconds(seconds) :
|
||||
minutes > 0 ? TimeSpan.FromMinutes(minutes) :
|
||||
hours > 0 ? TimeSpan.FromHours(hours) :
|
||||
days > 0 ? TimeSpan.FromDays(days) : throw new Exception("未指定延遲時間");
|
||||
return BackgroundJob.Schedule(() => DelayJob(message, seconds, minutes, hours, days), delay);
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
[DisplayName("Delay, Day = {4}, Hour = {3}, Minute = {2}, Second = {1}")]
|
||||
public static void DelayJob(string message, int seconds, int minutes = 0, int hours = 0, int days = 0)
|
||||
{
|
||||
Console.WriteLine("{0}: {1}", DateTimeOffset.Now, message);
|
||||
}
|
||||
|
||||
[HttpPost("Fire", Name = "Fire"), Produces("application/json")]
|
||||
public string Fire(string message)
|
||||
{
|
||||
return BackgroundJob.Enqueue(() => FireJob(message));
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
[DisplayName("Fire")]
|
||||
public static void FireJob(string message)
|
||||
{
|
||||
Console.WriteLine("{0}: {1}", DateTimeOffset.Now, message);
|
||||
}
|
||||
|
||||
[HttpPost("Recurring", Name = "Recurring"), Produces("application/json")]
|
||||
public string Recurring(string message, string jobId, string expression)
|
||||
{
|
||||
RecurringJob.RemoveIfExists(jobId);
|
||||
|
||||
RecurringJob.AddOrUpdate(jobId, () => _RecurringJob(message, jobId, expression), expression);
|
||||
|
||||
return jobId;
|
||||
}
|
||||
|
||||
[NonAction]
|
||||
[DisplayName("Recurring, JobId = {1}, Cron expression = {2}")]
|
||||
public static void _RecurringJob(string message, string jobId, string expression)
|
||||
{
|
||||
Console.WriteLine("{0}: {1}", DateTimeOffset.Now, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user