.net core 基于Hangfire+Mysql持久化實現(xiàn)定時任務(wù)配置方法
1.negut引入hangfire相關(guān)包
Hangfire.AspNetCore,Hangfire.Core,Hangfire.Dashboard.BasicAuthorization,Hangfire.MySqlStorage

2.Appsetting 配置hangfire資源
"HangFire": {
"Connection": "Server=127.0.0.1;uid=root;pwd=wakamysql666;database=Hangfire_DB;AllowLoadLocalInfile=true;Allow User Variables=True;",
"pathMatch": "/hangfire",
"Login": "login",
"PasswordClear": "pwd"
},
3.自定義擴(kuò)展類
/// <summary>
/// 任務(wù)調(diào)度
/// </summary>
public static class HangfireSetup
{
public static void AddHangfireSetup(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
if (services == null) throw new ArgumentNullException(nameof(services));
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)//此方法 只初次創(chuàng)建數(shù)據(jù)庫使用即可
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseStorage(new MySqlStorage(Appsettings.app("HangFire", "Connection"), new MySqlStorageOptions
{
TransactionIsolationLevel =
(IsolationLevel?) System.Data.IsolationLevel.ReadCommitted, //事務(wù)隔離級別。默認(rèn)是讀取已提交
QueuePollInterval = TimeSpan.FromSeconds(15), //- 作業(yè)隊列輪詢間隔。默認(rèn)值為15秒。
JobExpirationCheckInterval = TimeSpan.FromHours(1),
CountersAggregateInterval = TimeSpan.FromMinutes(5),
PrepareSchemaIfNecessary = false, // 如果設(shè)置為true,則創(chuàng)建數(shù)據(jù)庫表。默認(rèn)是true
DashboardJobListLimit = 50000,
TransactionTimeout = TimeSpan.FromMinutes(1),
TablesPrefix = "Hangfire"
})));
services.AddHangfireServer();
}
}
4.在startupConfigureServices注入擴(kuò)展
services.AddHangfireSetup();//任務(wù)調(diào)度

5.配置MIddleware
//任務(wù)調(diào)度中間件
public static class HangfireMiddleware
{
public static void UseHangfireMiddleware(this IApplicationBuilder app)
{
if (app == null) throw new ArgumentNullException(nameof(app));
app.UseHangfireServer(); //配置服務(wù)//ConfigureOptions()
app.UseHangfireDashboard(Appsettings.app("HangFire", "pathMatch"), HfAuthor()); //配置面板
//BackgroundJob.Enqueue(() => Console.WriteLine("Hello world from Hangfire!"));
HangfireService(); //配置各個任務(wù)
}
/// <summary>
/// 配置賬號模板信息
/// </summary>
/// <returns></returns>
public static DashboardOptions HfAuthor()
{
var filter = new BasicAuthAuthorizationFilter(
new BasicAuthAuthorizationFilterOptions
{
SslRedirect = false,
RequireSsl = false,
LoginCaseSensitive = false,
Users = new[]
{
new BasicAuthAuthorizationUser
{
Login = Appsettings.app("HangFire", "Login"), //可視化的登陸賬號
PasswordClear = Appsettings.app("HangFire", "PasswordClear") //可視化的密碼
}
}
});
return new DashboardOptions
{
Authorization = new[] {filter}
};
}
/// <summary>
/// 配置啟動
/// </summary>
/// <returns></returns>
public static BackgroundJobServerOptions ConfigureOptions()
{
return new()
{
Queues = new[] {"Job", nameof(HangfireConfigureQueue.picturetooss)}, //隊列名稱,只能為小寫
WorkerCount = Environment.ProcessorCount * 5, //并發(fā)任務(wù)
ServerName = "HangfireServer" //代表服務(wù)名稱
};
}
#region 配置服務(wù)
public static void HangfireService()
{
// "0 0 1 * * ? " 每天凌晨一點執(zhí)行阿里云OSS
RecurringJob.AddOrUpdate<IOrderItemInfoService>(_ => _.JobOSS(), "0 0 1 * * ? ", TimeZoneInfo.Local,
nameof(HangfireConfigureQueue.picturetooss));
// "0 0 1 * * ? " 每天早上七點執(zhí)行定時任務(wù)更新匯率
RecurringJob.AddOrUpdate<ICurrencyInfosService>(_ => _.UpdateRateByJob(), "0 0 7 * * ? ",
TimeZoneInfo.Local, nameof(HangfireConfigureQueue.picturetooss));
}
#endregion
}
6.startupConfigure配置使用中間件
app.UseHangfireMiddleware();//Job
效果圖:


結(jié)語:到此hangfire實現(xiàn)定時任務(wù)的配置已經(jīng)全部完成。
到此這篇關(guān)于.net core 基于Hangfire+Mysql持久化實現(xiàn)定時任務(wù)的文章就介紹到這了,更多相關(guān).net core Hangfire定時任務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
asp.net Context.Handler 頁面間傳值方法
很有用的頁面間傳值方法(Context.Handler),使用說明2008-08-08
一個Asp.Net的顯示分頁方法 附加實體轉(zhuǎn)換和存儲過程 帶源碼下載
現(xiàn)在自己寫的webform都不用服務(wù)器控件了,所以自己仿照aspnetpager寫了一個精簡實用的返回分頁顯示的html方法,其他話不說了,直接上代碼2012-10-10
.Net彈性和瞬態(tài)故障處理庫Polly實現(xiàn)彈性策略
這篇文章介紹了.Net彈性和瞬態(tài)故障處理庫Polly實現(xiàn)彈性策略的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
.NET5控制臺程序使用EF連接MYSQL數(shù)據(jù)庫的方法
這篇文章主要介紹了.NET5控制臺程序使用EF連接MYSQL數(shù)據(jù)庫,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08
asp.net 在客戶端顯示服務(wù)器端任務(wù)處理進(jìn)度條的探討
由于 HTTP 協(xié)議本身的無狀態(tài)性,B/S結(jié)構(gòu)的程序無法像C/S程序那樣,實時顯示程序處理的進(jìn)度。搜索一下網(wǎng)上,一般都是采用靜態(tài)變量保存程序執(zhí)行進(jìn)度的方法實現(xiàn),但是,這種方法是完全錯誤的,在并發(fā)的情況下,多個用戶訪問一個程序,會造成混亂。2009-09-09
asp.net mvc4 mysql制作簡單分頁組件(部分視圖)
這篇文章主要介紹了asp.net mvc4 mysql制作簡單分頁組件,附部分視圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

