【问题标题】:Hangfire job on Console/Web App solution?控制台/Web 应用程序解决方案的 Hangfire 工作?
【发布时间】:2015-12-23 17:50:36
【问题描述】:

我是 Hangfire 的新手,我正在尝试了解它是如何工作的。

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

我究竟应该在哪里安装 Hangfire?在 Web 应用程序或控制台中?还是应该将控制台转换为 Web 应用程序上的类?

【问题讨论】:

  • 您的 MVC 应用程序的作用是什么?它与您的控制台应用程序有什么关系吗?

标签: asp.net-mvc scheduled-tasks hangfire


【解决方案1】:

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

HangFire 概述

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

HangFire 安装

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

Hangfire 项目由几个可用的 NuGet 包组成 NuGet 库站点。这是您应该知道的基本软件包列表 关于:

  • Hangfire – 仅用于安装的引导程序包 适用于使用 SQL Server 作为作业存储的 ASP.NET 应用程序。它 简单地引用 Hangfire.Core、Hangfire.SqlServer 和 Microsoft.Owin.Host.SystemWeb 包。

  • Hangfire.Core – 基本包 包含 Hangfire 的所有核心组件。它可以用于任何 项目类型,包括 ASP.NET 应用程序、Windows 服务、控制台、 任何与 OWIN 兼容的 Web 应用程序、Azure Worker Role 等。

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

安装 HangFire 后,您可以将其配置为使用仪表板,这是一个界面,您可以在其中找到有关后台作业的所有信息。在我工作的公司中,我们多次使用 HangFire 来处理重复性工作,主要用于导入用户、跨应用程序同步信息以及执行在工作时间运行成本很高的操作,当我们想知道是否某个作业是否正在运行。它还使用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 应用程序中的情况。

【讨论】:

  • 我只想将我的 mvc 应用程序中的后台作业排入队列,该应用程序是长时间运行的操作,而是会在 wcf/windows 服务中处理这个长时间运行的操作,该服务将驻留在客户端。尽管有关此的信息是在hangfire docs中给出,但他们没有显示任何与此相关的配置:docs.hangfire.io/en/latest/background-processing/…
【解决方案2】:

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

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

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

  1. BackgroundJob.Enqueue --> 立即更新到 DB
  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.
    }
}

【讨论】:

  • 如果 Web 应用程序从 IIS 回收并且由于在我安排任务时没有用户在凌晨 2 点访问该站点而未运行,会发生什么情况。任务还会运行吗?
  • @MIKE :为此,您必须在应用程序中始终使用运行模式。 docs.hangfire.io/en/latest/deployment-to-production/…
  • @MIKE : 我想,超过预定时间就不会运行了
  • 这意味着我需要在生产环境中保持 4-5 个应用程序始终运行。这会是个问题吗?
  • @MIKE 只制作一个名为 HangFire 的应用程序并让它使用 HangFireServer。让这个应用程序拥有自己的应用程序池以始终运行。当您在 HangFire 存储中排队作业时,此 HangFire 应用程序将选择作业(无论您的应用程序如何)并执行它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多