简体   繁体   English

防止并行执行扩展的 ASP.NET Core 托管服务

[英]Prevent parallel executions of scaled ASP.NET Core hosted service

I have web api hosted on AWS (AWS fargate).我在 AWS (AWS fargate) 上托管了 web api。 I want to make sending some emails and other stuff as Background service .我想发送一些电子邮件和其他东西作为后台服务 Problem is that in case I decide my web api to scale that multiple instances of that hosted service will be executed.问题是,如果我决定我的 web api 来扩展将执行该托管服务的多个实例。

I have sql database and some table where "IsEmailSent" and "IsReportSent" flag exist.我有 sql 数据库和一些存在“IsEmailSent”和“IsReportSent”标志的表。

Background hosted service just loops through all rows where that fields are false so implmenetation of that service could be relatively easy.后台托管服务只是遍历所有字段为假的行,因此该服务的实现可能相对容易。

However, when it comes to "preventing" multiple instances run hosted service there are couple of options.然而,当谈到“防止”多个实例运行托管服务时,有几个选项。 What would be most easy way in this kind of setup?在这种设置中最简单的方法是什么? This is what to my mind first:这是我首先想到的:

Introducing some kind of database locking which could be dangerous and not too performant.引入某种可能很危险且性能不佳的数据库锁定。

Using AWS SQS and dequeuing inside background service is option but would like to avoid it if not needed since I currently don't use SQS anywhere in app.使用 AWS SQS 并在后台服务中出列是一种选择,但如果不需要,我想避免它,因为我目前不在应用程序的任何地方使用 SQS。

To manage multiple instances to not get the same item you need a "push" system.要管理多个实例而不是获得相同的项目,您需要一个“推送”系统。 The optimum way to implement that is via queue.实现它的最佳方式是通过队列。

If you don't want to use a queue, the other way is to control the instances, and "start" the worker in only one instance.如果您不想使用队列,另一种方法是控制实例,并仅在一个实例中“启动”worker。 You can use a simple database to select wich instance is available to work on items.您可以使用简单的数据库来选择可用于处理项目的实例。

But I repeat, the best way to do it is via Queue or via Hooks但我再说一遍,最好的方法是通过 Queue 或 Hooks

I have several idea how you can solve this problem.我有几个想法如何解决这个问题。

The first one, try to make master service the one of your multiscaled services.第一个,尝试使主服务成为您的多尺度服务之一。 It is the only one service who can handle commands and where you can start the background service for e-mail sending.它是唯一可以处理命令的服务,您可以在其中启动后台服务以进行电子邮件发送。 Other services will handles only queries(GET methods).其他服务将只处理查询(GET 方法)。 To achieve this you can use configuration file.为此,您可以使用配置文件。 And run master and slaves with different configuration.并以不同的配置运行主从。 You can find another way that is more according your infrastructure.您可以找到另一种更适合您的基础架构的方式。 The goal is run only one background service.目标是只运行一个后台服务。 This don't prefer this way.这不喜欢这种方式。

The second one require to introduce an additional service.第二个需要引入额外的服务。 It can be also .NET app with running background service, but it must do nothing else from reading DB and sending e-mail.它也可以是运行后台服务的 .NET 应用程序,但除了读取数据库和发送电子邮件之外,它必须什么都不做。 You run this application as one instance.您将此应用程序作为一个实例运行。 It haven't to be scaled.它不必被缩放。 Also you can use WebHook to decouple this service from database.您也可以使用 WebHook 将此服务与数据库分离。 It allows you subscribe to some events and send e-mails without accessing DB of another service.它允许您订阅某些事件并发送电子邮件,而无需访问其他服务的数据库。 If you wanna scale e-mail sender service can solve it by load balancer or you should look at event bus(if you already use it in your stack it is good choice).如果您想扩展电子邮件发件人服务,可以通过负载平衡器解决它,或者您应该查看事件总线(如果您已经在堆栈中使用它,这是一个不错的选择)。 This I would prefer more than the first one.这个我比第一个更喜欢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 处理 IAsyncEnumerable 项目与 asp.net 核心托管服务消费者中的多个并行任务 - C# process IAsyncEnumerable items with several parallel tasks in asp.net core hosted service consumer ASP.NET Core中托管服务的并行排队后台任务 - Parallel queued background tasks with hosted services in ASP.NET Core 无法访问Windows服务中托管的ASP.NET Core应用程序 - Cannot access ASP.NET Core application hosted in a Windows Service 在 ASP.NET 核心的托管服务中使用范围服务 - Using Scoped services within a hosted service in ASP.NET Core asp.net core 中的托管服务(后台服务)在每次 Publish azure 中重启 - hosted service (background service) in asp.net core restart in each Publish azure ASP.NET Core 托管在 Windows 服务中 - 未提供静态文件(即 /wwwroot 的内容) - ASP.NET Core hosted in a Windows Service - static files not being served (i.e. contents of /wwwroot) ASP.NET Core 3.1 - 从托管服务检查是否加载了客户端(索引)页面 - ASP.NET Core 3.1 - From a Hosted Service, check if client (Index) page is loaded Azure上托管的ASP.NET Core 2.2 App Service返回500,没有引发异常 - ASP.NET Core 2.2 App Service hosted on Azure returning 500 without an exception being thrown asp.net核心托管服务中的“计时器+ Task.Run”与“while循环+ Task.Delay” - "timer + Task.Run" vs "while loop + Task.Delay" in asp.net core hosted service ASP.NET并行Web服务调用 - ASP.NET Parallel Web Service Calls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM