使用Hangfire 遇到的一些问题分享一下

1、当在Dashboard的Recurring Job中发现报错"Could not load file or assembly"

因为没有在Dashboard的项目中引用对应的DLL,不是Job报错,不会影响Job的调度,但是无法使用Dashboard去立即执行某个此类Job

 

2、当在Failed Jobs中发现报错"Could not load file or assembly",并且不是每次都会报错

考虑是否因为有多个项目使用了同一个Dashboard的数据库SecmerName,且多个项目均使用Default的Queue。

建议创建服务以及任务的时候加上Queue参数,可以避免这种情况

 

[Queue("tag")]
public void Process()
{
}

//调用
Hangfire.BackgroundJob.Enqueue(() => Process() ));


//服务
 _server = new BackgroundJobServer(
                    new BackgroundJobServerOptions()
                    {
                        Queues = new string[] { "tag" }
                    }
                );

3、使用免费版的Hangfire模拟pro版中的Batch执行

int threadCount = 5 ;//线程数
int WorkCount=100;//需要执行的工作
int threadRunWorkCount = Convert.ToInt32(
Math.Ceiling((double)WorkCount / threadCount));
for (int i = 0; i < threadCount; i++)
{
     List<string> batchJobID = new List<string>();
     for (int j = 0; j < threadRunWorkCount; j++)
     {
           if (i * threadRunWorkCount + j == tagCategoryProcesses.Count) { break; }
           if (batchJobID.Count == 0)
           {
             batchJobID.Add(
             Hangfire.BackgroundJob.Enqueue(
             () => Console.WriteLine()
             ));
            }
            else
            {
              batchJobID.Add(
              Hangfire.BackgroundJob.ContinueWith(
                 batchJobID.LastOrDefault(),
               () =>  () => Console.WriteLine()));
             }
       }
  }
}

亲测可以用,但是会多跑很多JOB

posted on 2017-01-12 17:01  !!!!!!!!!!!!!!!  阅读(1160)  评论(0)    收藏  举报

导航