通过REST请求启动运行时间非常长的进程 [英] Start extremely long running processes through a REST request

查看:45
本文介绍了通过REST请求启动运行时间非常长的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一家自动化公司工作,所以我们创建了工业自动化流程.以前,这种自动化是在机器方面完成的,但是我们正在逐步过渡到使用c#控制机器.

在我当前的项目中,一天的生产大约需要2个小时.工厂的操作员拥有一个使用asp.net核心MVC在c#中创建的Web界面,他们可以在其中启动/暂停/停止该生产过程.

启动过程时,我们在控制器中等待一个函数,该函数基本上是一个while循环,用于控制此2小时的生产过程.

现在的问题是,当我发出REST请求以开始生产时,此请求需要2小时才能完成,我希望此请求立即完成,并且生产过程在asp.net核心应用程序的后台开始./p>

首先,我想我可以省去等待,而只需在控制器中执行即可(简化代码):

  _ = _productionController.StartLongProcess();//这包含while循环返回Ok(); 

但是由于_productionController的作用域以及它的所有依赖关系也都存在,所以当方法返回时,这些将立即被处理掉,例如,我无法再访问我的数据库.

该流程应该能够连续地与我们的数据库进行对话,以保存有关生产流程的信息,以防万一发生故障,这样我们就可以始终从上次中断的地方开始.

我现在对您的问题是,我们是否以错误的方式处理?我认为在asp.net控制器中启动这些长时间运行的进程是不好的做法.

即使在REST请求已经结束的情况下,如何确保在这个长时间运行的过程中始终可以访问我的DatabaseContext.仅为此方法创建单独的作用域?

解决方案

从ASP.NET Core 2.1开始,正确的方法(在asp.net内)是扩展 BackgroundService (或实现 IHostedService ).

传入的请求可以告诉后台服务开始长时间运行的操作并立即返回.当然,您将需要处理在现有请求完成之前发送重复请求或发送新请求的情况.

文档页面具有_ = _productionController.StartLongProcess(); // This contains the while loop return Ok();

But since _productionController is scoped and all its dependencies are as well, these immediately get disposed of when the method returns and I can't access my database anymore for example.

The process should be able to continuously talk to our database to save information about the production process in case something fails, that way we can always pick off where we left off.

My question to you is now, are we tackling this the wrong way? I imagine it's bad practice to start these long running processes in the asp.net controller.

How do I make sure I always have access to my DatabaseContext in this long running process even though the REST request has already ended. Create a separate scope only for this method?

解决方案

Starting ASP.NET Core 2.1, the right way to do this (within asp.net) is to extend BackgroundService (or implement IHostedService).

Incoming requests can tell the background service to start the long-running operation and return immediately. You'll of course need to handle cases where duplicate requests are sent in, or new requests are sent in before the existing request is completed.

The documentation page has an example where the BackgroundService reads commands of a queue and processes them one at a time.

How do I make sure I always have access to my DatabaseContext in this long running process even though the REST request has already ended. Create a separate scope only for this method?

Yes, create a separate scope.

My question to you is now, are we tackling this the wrong way? I imagine it's bad practice to start these long running processes in the asp.net controller.

We've done something similar in the past. As long as fault-tolerance (particularly w.r.t. app restarts) and idempotence are built into the long-running-operation's logic, you should be good to go.

这篇关于通过REST请求启动运行时间非常长的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆