新增方法練習顯示處理進度
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
using Hangfire;
|
using Hangfire;
|
||||||
|
using Hangfire.Console;
|
||||||
|
using Hangfire.PerformContextAccessor;
|
||||||
|
using Hangfire.Server;
|
||||||
using HangfireExample.WebService.Services;
|
using HangfireExample.WebService.Services;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
|
||||||
@@ -10,7 +12,12 @@ namespace HangfireExample.WebService.Controllers
|
|||||||
[ApiController]
|
[ApiController]
|
||||||
public class JobController : ControllerBase, IJobService
|
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")]
|
[HttpPost("Continuation", Name = "Continuation"), Produces("application/json")]
|
||||||
public string Continuation(string message, string jobId)
|
public string Continuation(string message, string jobId)
|
||||||
@@ -72,5 +79,37 @@ namespace HangfireExample.WebService.Controllers
|
|||||||
{
|
{
|
||||||
Console.WriteLine("{0}: {1}", DateTimeOffset.Now, message);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using Hangfire;
|
using Hangfire;
|
||||||
|
using Hangfire.Console;
|
||||||
using Hangfire.Dashboard;
|
using Hangfire.Dashboard;
|
||||||
|
using Hangfire.PerformContextAccessor;
|
||||||
using Hangfire.Storage.SQLite;
|
using Hangfire.Storage.SQLite;
|
||||||
using HangfireExample.WebService.Filters;
|
using HangfireExample.WebService.Filters;
|
||||||
|
|
||||||
@@ -17,9 +19,13 @@ namespace HangfireExample.WebService.Extensions
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static IServiceCollection AddHangfire(this IServiceCollection services)
|
public static IServiceCollection AddHangfire(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
// 將 Hangfire 處理序存取加入服務
|
||||||
|
services.AddHangfirePerformContextAccessor();
|
||||||
// 將 Hangfire 加入服務,使用 SQLite 作為儲存區
|
// 將 Hangfire 加入服務,使用 SQLite 作為儲存區
|
||||||
services.AddHangfire(configuration => configuration
|
services.AddHangfire(configuration => configuration
|
||||||
.UseSQLiteStorage("HangfireExample.db"));
|
.UseSQLiteStorage("HangfireExample.db")
|
||||||
|
.UsePerformContextAccessorFilter()
|
||||||
|
.UseConsole());
|
||||||
// 將 Hangfire Server 加入服務
|
// 將 Hangfire Server 加入服務
|
||||||
services.AddHangfireServer(options =>
|
services.AddHangfireServer(options =>
|
||||||
{
|
{
|
||||||
@@ -38,7 +44,7 @@ namespace HangfireExample.WebService.Extensions
|
|||||||
{
|
{
|
||||||
Authorization = [new DashboardAuthorizationFilter()],
|
Authorization = [new DashboardAuthorizationFilter()],
|
||||||
// 指定 Dashboard 指定讀取,不能操作,如重新執行、再次加入排程與刪除任務等
|
// 指定 Dashboard 指定讀取,不能操作,如重新執行、再次加入排程與刪除任務等
|
||||||
IsReadOnlyFunc = (DashboardContext context) => true,
|
IsReadOnlyFunc = (DashboardContext context) => false,
|
||||||
// 指定 Back to site 的連結
|
// 指定 Back to site 的連結
|
||||||
AppPath = "/swagger",
|
AppPath = "/swagger",
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,7 +7,9 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Bonura.Hangfire.PerformContextAccessor" Version="1.3.3" />
|
||||||
<PackageReference Include="Hangfire" Version="1.8.11" />
|
<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="Hangfire.Storage.SQLite" Version="0.4.1" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
Binary file not shown.
@@ -9,5 +9,6 @@
|
|||||||
public string Delay(string message, int seconds, int minutes = 0, int hours = 0, int days = 0);
|
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 Recurring(string message, string jobId, string expression);
|
||||||
public string Continuation(string message, string jobId);
|
public string Continuation(string message, string jobId);
|
||||||
|
public string Progress(int seconds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using Hangfire;
|
using Hangfire;
|
||||||
using Hangfire.SqlServer;
|
|
||||||
using Hangfire.Storage.SQLite;
|
using Hangfire.Storage.SQLite;
|
||||||
|
|
||||||
// 第三方擴充: Hangfire.Storage.SQLite 0.4.1
|
// 第三方擴充: Hangfire.Storage.SQLite 0.4.1
|
||||||
|
|||||||
Reference in New Issue
Block a user