C# Orchestration and firestore

huangapple go评论95阅读模式
英文:

C# Orchestration and firestore

问题

以下是您提供的内容的翻译:

我有一个基本的Azure函数,它以一个定时器开始(1分钟用于测试),但经过数小时的努力,我无法从Firestore获取我的数据。

触发器:

[FunctionName("OrchestrationTriggerFunction")]
public async static Task OrchestrationTriggerFunction(
        [TimerTrigger("0 */1 * * * *")] TimerInfo myTimer,
        [DurableClient] IDurableOrchestrationClient starter)
{
  await starter.StartNewAsync("Function1", null);
}

Function1:

[FunctionName("Function1")]
public async Task Function1(
  [OrchestrationTrigger] IDurableOrchestrationContext context
  )
{
  List<string> OrgsToProcess = new List<string>() { "ID" };

  foreach (string org in OrgsToProcess)
  {

    var vehicles = await context.CallActivityAsync<List<Vehicle>>("GetFirestoreVehicles", org);    

    var parallelTasks = new List<Task<Result>>();

    foreach (var vehicle in vehicles)
    {
      // 处理代码

      Task<CsvResult> task = context.CallActivityAsync<Result>("ProcessData", msg);
      parallelTasks.Add(task);
    }
    await Task.WhenAll(parallelTasks);

    await context.CallActivityAsync("Function2", parallelTasks);
    return;
  }
}

GetFirestoreVehicles:

[FunctionName("GetFirestoreVehicles")]
public Task<List<Vehicle>> GetFirestoreVehicles([ActivityTrigger] string org)
{
  return _firestoreDb.GetVehicles(org);
}

在Function1中,在 var vehicles = await context.CallActivityAsync<List<Vehicle>>("GetFirestoreVehicles", org);,我可以看到它具有正确数量的记录,然而所有属性都填充为null。

在调试时查看 GetFirestoreVehicles 时,它显示 Result = "{Not yet computed}",我无法在我的Firestore方法中使用 await,因为它会引发以下异常:
检测到多线程执行。这可能发生在编排器函数代码等待一个不是由DurableOrchestrationContext方法创建的任务的情况下

如何使我的Durable Function 等待来自Firestore的数据?

英文:

I have a basic Azure function that starts with a timmer (1 min for testing), but after hours of trying to make it work, I can't get my data from the firestore.

The trigger:

[FunctionName(&quot;OrchestrationTriggerFunction&quot;)]
public async static Task OrchestrationTriggerFunction(
        [TimerTrigger(&quot;0 */1 * * * *&quot;)] TimerInfo myTimer,
        [DurableClient] IDurableOrchestrationClient starter)
{
  await starter.StartNewAsync(&quot;Function1&quot;, null);
}

Function1:

[FunctionName(&quot;Function1&quot;)]
public async Task Function1(
  [OrchestrationTrigger] IDurableOrchestrationContext context
  )
{
  List&lt;string&gt; OrgsToProcess = new List&lt;string&gt;() { &quot;ID&quot; };

  foreach (string org in OrgsToProcess)
  {

    var vehicles = await context.CallActivityAsync&lt;List&lt;Vehicle&gt;&gt;(&quot;GetFirestoreVehicles&quot;, org);    

    var parallelTasks = new List&lt;Task&lt;Result&gt;&gt;();

    foreach (var vehicle in vehicles)
    {
      // processing code

      Task&lt;CsvResult&gt; task = context.CallActivityAsync&lt;Result&gt;(&quot;ProcessData&quot;, msg);
      parallelTasks.Add(task);
    }
    await Task.WhenAll(parallelTasks);

    await context.CallActivityAsync(&quot;Function2&quot;, parallelTasks);
    return;
  }
}

GetFirestoreVehicles:

[FunctionName(&quot;GetFirestoreVehicles&quot;)]
public Task&lt;List&lt;Vehicle&gt;&gt; GetFirestoreVehicles([ActivityTrigger] string org)
{
  return _firestoreDb.GetVehicles(org);
}

In Function1, on var vehicles = await context.CallActivityAsync&lt;List&lt;Vehicle&gt;&gt;(&quot;GetFirestoreVehicles&quot;, org); I can see that it has the correct number of records, however all the properties are filled with null.

When looking at the GetFirestoreVehicles during the debugging it says Result = &quot;{Not yet computed}&quot; and I'm unable to use await in my firestore method as it throws the following exception:
multithreaded execution was detected. This can happen if the orchestrator function code awaits on a task that was not created by a DurableOrchestrationContext method

How can I make my Durable Function await the data from firestore?

答案1

得分: 2

以下是翻译好的部分:

The GetFirestoreVehicles function should await the result to function properly like this:

[FunctionName("GetFirestoreVehicles")]
public async Task<List<Vehicle>> GetFirestoreVehicles([ActivityTrigger] string org)
{
    return await _firestoreDb.GetVehicles(org);
}

Also, this line is very strange:

await context.CallActivityAsync("Function2", parallelTasks);

You are passing a list of Task<T> to another function. Instead, you need to pass the resulting list of T, not the Task collection itself. Something like this will do:

var results = parallelTasks.Select(t => t.Result);  
await context.CallActivityAsync("Function2", results);

That means Function2 should accept IEnumerable<Result> instead of List<Task<Result>>.

英文:

The GetFirestoreVehicles function should await the result to function properly like this:

[FunctionName(&quot;GetFirestoreVehicles&quot;)]
public async Task&lt;List&lt;Vehicle&gt;&gt; GetFirestoreVehicles([ActivityTrigger] string org)
{
    return await _firestoreDb.GetVehicles(org);
}

Also, this line is very strange:

await context.CallActivityAsync(&quot;Function2&quot;, parallelTasks);

You are passing a list of Task&lt;T&gt; to another function. Instead, you need to pass the resulting list of T, not the Task collection itself. Something like this will do:

var results = parallelTasks.Select(t =&gt; t.Result);  
await context.CallActivityAsync(&quot;Function2&quot;, results);

That means Function2 should accept IEnumerable&lt;Result&gt; instead of List&lt;Task&lt;Result&gt;&gt;

huangapple
  • 本文由 发表于 2023年3月15日 20:27:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定