Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
goatrocks committed May 18, 2022
1 parent c3b3ccc commit 257f38e
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,39 @@ protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, A

}

protected abstract void Results(AsyncCodeActivityContext context, IEnumerable<TOutput> result);
protected override async Task<Action<AsyncCodeActivityContext>> ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
{
// Inputs
var timeout = TimeoutMS.Get(context);

var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(timeout);

// Set a timeout on the execution
var task = ExecuteWithTimeout(context, cts.Token);
var timer = Task.Delay(timeout, cts.Token);
var completedTask = await Task.WhenAny(task, timer);
if (completedTask == task)
{
// Outputs
return (ctx) =>
{
Results(ctx, task.Result);
};
}

else
{
throw new TimeoutException(Resources.Timeout_Error);
}


}

protected abstract void Results(AsyncCodeActivityContext context, TOutput result);

protected abstract Task<TOutput> ExecuteWithTimeout(AsyncCodeActivityContext context, CancellationToken cancellationToken = default);
}
}


0 comments on commit 257f38e

Please sign in to comment.