2017-08-21 16 views
1

私はHangFireを使用してX分/時間ごとにタスクを処理し始め、定期的なジョブを追加する必要があります。コンソールアプリケーションでは、私はそれをうまくやっていましたが、asp.netで、私はBackgroundJob.Enqueueメソッドを使って一度しか動かすことができません。asp.netのHangFire定期ジョブ

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 
    } 
} 

私はそれが正しいと思ったが、明らかに私はここで何か間違っていた。問題はどこにあると思いますか?

答えて

2

2つの問題があります。

1。 idを渡しているため、実装では不要です。

BackgroundJob.Enqueueは、ジョブの一意の識別子を返します。コードでは、このジョブはすでにキューに入れられて実行されています。

変更

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

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

にまた、あなたはusing文でBackgroundJobServerの作成をラップしています。それからそこに仕事を作ります。したがって、BackgroundJobServerはGCによってクリアされています。

代わりに、これを試してみてください。

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); 
    } 
} 
+0

が、私はそれを変えました。しかし、まだ一度だけ実行します。 – taiko

+0

@taiko編集された回答を参照 – Alex

+0

[OK]を変更して何も得られませんでした。しかし、私はBackgroundJob.Enqueue(()=> Actualizacoes()を追加しました。 _backgroundJobServer = new BackgroundJobServer();とRecurringJob.AddOrUpdate(()=> Actualizacoes()、Cron.Minutely);それは働いた。それを更新する前にまずそれを作成しなければならないようです。意味をなさない私はそれを受け入れることができるようにあなたの答えを編集します。そして大きな感謝! :) – taiko

関連する問題