protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { EgressRequest egressRequest = await _queue.DequeueAsync(stoppingToken); //Note we do not await these tasks, but we do limit how many can be executed at the same time _ = Task.Run(() => ExecuteEgressOperation(egressRequest, stoppingToken), stoppingToken); } }
public ValueTask EnqueueAsync( EgressRequest workItem) { if (workItem == null) { throw new ArgumentNullException(nameof(workItem)); } return(_queue.Writer.WriteAsync(workItem)); }
private async Task ExecuteEgressOperation(EgressRequest egressRequest, CancellationToken stoppingToken) { //We have two stopping tokens, one per item that can be triggered via Delete //and if we are stopping the service using (CancellationTokenSource linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(egressRequest.CancellationTokenSource.Token, stoppingToken)) { CancellationToken token = linkedTokenSource.Token; token.ThrowIfCancellationRequested(); var result = await egressRequest.EgressOperation.ExecuteAsync(_serviceProvider, token); //It is possible that this operation never completes, due to infinite duration operations. _operationsStore.CompleteOperation(egressRequest.OperationId, result); } }