116 lines
3.9 KiB
C#
116 lines
3.9 KiB
C#
using Hangfire;
|
|
using Hangfire.Console;
|
|
using Hangfire.PerformContextAccessor;
|
|
using Hangfire.Server;
|
|
using HangfireExample.WebService.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.ComponentModel;
|
|
|
|
namespace HangfireExample.WebService.Controllers
|
|
{
|
|
[Route("Job")]
|
|
[ApiController]
|
|
public class JobController : ControllerBase, IJobService
|
|
{
|
|
public JobController(IPerformContextAccessor perform)
|
|
{
|
|
this.perform = perform;
|
|
}
|
|
|
|
private readonly IPerformContextAccessor perform;
|
|
|
|
[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);
|
|
}
|
|
|
|
[HttpPost("Progress", Name = "Progress"), Produces("application/json")]
|
|
public string Progress(int seconds)
|
|
{
|
|
return BackgroundJob.Enqueue(() => ProgressJob(perform.PerformingContext, seconds));
|
|
}
|
|
|
|
[NonAction]
|
|
[DisplayName("Progress, Seconds = {1}")]
|
|
public void ProgressJob(PerformContext context, int seconds)
|
|
{
|
|
var bar = context.WriteProgressBar();
|
|
|
|
context.WriteLine("The text color change to red");
|
|
|
|
context.SetTextColor(ConsoleTextColor.Red);
|
|
|
|
context.WriteLine("This progress is ready to start");
|
|
|
|
context.ResetTextColor();
|
|
|
|
foreach (var i in Enumerable.Range(0, seconds).ToList().WithProgress(bar))
|
|
{
|
|
Thread.Sleep(1000);
|
|
}
|
|
|
|
context.SetTextColor(ConsoleTextColor.Green);
|
|
|
|
context.WriteLine("This progress has finished");
|
|
|
|
context.ResetTextColor();
|
|
}
|
|
}
|
|
}
|