简体   繁体   English

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

[英]HangFire recurring jobs on asp.net

I started using HangFire to process a task every X minutes/hours and I need to add recurring jobs. 我开始使用HangFire每隔X分钟/小时处理一次任务,并且需要添加重复性工作。 In console application I managed to do it just fine but on asp.net I can only get it to work once, through the BackgroundJob.Enqueue method. 在控制台应用程序中,我设法做到了这一点,但是在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
    }
}

I thought I got it right but evidently I got something wrong here. 我以为我做对了,但显然我在这里做错了。 Where do you think the problem might be at? 您认为问题可能在哪里?

There's two problems. 有两个问题。

One. 一。 because you're passing id in - which is unnecessary in your implementation. 因为你传递id在-这是在您的实现是不必要的。

BackgroundJob.Enqueue returns a unique identifier for a job. BackgroundJob.Enqueue返回作业的唯一标识符。 In your code, this job has already been queued and executed. 在您的代码中,此作业已排队并执行。

Change 更改

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

to

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

Also, you're wrapping the creation of BackgroundJobServer in a using statement. 同样,您将在using语句中包装BackgroundJobServer的创建。 Then creating the job in there. 然后在其中创建作业。 Therefore, the BackgroundJobServer is being cleared up by GC. 因此,GC正在清除BackgroundJobServer

Instead, try this: 相反,请尝试以下操作:

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