HangFire在asp.net上重复工作

时间:2017-08-21 16:02:53

标签: c# asp.net hangfire

我开始使用HangFire每隔X分钟/小时处理一项任务,我需要添加定期作业。在控制台应用程序中,我设法做得很好但是在asp.net上我只能通过BackgroundJob.Enqueue方法让它工作一次。

public class Global : HttpApplication
{
    private static string LigacaoBD = myConn;
    private static ApiMethods sportRadar = new ApiMethods();
    private static Jogo jogo = new Jogo(LigacaoBD);
    private static List<SportRadar.ClassesCliente.Jogo> jogos;
    private static List<SportRadar.ClassesCliente.Competicao> competicoes;

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        GlobalConfiguration.Configuration.UseSqlServerStorage(LigacaoBD);

        using (var connection = JobStorage.Current.GetConnection())
        {
            foreach (var recurringJob in connection.GetRecurringJobs())
            {
                RecurringJob.RemoveIfExists(recurringJob.Id);
            }
        }

        using (var server = new BackgroundJobServer())
        {
            // This works just fine
            var id=BackgroundJob.Enqueue(() => Actualizacoes());
            // This does not.
            // I even checked the DB for job queue or something but couldn't find anything
            RecurringJob.AddOrUpdate(id, () => Actualizacoes(), Cron.Minutely);
        }
    }



    public void Actualizacoes()
    {
        // Stuff I need to do regularly
    }
}

我以为我做对了,但显然我在这里弄错了。您认为问题可能在哪里?

1 个答案:

答案 0 :(得分:3)

有两个问题。

一。因为您正在传递id - 这在您的实施中是不必要的。

BackgroundJob.Enqueue返回作业的唯一标识符。在您的代码中,此作业已经排队并执行。

更改

RecurringJob.AddOrUpdate(id, () => Actualizacoes(), Cron.Minutely);

RecurringJob.AddOrUpdate(() => Actualizacoes(), Cron.Minutely);

此外,您正在BackgroundJobServer语句中创建using。然后在那里创建工作。因此,GC正在清除BackgroundJobServer

相反,试试这个:

public class Global : HttpApplication
{
    private static string LigacaoBD = myConn;
    private static ApiMethods sportRadar = new ApiMethods();
    private static Jogo jogo = new Jogo(LigacaoBD);
    private static List<SportRadar.ClassesCliente.Jogo> jogos;
    private static List<SportRadar.ClassesCliente.Competicao> competicoes;

    private BackgroundJobServer _backgroundJobServer;

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        GlobalConfiguration.Configuration.UseSqlServerStorage(LigacaoBD);

        using (var connection = JobStorage.Current.GetConnection())
        {
            foreach (var recurringJob in connection.GetRecurringJobs())
            {
                RecurringJob.RemoveIfExists(recurringJob.Id);
            }
        }

        //create an instance of BackgroundJobServer
        _backgroundJobServer = new BackgroundJobServer();

        //add your recurring job
        RecurringJob.AddOrUpdate(() => Actualizacoes(), Cron.Minutely);
    }
}