日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

hangfire支持mysql_快速入门 - Hangfire.HttpJob 中文文档

發(fā)布時間:2024/1/8 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hangfire支持mysql_快速入门 - Hangfire.HttpJob 中文文档 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

先介紹下 Hangfire.HttpJob 自身引用的組件

Hangfire.AspNetCore (因為Hangfire.HttpJob是作為Hangfire的插件,Hangfire.AspNetCore是Hangfire的核心組件)

Hangfire.Console (Hangfire.Console是Hangfire的Dashbord的一個插件,記錄job運行日志的)

Hangfire.Tags (這個是Hangfire的Dashbord的一個插件,把運行的job按照名稱進行分組在dashbord里面方便查看)

HttpClientFactory (由于Hangfire.HttpJob是用web HttpClient的方式進行job調用的,所以使用我開源的HttpCientFactory組件可以規(guī)避HttpCliet使用上的一些坑點)

MailKit (Hangfire.HttpJob支持job運行失敗或者成功發(fā)送郵件通知)

Newtonsoft.Json (這個不用介紹了)

這里測試用Mysql作為hangfire的存儲。如果用SqlServer可以參考wiki的介紹

nuget 引用如下包:

Hangfire.HttpJob

Hangfire.Tags.Mysql (這個是我開源的一個針對Hangfire.Tags的mysql存儲)

Hangfire.MySql.Core (這個是Hangfire本身的mysql存儲)

Hangfire.Dashboard.BasicAuthorization (這個是Hangfire的auth認證組件)

我這里用Nlog記錄日志

NLog.Extensions.Logging

public class Startup{

public void ConfigureServices(IServiceCollection services)

{

services.AddHangfire(Configuration);//Configuration是下面的方法

}

private void Configuration(IGlobalConfiguration globalConfiguration)

{

var mysqlOption = new MySqlStorageOptions

{

TransactionIsolationLevel = IsolationLevel.ReadCommitted,

QueuePollInterval = TimeSpan.FromSeconds(15),

JobExpirationCheckInterval = TimeSpan.FromHours(1),

CountersAggregateInterval = TimeSpan.FromMinutes(5),

PrepareSchemaIfNecessary = true,

DashboardJobListLimit = 50000,

TransactionTimeout = TimeSpan.FromMinutes(1),

TablePrefix = "hangfire"

}

globalConfiguration.UseStorage(new MySqlStorage("HangfireMysqlConnectionString",mysqlOption))

.UseConsole(new ConsoleOptions()

{

BackgroundColor = "#000079"

})

.UseHangfireHttpJob(new HangfireHttpJobOptions

{

//你如果不使用這個使用釘釘也是可以的 看wiki里面關于使用釘釘通知的介紹

MailOption = new MailOption

{

Server = "smtp.qq.com",

Port = 465,

UseSsl = true,

User = "1877682825@qq.com",

Password = "test",

},

DefaultRecurringQueueName = "recurring" //這個是在下面設置的queue列表中的其中一個

})

.UseTagsWithMysql(sqlOptions: mysqlOption);

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logging)

{

#region NLOG 你不用NLOG也可以去掉

NLog.LogManager.LoadConfiguration("NLog.Config");

logging.AddNLog();

#endregion

//強制顯示中文

System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");

//強制顯示英文

//System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("");

var queues = new List {"default","apis","recurring"};

app.UseHangfireServer(new BackgroundJobServerOptions

{

ServerTimeout = TimeSpan.FromMinutes(4),

SchedulePollingInterval = TimeSpan.FromSeconds(15),//秒級任務需要配置短點,一般任務可以配置默認時間,默認15秒

ShutdownTimeout = TimeSpan.FromMinutes(30),//超時時間

Queues = queues,//隊列

WorkerCount = Math.Max(Environment.ProcessorCount, 40)//工作線程數(shù),當前允許的最大線程,默認20

});

var hangfireStartUpPath = "/job";

app.UseHangfireDashboard(hangfireStartUpPath, new DashboardOptions

{

AppPath = "#",

DisplayStorageConnectionString = false,

IsReadOnlyFunc = Context => false,

Authorization = new[] { new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions

{

RequireSsl = false,

SslRedirect = false,

LoginCaseSensitive = true,

Users = new []

{

new BasicAuthAuthorizationUser

{

Login = "admin",

PasswordClear = "test"

}

}

}) }

});

var hangfireReadOnlyPath = "/job-read";

//只讀面板,只能讀取不能操作

app.UseHangfireDashboard(hangfireReadOnlyPath, new DashboardOptions

{

IgnoreAntiforgeryToken = true,//這里一定要寫true 不然用client庫寫代碼添加webjob會出錯

AppPath = hangfireStartUpPath,//返回時跳轉的地址

DisplayStorageConnectionString = false,//是否顯示數(shù)據(jù)庫連接信息

IsReadOnlyFunc = Context => true

});

app.Run(async (context) =>

{

await context.Response.WriteAsync("ok.");

});

}

}

注意: 如果沒有在數(shù)據(jù)庫里面創(chuàng)建一個db叫hangfire的話 會報錯! 可以不創(chuàng)建hangfire的表 因為按照如上配置 啟動程序會判斷有沒有叫hangfire的db。如果存在沒有表會初始化hangfire的表!

按照以上配置 啟動程序

瀏覽器打開 localhost:5000/job 就會打開hangfire的 dashbord

瀏覽器打開 localhost:5000/job-read 就會打開 hangfire 的只讀 dashbord 以上按鈕將隱藏

以上代碼在本倉庫的Test里面有,你配置好 db 后可以直接跑起來的

總結

以上是生活随笔為你收集整理的hangfire支持mysql_快速入门 - Hangfire.HttpJob 中文文档的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。