Console/Web 应用解决方案上的 Hangfire 作业?

Hangfire job on Console/Web App solution?

我是 Hangfire 的新手,我想了解它是如何工作的。

所以我在同一个解决方案中有一个 MVC 5 应用程序和一个控制台应用程序。控制台应用程序是一个简单的应用程序,仅更新数据库上的一些数据(最初计划使用 Windows Task Scheduler)。

我应该在哪里安装 Hangfire?在 Web 应用程序或控制台中?或者我应该将控制台转换为 Web 应用程序上的 class 吗?

不再需要任何控制台应用程序来更新数据库。您可以在 MVC 应用程序本身中使用 hangfire。

http://docs.hangfire.io/en/latest/configuration/index.html

添加hangfire配置后,您可以使用普通的MVC方法进行控制台操作,如更新数据库。 根据您的要求,您可以使用

  1. BackgroundJob.Enqueue --> 立即更新到数据库
  2. BackgroundJob.Schedule --> 数据库更新延迟
  3. RecurringJob.AddOrUpdate --> 像 windows 服务一样定期更新数据库。

下面是一个例子,

public class MyController : Controller
{

    public void MyMVCMethod(int Id)
    {
       BackgroundJob.Enqueue(() => UpdateDB(Id));  
    }

    public void UpdateDB(Id)
    {
      // Code to update the Database.
    }
}

如果我理解正确的话,你的解决方案中的控制台就像一个“伪”HangFire,因为就像你说的那样它会超时执行一些数据库操作并且你计划使用任务计划程序来执行它。

HangFire 概述

HangFire 旨在通过您的控制台应用程序完全满足您的需求,但具有更多的功能和功能,因此您可以避免自己创建所有这些的所有开销。

HangFire 安装

HangFire 通常与 ASP.NET 应用程序一起安装,但如果您仔细阅读文档,您会惊讶地发现:

Hangfire project consists of a couple of NuGet packages available on NuGet Gallery site. Here is the list of basic packages you should know about:

  • Hangfire – bootstrapper package that is intended to be installed only for ASP.NET applications that uses SQL Server as a job storage. It simply references to Hangfire.Core, Hangfire.SqlServer and Microsoft.Owin.Host.SystemWeb packages.

  • Hangfire.Core – basic package that contains all core components of Hangfire. It can be used in any project type, including ASP.NET application, Windows Service, Console, any OWIN-compatible web application, Azure Worker Role, etc.

如您所见,HangFire 可用于任何类型的项目,包括控制台应用程序,但您需要根据作业存储类型管理和添加所有库你会用。 See more here:

安装 HangFire 后,您可以将其配置为使用仪表板,这是一个界面,您可以在其中找到有关后台作业的所有信息。在我工作的公司,我们多次使用 HangFire 进行重复性工作,主要是导入用户、跨应用程序同步信息以及执行在工作时间对 运行 来说成本很高的操作,事实证明,当我们想知道某项工作是否 运行ning。它还使用 CRON 来安排操作。

我们现在使用的示例是:

Startup.cs

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        //Get the connection string of the HangFire database 
        GlobalConfiguration.Configuration.UseSqlServerStorage(connection);
        
        //Start HangFire Server and enable the Dashboard
        app.UseHangfireDashboard();
        app.UseHangfireServer();
        
        //Start HangFire Recurring Jobs
        HangfireServices.Instance.StartSendDetails();
        HangfireServices.Instance.StartDeleteDetails();
    }
}

HangfireServices.cs

public class HangfireServices
{
    //.. dependency injection and other definitions
    
    //ID of the Recurring JOBS
    public static string SEND_SERVICE = "Send";
    public static string DELETE_SERVICE = "Delete";

    public void StartSend()
    {
        RecurringJob.AddOrUpdate(SEND_SERVICE, () =>
            Business.Send(), //this is my class that does the actual process
            HangFireConfiguration.Instance.SendCron.Record); //this is a simple class that reads an configuration CRON file
    }

    public void StartDeleteDetails()
    {
        RecurringJob.AddOrUpdate(DELETE_SERVICE, () =>
            Business.SendDelete(), //this is my class that does the actual process
            HangFireConfiguration.Instance.DeleteCron.Record); //this is a simple class that reads an configuration CRON file
    }
}

HangFireConfiguration.cs

public sealed class HangFireConfiguration : ConfigurationSection
{
    private static HangFireConfiguration _instance;

    public static HangFireConfiguration Instance
    {
        get { return _instance ?? (_instance = (HangFireConfiguration)WebConfigurationManager.GetSection("hangfire")); }
    }

    [ConfigurationProperty("send_cron", IsRequired = true)]
    public CronElements SendCron
    {
        get { return (CronElements)base["send_cron"]; }
        set { base["send_cron"] = value; }
    }

    [ConfigurationProperty("delete_cron", IsRequired = true)]
    public CronElements DeleteCron
    {
        get { return (CronElements)base["delete_cron"]; }
        set { base["delete_cron"] = value; }
    }
}

hangfire.config

<hangfire>
  <send_cron record="0,15,30,45 * * * *"></send_cron>
  <delete_cron record="0,15,30,45 * * * *"></delete_cron>
</hangfire>

上面的 CRON 表达式将 运行 在每天每小时的 0、15、30、45 分钟。

Web.config

<configSections>
   
    <!-- Points to the HangFireConfiguration class -->
    <section name="hangfire" type="MyProject.Configuration.HangFireConfiguration" />
    
</configSections>

<!-- Points to the .config file -->
<hangfire configSource="Configs\hangfire.config" />

结论

鉴于您描述的情况,我可能会在您的 ASP.NET MVC 应用程序中安装 HangFire 并删除控制台应用程序,这很简单,因为这是一个不用担心的项目。即使您 可以 将它安装在控制台应用程序上,我也宁愿不遵循该路径,因为如果您撞到砖墙(相信我,您会撞到的),您很可能会主要针对安装在 ASP.NET 应用程序中的情况寻求帮助。