c# - 如何在任务完成之前返回但保持运行?
问题描述
我有一个 Azure 函数,其中一个异步方法async Task<IActionResult> MethodA
调用async Task MethodB
. 由于MethodB
预计总是需要超过 1 分钟,因此我们需要在完成前开始MethodB
并返回 202 Accepted 。在内部,我们通过将信息存储在表格中来跟踪状态。如果失败或抛出异常,我们会捕获异常并相应地更新表。这样,当客户端查询任务的状态时,它会从表中获取结果。这是实际发生的伪代码:MethodA
MethodB
MethodB
MethodB
// The starting method.
[FunctionName("MethodA")]
public static async Task<IActionResult> MethodA(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "start")] HttpRequest request,
ILogger logger)
{
// Somehow start MethodB so that it runs and we can return 202 Accepted before it finishes.
return new AcceptedResult();
}
private static async Task MethodB(ILogger logger)
{
try
{
// Insert row into table with status "running" and do logging.
// Do stuff that takes longer than 1 minute and do more logging.
}
catch(Exception exception) // Very general exception handling for pseudo-code.
{
// Update row in table to status "failed" an do some logging.
}
}
[FunctionName("MethodC")
public static async Task<IActionResult> MethodC(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "get")] HttpRequest request,
ILogger logger)
{
// Looks for row in table, gets the status, and do logging.
// Returns 200 Ok if still running or succeeded, otherwise otherwise some failure code.
// Also returns the exact status in the table to differentiate between running and succeeded.
}
有哪些启动选项MethodB
可以在我返回 202 Accepted 后仍然运行?我看到了很多关于不同解决方案的东西,其中一些阻塞线程,而另一些则没有,所以这对我来说有点令人困惑,因为我是新手。
解决方案
推荐阅读
- react-native - Shopify create customer api 正在使用邮递员,但不适用于反应本机应用程序
- php - 错误:无效参数:状态 [rest_invalid_param]
- r - 警告消息:损坏的数据帧
- c++ - DisableAddressSharing 注册表不起作用
- node.js - 将文件添加到s3中的多个文件夹
- python - eb 配置和 .ebextensions/ - .ebextensions/ 不工作
- c# - 如何反序列化包含根元素的 JSON;页脚元素和不相关的数据?
- python - 如何使用 xpath 获取所有子节点文本?
- python - 如何使用 tkinter 组合创建一个类似简单对话框的窗口?
- javascript - Marklogic 片段返回不完整的结果集