簡體   English   中英

如何使用 Mysql 存儲配置 Hangfire

[英]How to configure Hangfire with Mysql storage

我有一個使用 ASP.NET CORE 開發的 Web 應用程序,我想向我的客戶發送每周時事通訊。 環顧四周,我認為 Hangfire 會是我使用的好工具。 不幸的是,我似乎無法使用 Mysql 數據庫讓它工作。 它與 InMemboryStorage 完美配合。

下面是我的設置。

ASP.NET 核心 2.2

掛火 1.7.8

Hangfire.Mysql.Core 2.2.5

Hangfire 在數據庫中創建了大約 9 個表,但是當我嘗試創建后台作業時,。 我收到一條錯誤消息,提示“hangfire_state 表不存在”。

下面是我在啟動時的配置,以及錯誤的堆棧跟蹤。 如果您知道為什么會發生這種情況,請提供幫助。

謝謝!

services.AddHangfire(cfg =>
            {
//                cfg.UseMemoryStorage();
                cfg.UseStorage(new MySqlStorage(
                    "server=localhost; database=hangfire; password=''; uid=root; port=3306; Allow User Variables=True"));
            });

創建用於測試的后台作業的控制器操作

[HttpGet("/hagnfire-test")]
        public IActionResult TestHangfire()
        {
            BackgroundJob.Enqueue(() => Console.WriteLine("Hangfire works!"));

            return Ok("Hangfire test");
        }

以下是錯誤;

MySqlException: Table 'hangfire.hangfire_state' doesn't exist
MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in ResultSet.cs, line 49

MySqlException: Table 'hangfire.hangfire_state' doesn't exist
MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet() in MySqlDataReader.cs, line 116

BackgroundJobClientException: Background job creation failed. See inner exception for details.
Hangfire.BackgroundJobClient.Create(Job job, IState state)

堆棧跟蹤

MySqlException: Table 'hangfire.hangfire_state' doesn't exist
MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet() in MySqlDataReader.cs
MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary<string, CachedProcedure> cachedProcedures, IMySqlCommand command, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in MySqlDataReader.cs
MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(IReadOnlyList<IMySqlCommand> commands, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in CommandExecutor.cs
MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in MySqlCommand.cs
MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() in MySqlCommand.cs
Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, ref CommandDefinition command, Action<IDbCommand, object> paramReader) in SqlMapper.cs
Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, ref CommandDefinition command) in SqlMapper.cs
Dapper.SqlMapper.Execute(IDbConnection cnn, string sql, object param, IDbTransaction transaction, Nullable<int> commandTimeout, Nullable<CommandType> commandType) in SqlMapper.cs
Hangfire.MySql.Core.MySqlWriteOnlyTransaction+<>c__DisplayClass7_0.<SetJobState>b__0(MySqlConnection x)
Hangfire.MySql.Core.MySqlWriteOnlyTransaction.<Commit>b__30_0(MySqlConnection connection)
Hangfire.MySql.Core.MySqlStorage+<>c__DisplayClass19_0.<UseTransaction>b__0(MySqlConnection connection)
Hangfire.MySql.Core.MySqlStorage+<>c__DisplayClass20_0<T>.<UseTransaction>b__0(MySqlConnection connection)
Hangfire.MySql.Core.MySqlStorage.UseConnection<T>(Func<MySqlConnection, T> func)
Hangfire.MySql.Core.MySqlStorage.UseTransaction<T>(Func<MySqlConnection, T> func, Nullable<IsolationLevel> isolationLevel)
Hangfire.MySql.Core.MySqlStorage.UseTransaction(Action<MySqlConnection> action)
Hangfire.Client.CoreBackgroundJobFactory+<>c__DisplayClass14_0.<Create>b__3(int attempt)
Hangfire.Client.CoreBackgroundJobFactory+<>c__DisplayClass15_0.<RetryOnException>b__0(int attempt)
Hangfire.Client.CoreBackgroundJobFactory.RetryOnException<T>(ref int attemptsLeft, Func<int, T> action)
Hangfire.Client.CoreBackgroundJobFactory.RetryOnException<T>(ref int attemptsLeft, Func<int, T> action)
Hangfire.Client.CoreBackgroundJobFactory.RetryOnException(ref int attemptsLeft, Action<int> action)
Hangfire.Client.CoreBackgroundJobFactory.Create(CreateContext context)
Hangfire.Client.BackgroundJobFactory+<>c__DisplayClass12_0.<CreateWithFilters>b__0()
Hangfire.Client.BackgroundJobFactory.InvokeClientFilter(IClientFilter filter, CreatingContext preContext, Func<CreatedContext> continuation)
Hangfire.Client.BackgroundJobFactory.Create(CreateContext context)
Hangfire.BackgroundJobClient.Create(Job job, IState state,

您需要在StartUp配置中初始化屬性PrepareSchemaIfNecessary = true

傳遞以下選項:

var options =
new MySqlStorageOptions {
    TransactionIsolationLevel = IsolationLevel.ReadCommitted,
    QueuePollInterval = TimeSpan.FromSeconds(15),
    JobExpirationCheckInterval = TimeSpan.FromHours(1),
    CountersAggregateInterval = TimeSpan.FromMinutes(5),
    PrepareSchemaIfNecessary = true,
    DashboardJobListLimit = 50000
};
var storage = new MySqlStorage(connectionString, options);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM