C# 如何在基于事件的设计中使用Azure批处理并终止/清理完成的作业

C# 如何在基于事件的设计中使用Azure批处理并终止/清理完成的作业,c#,.net,azure,azure-batch,C#,.net,Azure,Azure Batch,使用Azure批处理,我的项目使用带有函数和队列的基于事件的设计将作业添加到池中。作业完成后,即使所有任务都已完成,它仍处于“活动”状态 计时器会触发(使用应用程序服务计划的单个)功能,该计时器从队列中读取X数量的消息。职能: 创建池(如果不存在) 创造就业机会 将任务添加到该作业中 这很有效。但是,任务完成后,即使所有任务都已完成,作业状态仍保持活动状态。我希望作业终止/清理/将状态设置为“已完成” 我希望我的功能是短暂的,不希望有任何状态性。因此,我没有使用foreach(job.Com

使用Azure批处理,我的项目使用带有函数和队列的基于事件的设计将作业添加到池中。作业完成后,即使所有任务都已完成,它仍处于“活动”状态

计时器会触发(使用应用程序服务计划的单个)功能,该计时器从队列中读取X数量的消息。职能:

  • 创建池(如果不存在)
  • 创造就业机会
  • 将任务添加到该作业中
这很有效。但是,任务完成后,即使所有任务都已完成,作业状态仍保持活动状态。我希望作业终止/清理/将状态设置为“已完成”

我希望我的功能是短暂的,不希望有任何状态性。因此,我没有使用
foreach(job.CompletedTasks()中的CloudTask任务)
来等待任务的状态

另一种方法是使用任务依赖关系,这需要
batchClient.Utilities.CreateTaskStateMonitor()
,因此需要一种状态完整的方法

在基于事件的设计中使用Azure批处理的最佳方式是什么?具体来说,任务完成后如何终止/清理作业?

一旦作业下的所有任务完成,您可以让作业“自动完成”。CloudJob对象上有一个名为的属性

在向作业添加任务时,您首先需要将其设置为
NoAction
(默认值)。将所有任务添加到作业后,可以将该值更新为
TerminateJob
,然后调用
Commit()/CommitAsync()
。请注意,如果保留最初提交的CloudJob,则在修改属性并提交之前,需要先
Refresh()/RefreshAsync()
。或者,您可以
GetJob()/getjobsync()
,修改,然后提交


对于基于事件的设计,您可以查看启用,并查看这是否适合您的场景。

最终解决方案,在fpark回答后使用代码:

公共类编排器()
{
公共任务ExecuteAsync()
{
//创建包含计算节点的批处理池
//执行任务的。
var pool=await_batchManager.CreatePoolIfNotExistsAsync();
//创建运行任务的作业。
var job=wait _batchManager.CreateJobIfNotExistsAsync(_domain,pool.Id);
//从批处理服务获取绑定作业
等待作业。刷新异步();
//创建任务集合并将其添加到批处理作业中。
var tasks=wait _fileProcessingTasksFactory.CreateAsync(job.Id);
//将任务添加到作业中;任务将自动安排
//用于批处理服务在节点上执行。
等待作业。AddTaskAsync(任务);
job.OnAllTasksComplete=OnAllTasksComplete.TerminateJob;
等待作业。CommitAsync();
}
}
公共类BatchManager()
公共异步任务CreatePoolIfNotExistsAsync()
{
//用于创建和返回池的代码。
}
公共异步任务CreateJobIfNotExistsAsync(字符串域,字符串池ID)
{
//作业id不能包含:因此请替换它们。
var jobId=$“{domain}-{DateTime.UtcNow:s}”。替换(“:”,“-”;
var job=_parameters.BatchClient.JobOperations.CreateJob();
job.Id=jobId;
job.poolformation=新的poolformation{PoolId=PoolId};
等待作业。CommitAsync();
返回工作;
}
}
如果您试图直接使用
onalTasksComplete.TerminateJob
创建作业,您将收到以下错误:

Microsoft.Azure.Batch: This object is in an invalid state.  Write access is not allowed.
2018-03-27 07:57:40.738 +02:00 [Error] "636577269909538505" - Failure while scheduling Azure Batch tasks.
System.InvalidOperationException: This object is in an invalid state.  Write access is not allowed.
   at Microsoft.Azure.Batch.PropertyAccessor`1.ThrowIfReadOnly(Boolean overrideReadOnly)
   at Microsoft.Azure.Batch.PropertyAccessor`1.<>c__DisplayClass19_0.<SetValue>b__0()
   at Microsoft.Azure.Batch.PropertyAccessController.WriteProperty(Action propertyWriteAction, BindingAccess allowedAccess, String propertyName)
   at Microsoft.Azure.Batch.PropertyAccessor`1.SetValue(T value, Boolean overrideReadOnly, Boolean overrideAccessControl)
   at Microsoft.Azure.Batch.CloudJob.set_OnAllTasksComplete(Nullable`1 value)
   at BatchManager.CreateJobIfNotExist(String domain, String poolId) in C:\ProjectsGitHub\ProjectName\BatchManager.cs:line 107
   at FileProcessingOrchestrator.<ExecuteAsync>d__6.MoveNext() in C:\ProjectsGitHub\ProjectName\FileProcessingOrchestrator.cs:line 48
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Nnip.Qrs.EdgarDataProcessing.Parallelization.FunctionApp.ScheduleAzureBatchTasks.<Run>d__0.MoveNext() in C:\ProjectsGitHub\ProjectName\FunctionApp\ScheduleAzureBatchTasks.cs:line 93

Microsoft.Azure.Batch: This object is in an invalid state.  Write access is not allowed.
A ScriptHost error has occurred
Exception while executing function: ScheduleAzureBatchTasks. Microsoft.Azure.Batch: This object is in an invalid state.  Write access is not allowed.
Exception while executing function: ScheduleAzureBatchTasks
Exception while executing function: ScheduleAzureBatchTasks. Microsoft.Azure.Batch: This object is in an invalid state.  Write access is not allowed.
Function completed (Failure, Id=6173b9d2-5058-4a6d-9406-1cf00340774e, Duration=71076ms)

Executed 'ScheduleAzureBatchTasks' (Failed, Id=6173b9d2-5058-4a6d-9406-1cf00340774e)
System.Private.CoreLib: Exception while executing function: ScheduleAzureBatchTasks. Microsoft.Azure.Batch: This object is in an invalid state.  Write access is not allowed.
  Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is '6173b9d2-5058-4a6d-9406-1cf00340774e'
System.Private.CoreLib: Exception while executing function: ScheduleAzureBatchTasks. Microsoft.Azure.Batch: This object is in an invalid state.  Write access is not allowed.
Microsoft.Azure.Batch:此对象处于无效状态。不允许写访问。
2018-03-27 07:57:40.738+02:00[错误]“63657726990953855”-调度Azure批处理任务时失败。
System.InvalidOperationException:此对象处于无效状态。不允许写访问。
位于Microsoft.Azure.Batch.PropertyAccessor`1.ThrowIfReadOnly(布尔覆盖AdOnly)
在Microsoft.Azure.Batch.PropertyAccessor`1.c_uuDisplayClass19_0.b_u0()上
位于Microsoft.Azure.Batch.PropertyAccessController.WriteProperty(操作propertyWriteAction、BindingAccess allowedAccess、字符串propertyName)
位于Microsoft.Azure.Batch.PropertyAccessor`1.SetValue(T值、布尔重写Adonly、布尔重写AccessControl)
在Microsoft.Azure.Batch.CloudJob.set_onalTasksComplete上(可为null的`1值)
在C:\ProjectsGitHub\ProjectName\BatchManager.cs中的BatchManager.CreateJobIfNotExist(字符串域,字符串池ID)处:第107行
在C:\ProjectsGitHub\ProjectName\FileProcessingOrchestrator.cs中的FileProcessingOrchestrator.d\uu 6.MoveNext()处:第48行
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在C:\ProjectsGitHub\ProjectName\FunctionApp\ScheduleAzureBatchTasks.cs:第93行中的Nnip.Qrs.EdgarDataProcessing.Parallelization.FunctionPP.ScheduleAzureBatchTasks.d_u0.MoveNext()处
Microsoft.Azure.Batch:此对象处于无效状态。不允许写访问。
发生脚本主机错误
执行函数时出现异常:ScheduleAzureBatchTasks。Microsoft.Azure.Batch:此对象处于无效状态。不允许写访问。
执行函数时出现异常:ScheduleAzureBatchTasks
执行函数时出现异常:ScheduleAzureBatchTasks。Microsoft.Azure.Batch:此对象处于无效状态。不允许写访问。
功能完成(故障,Id=6173b9d2-5058-4a6d-9406-1cf00340774e,持续时间=71076ms)
已执行“ScheduleAzureBatchTasks”(失败,Id=6173b9d2-5058-4a6d-9406-1cf00340774e)
System.Private.CoreLib:执行函数ScheduleAzureBatchTasks时发生异常。Microsoft.Azure.Batch:此对象处于无效状态。不允许写访问。