【问题标题】:How to make sure processing is handled gracefully如何确保处理得到妥善处理
【发布时间】:2018-12-14 20:27:39
【问题描述】:

我希望我的申请能够优雅地结束。有可能这样做吗?如果是这样,我将如何做到这一点?

以下是应用程序外观的一般概述:

// .NET Core Console App
while (true)
{
    // Get the next message from the Queue. The Queue is a database table that contains a list of "things that need to be done".

    // Spawn a Task to handle that message. Send the Task all the data it requires to complete. Generally speaking, the Task will collate all the data in an object and make a call (PUT, POST, DELETE) against a set of RESTful web services.

    // Sleep for some time.
    Thread.Sleep(sleepTimeInSeconds * 1000);
}

此应用程序通常会在某些服务器上不间断地运行。但是,如果用户需要将其关闭,我希望该作业能够优雅地退出:

  • 停止查询队列以获取更多消息。查询到的消息被数据库锁定,其他进程无法处理同一条消息。
  • 允许完成所有正在运行的任务。

有可能做到这一点吗?我可以使用哪些技术/库/概念来实现这一目标?

【问题讨论】:

    标签: c# process task exit


    【解决方案1】:

    为避免在代码中管理作业的复杂性,您可以使用第三方包,即Hangfire

    基本上,您将解雇并忘记您的工作,并让另一个进程处理运行这些工作。如果您的应用出现故障,hangfire 服务器将继续运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 2019-04-26
      • 2013-07-30
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多