1
0

新增方法練習顯示處理進度

This commit is contained in:
2024-03-18 10:43:42 +08:00
parent ea4e5da703
commit e55b957028
6 changed files with 52 additions and 5 deletions

View File

@@ -1,6 +1,8 @@
using Hangfire;
using Hangfire.Console;
using Hangfire.PerformContextAccessor;
using Hangfire.Server;
using HangfireExample.WebService.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel;
@@ -10,7 +12,12 @@ namespace HangfireExample.WebService.Controllers
[ApiController]
public class JobController : ControllerBase, IJobService
{
public JobController() { }
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)
@@ -72,5 +79,37 @@ namespace HangfireExample.WebService.Controllers
{
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();
}
}
}

View File

@@ -1,5 +1,7 @@
using Hangfire;
using Hangfire.Console;
using Hangfire.Dashboard;
using Hangfire.PerformContextAccessor;
using Hangfire.Storage.SQLite;
using HangfireExample.WebService.Filters;
@@ -17,9 +19,13 @@ namespace HangfireExample.WebService.Extensions
/// <returns></returns>
public static IServiceCollection AddHangfire(this IServiceCollection services)
{
// 將 Hangfire 處理序存取加入服務
services.AddHangfirePerformContextAccessor();
// 將 Hangfire 加入服務,使用 SQLite 作為儲存區
services.AddHangfire(configuration => configuration
.UseSQLiteStorage("HangfireExample.db"));
.UseSQLiteStorage("HangfireExample.db")
.UsePerformContextAccessorFilter()
.UseConsole());
// 將 Hangfire Server 加入服務
services.AddHangfireServer(options =>
{
@@ -38,7 +44,7 @@ namespace HangfireExample.WebService.Extensions
{
Authorization = [new DashboardAuthorizationFilter()],
// 指定 Dashboard 指定讀取,不能操作,如重新執行、再次加入排程與刪除任務等
IsReadOnlyFunc = (DashboardContext context) => true,
IsReadOnlyFunc = (DashboardContext context) => false,
// 指定 Back to site 的連結
AppPath = "/swagger",
});

View File

@@ -7,7 +7,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bonura.Hangfire.PerformContextAccessor" Version="1.3.3" />
<PackageReference Include="Hangfire" Version="1.8.11" />
<PackageReference Include="Hangfire.Console" Version="1.4.3" />
<PackageReference Include="Hangfire.Storage.SQLite" Version="0.4.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

View File

@@ -9,5 +9,6 @@
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);
public string Progress(int seconds);
}
}