-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving requesting push message delivery to background. Resolves #4
- Loading branch information
Showing
11 changed files
with
152 additions
and
15 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
13 changes: 13 additions & 0 deletions
13
Demo.AspNetCore.PushNotifications.Services.Abstractions/IPushNotificationsQueue.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Lib.Net.Http.WebPush; | ||
|
||
namespace Demo.AspNetCore.PushNotifications.Services.Abstractions | ||
{ | ||
public interface IPushNotificationsQueue | ||
{ | ||
void Enqueue(PushMessage message); | ||
|
||
Task<PushMessage> DequeueAsync(CancellationToken cancellationToken); | ||
} | ||
} |
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
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
65 changes: 65 additions & 0 deletions
65
Demo.AspNetCore.PushNotifications.Services/PushNotificationsDequeuer.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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Lib.Net.Http.WebPush; | ||
using Demo.AspNetCore.PushNotifications.Services.Abstractions; | ||
|
||
namespace Demo.AspNetCore.PushNotifications.Services | ||
{ | ||
internal class PushNotificationsDequeuer : IHostedService | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly IPushNotificationsQueue _messagesQueue; | ||
private readonly IPushNotificationService _notificationService; | ||
private readonly CancellationTokenSource _stopTokenSource = new CancellationTokenSource(); | ||
|
||
private Task _dequeueMessagesTask; | ||
|
||
public PushNotificationsDequeuer(IServiceProvider serviceProvider, IPushNotificationsQueue messagesQueue, IPushNotificationService notificationService) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_messagesQueue = messagesQueue; | ||
_notificationService = notificationService; | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_dequeueMessagesTask = Task.Run(DequeueMessagesAsync); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
_stopTokenSource.Cancel(); | ||
|
||
return Task.WhenAny(_dequeueMessagesTask, Task.Delay(Timeout.Infinite, cancellationToken)); | ||
} | ||
|
||
private async Task DequeueMessagesAsync() | ||
{ | ||
while (!_stopTokenSource.IsCancellationRequested) | ||
{ | ||
PushMessage message = await _messagesQueue.DequeueAsync(_stopTokenSource.Token); | ||
|
||
if (!_stopTokenSource.IsCancellationRequested) | ||
{ | ||
using (IServiceScope serviceScope = _serviceProvider.CreateScope()) | ||
{ | ||
IPushSubscriptionStore subscriptionStore = serviceScope.ServiceProvider.GetRequiredService<IPushSubscriptionStore>(); | ||
|
||
await subscriptionStore.ForEachSubscriptionAsync((PushSubscription subscription) => | ||
{ | ||
// Fire-and-forget | ||
_notificationService.SendNotificationAsync(subscription, message, _stopTokenSource.Token); | ||
}, _stopTokenSource.Token); | ||
} | ||
|
||
} | ||
} | ||
|
||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Demo.AspNetCore.PushNotifications.Services/PushNotificationsQueue.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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Collections.Concurrent; | ||
using Lib.Net.Http.WebPush; | ||
using Demo.AspNetCore.PushNotifications.Services.Abstractions; | ||
|
||
namespace Demo.AspNetCore.PushNotifications.Services | ||
{ | ||
internal class PushNotificationsQueue : IPushNotificationsQueue | ||
{ | ||
private readonly ConcurrentQueue<PushMessage> _messages = new ConcurrentQueue<PushMessage>(); | ||
private readonly SemaphoreSlim _messageEnqueuedSignal = new SemaphoreSlim(0); | ||
|
||
public void Enqueue(PushMessage message) | ||
{ | ||
if (message == null) | ||
{ | ||
throw new ArgumentNullException(nameof(message)); | ||
} | ||
|
||
_messages.Enqueue(message); | ||
|
||
_messageEnqueuedSignal.Release(); | ||
} | ||
|
||
public async Task<PushMessage> DequeueAsync(CancellationToken cancellationToken) | ||
{ | ||
await _messageEnqueuedSignal.WaitAsync(cancellationToken); | ||
|
||
_messages.TryDequeue(out PushMessage message); | ||
|
||
return message; | ||
} | ||
} | ||
} |
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