繁体   English   中英

在asp.net上的HangFire周期性工作

[英]HangFire recurring jobs on asp.net

我开始使用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
    }
}

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

有两个问题。

一。 因为你传递id在-这是在您的实现是不必要的。

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

更改

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

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

同样,您将在using语句中包装BackgroundJobServer的创建。 然后在其中创建作业。 因此,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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM