-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use FrozenDictionary to store channels
- Loading branch information
Showing
6 changed files
with
19 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 8 additions & 8 deletions
16
src/DCA.Extensions.BackgroundTask/BackgroundTaskHostedService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
using System.Collections.ObjectModel; | ||
using System.Collections.Frozen; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace DCA.Extensions.BackgroundTask; | ||
|
||
/// <summary> | ||
/// A hosted service to manage task channels' lifetime | ||
/// </summary> | ||
/// <param name="logger"></param> | ||
/// <summary> | ||
/// A hosted service to manage task channels' lifetime | ||
/// </summary> | ||
/// <param name="logger"></param> | ||
/// <param name="channels"></param> | ||
public class BackgroundTaskHostedService( | ||
ILogger<BackgroundTaskHostedService> logger, | ||
ReadOnlyCollection<BackgroundTaskChannel> channels) : IHostedService | ||
FrozenDictionary<string, BackgroundTaskChannel> channels) : IHostedService | ||
{ | ||
private readonly ILogger _logger = logger; | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("Starting channels..."); | ||
await Task.WhenAll(channels.Select(x => x.StartAsync())); | ||
await Task.WhenAll(channels.Values.Select(x => x.StartAsync())); | ||
_logger.LogInformation("Started channels..."); | ||
} | ||
|
||
public async Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("Stopping background channels..."); | ||
await Task.WhenAny(channels.Select(x => x.StopAsync())); | ||
await Task.WhenAny(channels.Values.Select(x => x.StopAsync())); | ||
_logger.LogInformation("Stopped background channels..."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters