113/03/12
This commit is contained in:
127
README.md
127
README.md
@@ -66,6 +66,131 @@ GlobalConfiguration
|
||||
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
|
||||
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
|
||||
QueuePollInterval = TimeSpan.Zero,
|
||||
UseRecommendedIsolationLevel = true
|
||||
UseRecommendedIsolationLevel = true,
|
||||
DisableGlobalLocks = true, // Migration to Schema 7 is required
|
||||
});
|
||||
|
||||
// Hangfire SQLite
|
||||
GlobalConfiguration
|
||||
.Configuration
|
||||
.UseSQLiteStorage("HangfireExample.db"));
|
||||
```
|
||||
|
||||
## Server
|
||||
|
||||
啟動 Hangfire 伺服器
|
||||
|
||||
```
|
||||
services.AddHangfireServer();
|
||||
```
|
||||
|
||||
## Using Dashboard UI
|
||||
|
||||
開啟 Dashboard 功能
|
||||
|
||||
```
|
||||
app.UseHangfireDashboard();
|
||||
```
|
||||
|
||||
* Configuring Authorization
|
||||
|
||||
設定自定義驗證
|
||||
|
||||
```
|
||||
app.UseHangfireDashboard("hangfire", new DashboardOptions
|
||||
{
|
||||
Authorization = [new DashboardAuthorizationFilter()],
|
||||
});
|
||||
```
|
||||
|
||||
* Read-only view
|
||||
|
||||
設定 UI 為 ReadOnly
|
||||
|
||||
```
|
||||
app.UseHangfireDashboard("hangfire", new DashboardOptions
|
||||
{
|
||||
IsReadOnlyFunc = (DashboardContext context) => true,
|
||||
});
|
||||
```
|
||||
|
||||
* Change URL Mapping
|
||||
|
||||
修改 Dashboard UI 的預設根路由
|
||||
|
||||
```
|
||||
app.UseHangfireDashboard("hangfire");
|
||||
```
|
||||
|
||||
* Change Back to site Link
|
||||
|
||||
修改返回按鈕的路由
|
||||
|
||||
```
|
||||
app.UseHangfireDashboard("hangfire", new DashboardOptions
|
||||
{
|
||||
AppPath = "/swagger", // 導向到 Swagger UI
|
||||
});
|
||||
```
|
||||
|
||||

|
||||
|
||||
* Multiple Dashboards
|
||||
|
||||
設定不同資料來源的 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"));
|
||||
```
|
||||
|
||||
## Background Methods
|
||||
|
||||
排程可以直接執行特定方法,Hangfire 會將方法(可使用非同步)、類型與參數序列化後保存到資料庫,Hangfire 依照排程類型,分為以下四種
|
||||
|
||||
* Fire-and-Forget jobs: 將任務丟入執行佇列,直接執行
|
||||
|
||||
```
|
||||
BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-Forget"));
|
||||
```
|
||||
|
||||
* Delayed jobs: 延遲指定時間後,將任務丟入執行佇列
|
||||
|
||||
```
|
||||
BackgroundJob.Schedule(() => Console.WriteLine("Delayed"), TimeSpan.FromDays(1));
|
||||
```
|
||||
|
||||
* Recurring jobs: 依照指定間隔,將任務丟入執行佇列
|
||||
|
||||
```
|
||||
RecurringJob.AddOrUpdate("job_id", () => Console.Write("Recurring By CRON"), Cron.Daily);
|
||||
|
||||
RecurringJob.AddOrUpdate("job_id", () => Console.Write("Recurring By CRON expressions"), "0 12 * */2");
|
||||
```
|
||||
|
||||
移除已存在的排程任務
|
||||
|
||||
```
|
||||
RecurringJob.RemoveIfExists("job_id");
|
||||
```
|
||||
|
||||
手動觸發已存在的排程任務
|
||||
|
||||
```
|
||||
RecurringJob.Trigger("job_id");
|
||||
```
|
||||
|
||||
|
||||
* Continuations: 指定的任務完成後,才丟入執行佇列
|
||||
|
||||
```
|
||||
var jobId = BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-Forget"));
|
||||
|
||||
BackgroundJob.ContinueJobWith(jobid, () => Console.WriteLine("Continuations"));
|
||||
```
|
||||
Reference in New Issue
Block a user