【问题标题】:Hangfire background Job with scheduleHangfire 背景作业与时间表
【发布时间】:2016-02-03 21:01:59
【问题描述】:

有没有办法在 hangfire 中为后台作业定期安排? 我尝试了以下方法,但它只触发一次。

 using (var server = new BackgroundJobServer(options))
            {
                Console.WriteLine("Hangfire Server started. Press any key to exit...");
               // Console.ReadKey();
            }
            return null;

【问题讨论】:

    标签: hangfire


    【解决方案1】:

    如果您想要定期安排工作或想要在任何特定日期或月份执行方法。对于 Cron 表达式,您需要遵循 this

    例如:

    DateTime date =
                    DateTime.Parse(ScheduleTime, System.Globalization.CultureInfo.CurrentCulture)
                        .ToUniversalTime();
                var dateString = ScheduleTime.Minute + " " + ScheduleTime.Hour + " " + ScheduleTime.ScheduleDate + " * *";
    
                RecurringJob.AddOrUpdate<IClassController>(
                    "RecurringName", s => s.Execute(MViewModel),
                    dateString);
    
    
        public async Task<bool> Execute(MViewModel mViewModel)
        {
            try
            {
                if (mViewModel.ExpiryDate != null && mViewModel.ExpiryDate.Value < DateTime.Now)
                {
                    RecurringJob.RemoveIfExists("RecurringName");
                }
                else
                {
                    var url = WebConfigurationManager.AppSettings["apiPath"];
                    string apiUrl = url + "api/VApi/CallAPi";
    
                    using (HttpClient client = new HttpClient())
                    {
                        client.BaseAddress = new Uri(apiUrl);
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                            "bW9iaWxlYXBw");
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                        var serializedJson = JsonConvert.SerializeObject(mViewModel);
    
                        var response = await client.PostAsJsonAsync(apiUrl, serializedJson);
                    }
                }
            }
            catch (Exception exception)
            {
                _logger.Log(LogType.Error, ExceptionType.Application, ToString(), exception);
                return false;
            }
            return true;
        }
    

    【讨论】:

      【解决方案2】:

      使用 RecurringJob 创建它。 Hangfire Recurring Tasks

      RecurringJob.AddOrUpdate("some-id", () => Console.WriteLine(), Cron.Hourly);
      

      【讨论】:

        【解决方案3】:

        您可以像这样使用 hangfire 安排作业:

         Hangfire.BackgroundJob.Schedule<VerificationEmail>((j) => j.FollowUpVerficationEmail(Email), TimeSpan.FromHours(24));
        

        这里的VerificationEmail 是包含FollowUpVerficationEmail 方法的类,用于在每24 小时后发送后续电子邮件。

        【讨论】:

          【解决方案4】:

          你可以这样使用,

          RecurringJob.AddOrUpdate(/*(optional) job Id*/,() => /*do your stuff here */, /*Cron Expression*/);
          

          1)你可以通过HangFire的Cron类来选择你的cron表达式,如下图所示

          RecurringJob.AddOrUpdate("job1", () => Console.WriteLine("hey"), Cron.Hourly);
          

          2) 或者您可以通过here 指定您的cron 表达式,

          RecurringJob.AddOrUpdate("job2", () => Console.WriteLine("hey"), "0/10 0 0 ? * * *");
          

          您可以查看here了解更多详情。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-12-25
            • 1970-01-01
            • 1970-01-01
            • 2022-05-20
            • 1970-01-01
            • 2020-03-04
            • 1970-01-01
            • 2020-01-11
            相关资源
            最近更新 更多