1
0

113/03/12

This commit is contained in:
2024-03-12 11:57:08 +08:00
parent 634172304f
commit 0c512bcd82
12 changed files with 308 additions and 29 deletions

View 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);
}
}
}

View File

@@ -1,5 +1,7 @@
using Hangfire;
using Hangfire.Dashboard;
using Hangfire.Storage.SQLite;
using HangfireExample.WebService.Filters;
namespace HangfireExample.WebService.Extensions
{
@@ -18,13 +20,37 @@ namespace HangfireExample.WebService.Extensions
// 將 Hangfire 加入服務,使用 SQLite 作為儲存區
services.AddHangfire(configuration => configuration
.UseSQLiteStorage("HangfireExample.db"));
// 將 Hangfire Server 加入服務
services.AddHangfireServer(options =>
{
// 指定 Hangfire Server 的名稱
options.ServerName = "HangfireExample";
// 指定 Hangfire Server 執行佇列的 Tags
options.Queues = ["default", "wb",];
});
return services;
}
public static WebApplication UseHangfireDashboard(this WebApplication app, IConfiguration configuration)
{
// HangfireDashboard 預設路由為 /Hangfire
app.UseHangfireDashboard(configuration.GetValue<string>("DashboardRoot"));
// Hangfire Dashboard 預設路由為 /Hangfire
app.UseHangfireDashboard(configuration.GetValue<string>("DashboardRoot"), new DashboardOptions
{
Authorization = [new DashboardAuthorizationFilter()],
// 指定 Dashboard 指定讀取,不能操作,如重新執行、再次加入排程與刪除任務等
IsReadOnlyFunc = (DashboardContext context) => true,
// 指定 Back to site 的連結
AppPath = "/swagger",
});
// 設定第二資料來源的 Dashboard
//app.UseHangfireDashboard(configuration.GetValue<string>("DashboardRoot"), new DashboardOptions
//{
// Authorization = [new DashboardAuthorizationFilter()],
// // 指定 Dashboard 指定讀取,不能操作,如重新執行、再次加入排程與刪除任務等
// IsReadOnlyFunc = (DashboardContext context) => true,
// // 指定 Back to site 的連結
// AppPath = "/swagger",
//}, new SQLiteStorage("HangfireExample2.db"));
return app;
}
}

View File

@@ -0,0 +1,17 @@
using Hangfire.Annotations;
using Hangfire.Dashboard;
namespace HangfireExample.WebService.Filters
{
/// <summary>
/// Hangfire Dashboard 的認證過濾中介。
/// </summary>
public class DashboardAuthorizationFilter : IDashboardAuthorizationFilter
{
public bool Authorize([NotNull] DashboardContext context)
{
// 認證時,直接回傳成功(無驗證)
return true;
}
}
}

View File

@@ -12,8 +12,4 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers\" />
</ItemGroup>
</Project>

View File

@@ -1,4 +1,24 @@
{
{
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5185"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "hangfire",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
@@ -7,25 +27,5 @@
"applicationUrl": "http://localhost:1922",
"sslPort": 0
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5185",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
}

View File

@@ -0,0 +1,13 @@
namespace HangfireExample.WebService.Services
{
/// <summary>
/// Job 服務。
/// </summary>
public interface IJobService
{
public string Fire(string message);
public string Delay(string message, int seconds, int minutes = 0, int hours = 0, int days = 0);
public string Recurring(string message, string jobId, string expression);
public string Continuation(string message, string jobId);
}
}

View File

@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"Hangfire": "HangfireExample.db"
},
"Logging": {
"LogLevel": {
"Default": "Information",