1
我有一個ReliableQueue<MyTask>
,它被排隊到一個不同的作用域中,並且將事務排隊到事務中,然後想要在每個任務上運行一些長時間運行的計算。StatefulService中的嵌套事務,用於保存異常事務的異步狀態
這裏的問題是,如果我的隊列事務被中止,我不想失去長計算的實例。它將繼續在後臺運行,獨立運行,我只是想檢查一下它是否完成,一旦我重新嘗試處理任務。
代碼段:
public void protected override async Task RunAsync(CancellationToken cancellationToken)
{
var queue = await StateManager.GetOrAddAsync<IReliableQueue<MyTask>>(...);
while(!cancellationToken.IsCancellationRequested)
{
using (var transaction = ...)
{
var myTaskConditional = await queue.TryDequeueAsync(transaction);
if (!myTaskConditional.HasValue)
{
break;
}
await DoLongProcessing(myTaskConditional)
await transaction.CommitAsync();
}
}
}
private async void DoLongProcessing(MyTask myTask) {
var dict = await StateManager.GetOrAddAsync<IReliableDictionary<Guid,Guid>>(...);
Conditional<Guid> guidConditional;
using (var transaction = ...)
{
guidConditional = await dict.TryGetValueAsync(myTask.TaskGuid);
if (guidConditional.HasValue) {
await transaction.CommitAsync();
// continue handling knowing we already started, continue to wait for
await WaitForClaulcationFinish(guidConditional.Value);
}
else {
// start handling knowing we never handled this task, create new guid and store it in dict
var runGuid = await StartRunningCalculation(runGuid);
await dict.AddAsync(myTask.TaskGuid, runGuid);
await transaction.CommitAsync();
await WaitForClaulcationFinish(runGuid);
}
}
}
我擔心的:我使用嵌套事務,並且不推薦使用。
如果我單獨爲ReliableQueue
或ReliableDictionary
單獨使用交易,實際上是否存在死鎖風險?
有沒有一個更好的目標設計,我想要實現的目標?
StartRunningCalculation是做什麼的? –
@FrancescoB。它運行Python腳本並進行長時間的計算 – Mugen