yuzd / Hangfire.HttpJob

httpjob for Hangfire,restful api for Hangfire,job调度与业务分离
https://github.com/yuzd/Hangfire.HttpJob/wiki
MIT License
618 stars 184 forks source link

使用sqlserver 作为存储无法工作 #211

Closed andycheng-jiuluomall closed 2 months ago

andycheng-jiuluomall commented 2 months ago

.net 8 hangfirehttpjob 3.8.4

使用sqlserver 报错以下是我的代码

另外您项目中的SqlserverhangfireTest 启动也会有报错 引发的异常:“System.NotSupportedException”(位于 Hangfire.SqlServer.dll 中)

System.ArgumentException: The functions QueueCommand and AddCommand cannot be found.
   at Hangfire.Tags.SqlServer.SqlTagsTransaction..ctor(SqlServerStorageOptions options, IWriteOnlyTransaction transaction)
   at Hangfire.Tags.SqlServer.SqlTagsServiceStorage.GetTransaction(IWriteOnlyTransaction transaction)
   at Hangfire.Tags.Storage.TagExpirationTransaction.Persist(String jobid)
   at Hangfire.Tags.States.TagsCleanupStateFilter.OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
   at Hangfire.States.StateMachine.InvokeOnStateApplied(Tuple`2 x)
public class Option
{
    public HangfireHttpJobOptions HttpJobOptions { set; get; }
    public JobStorage Storage { get; set; }

    public DashboardOptions DashboardOptions { set; get; }

    public Action<IGlobalConfiguration> UseTag { set; get; }

    public Action<BackgroundJobServerOptions> ServerOptions { set; get; }
}

public static void AddService(WebApplicationBuilder builder, Option? option = null)
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
    builder.Services.AddTransient<IBackgroundProcess, ProcessMonitor>();
    builder.Services.AddHangfire(_=> {
        _.UseHeartbeatPage().UseConsole(new()
        {
            BackgroundColor = "#000079"
        });
        if (option?.Storage != null) _.UseStorage(option.Storage);
        if (option?.HttpJobOptions != null) _.UseHangfireHttpJob(option.HttpJobOptions);
        if (option?.UseTag != null) option.UseTag(_);
    });
    if (option?.ServerOptions != null) builder.Services.AddHangfireServer(option.ServerOptions);
}
public static void Use(WebApplication app, Option? option = null)
{
    if (option?.DashboardOptions != null && option?.Storage != null)
        app.UseHangfireDashboard("/hangfire", option.DashboardOptions, option.Storage);
}
var hangfireOption = new HangfireExt.Option()
{
    HttpJobOptions = new() { 
        DefaultRecurringQueueName= "recurring",
        DefaultBackGroundJobQueueName = "default",
        JobExpirationTimeoutDay = 7,
    },
    DashboardOptions = new()
    {
        DisplayStorageConnectionString=false,
        IgnoreAntiforgeryToken=true,
        Authorization = new[] {
            new BasicAuthAuthorizationFilter(new(){
                RequireSsl = false,
                SslRedirect = false,
                Users = new[]{ new BasicAuthAuthorizationUser() { Login="admin", PasswordClear="Ww123123@" } }
            })
        }
    },
    Storage = new SqlServerStorage(AppSetting.App<string>("Hangfire:Storage:Sqlserver")),
    UseTag = _ => { _.UseTagsWithSql(new() { TagsListStyle = TagsListStyle.Dropdown },null, new SqlServerStorage(AppSetting.App<string>("Hangfire:Storage:Sqlserver"))); },
    ServerOptions = _ => {
        _.Queues = new[] { "default", "apis", "recurring" };
    },
};
HangfireExt.AddService(builder, hangfireOption);
andycheng-jiuluomall commented 2 months ago

我代码中做了一层抽象设计 没有直接使用UseSqlServerStorage 而是给通用的Storage传入 之前项目中.netcore 3.1 时也是类似的代码是可以正常工作的 现在框架升级到.net8 hangfire也升级到最新了之后就无法工作了

yuzd commented 2 months ago

hangfire.tag.sqlserver组件有bug导致,等他更新

donnytian commented 2 months ago

fixed by FaceIT.Hangfire.Tags 1.9.0-beta.3