public async Task RunAsync(CancellationToken cancellationToken) { IReliableConcurrentQueue <DependencyUpdateItem> queue = await StateManager.GetOrAddAsync <IReliableConcurrentQueue <DependencyUpdateItem> >("queue"); while (!cancellationToken.IsCancellationRequested) { try { using (ITransaction tx = StateManager.CreateTransaction()) { ConditionalValue <DependencyUpdateItem> maybeItem = await queue.TryDequeueAsync( tx, cancellationToken); if (maybeItem.HasValue) { DependencyUpdateItem item = maybeItem.Value; using (Logger.BeginScope( "Processing dependency update for build {buildId} in channel {channelId}", item.BuildId, item.ChannelId)) { await UpdateDependenciesAsync(item.BuildId, item.ChannelId); } } await tx.CommitAsync(); } await Task.Delay(1000, cancellationToken); } catch (TaskCanceledException tcex) when(tcex.CancellationToken == cancellationToken) { // ignore } catch (Exception ex) { Logger.LogError(ex, "Processing queue messages"); } } }
public async Task <TimeSpan> RunAsync(CancellationToken cancellationToken) { IReliableConcurrentQueue <DependencyUpdateItem> queue = await StateManager.GetOrAddAsync <IReliableConcurrentQueue <DependencyUpdateItem> >("queue"); try { using (ITransaction tx = StateManager.CreateTransaction()) { ConditionalValue <DependencyUpdateItem> maybeItem = await queue.TryDequeueAsync( tx, cancellationToken); if (maybeItem.HasValue) { DependencyUpdateItem item = maybeItem.Value; using (Logger.BeginScope( "Processing dependency update for build {buildId} in channel {channelId}", item.BuildId, item.ChannelId)) { await UpdateDependenciesAsync(item.BuildId, item.ChannelId); } } await tx.CommitAsync(); } } catch (TaskCanceledException tcex) when(tcex.CancellationToken == cancellationToken) { return(TimeSpan.MaxValue); } catch (Exception ex) { Logger.LogError(ex, "Processing queue messages"); } return(TimeSpan.FromSeconds(1)); }