Compare commits
No commits in common. "3c0926fdf35acee9272a3a811a2545ea00d39170" and "0af2ed3bb8b270a5f0d4db177e362803f7702e93" have entirely different histories.
3c0926fdf3
...
0af2ed3bb8
|
@ -12,10 +12,13 @@ namespace AcaMate.V1.Controllers;
|
|||
[ApiExplorerSettings(GroupName = "공통")]
|
||||
public class PushController : ControllerBase
|
||||
{
|
||||
|
||||
private readonly IWebHostEnvironment _env;
|
||||
private readonly ILogger<PushController> _logger;
|
||||
private readonly IPushQueue _pushQueue;
|
||||
public PushController(ILogger<PushController> logger, IPushQueue pushQueue)
|
||||
public PushController(IWebHostEnvironment env, ILogger<PushController> logger, IPushQueue pushQueue)
|
||||
{
|
||||
_env = env;
|
||||
_logger = logger;
|
||||
_pushQueue = pushQueue;
|
||||
}
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AcaMate.V1.Models;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace AcaMate.V1.Services;
|
||||
|
||||
public interface IPushQueue
|
||||
{
|
||||
void Enqueue(PushRequest request);
|
||||
Task<PushRequest> DequeueAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
public class InMemoryPushQueue: IPushQueue
|
||||
{
|
||||
private readonly ConcurrentQueue<PushRequest> _queue = new ConcurrentQueue<PushRequest>();
|
||||
private readonly SemaphoreSlim _signal = new SemaphoreSlim(0);
|
||||
|
||||
public void Enqueue(PushRequest request)
|
||||
{
|
||||
if( request is null )
|
||||
throw new ArgumentNullException(nameof(request));
|
||||
_queue.Enqueue(request);
|
||||
_signal.Release();
|
||||
}
|
||||
|
||||
public async Task<PushRequest> DequeueAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _signal.WaitAsync(cancellationToken);
|
||||
_queue.TryDequeue(out var request);
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
||||
public class PushBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly IPushQueue _queue;
|
||||
private readonly IApnsPushService _pushService;
|
||||
|
||||
public PushBackgroundService(IPushQueue queue, IApnsPushService pushService)
|
||||
{
|
||||
_queue = queue;
|
||||
_pushService = pushService;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var pushRequest = await _queue.DequeueAsync(stoppingToken);
|
||||
try
|
||||
{
|
||||
await _pushService.SendPushNotificationAsync(pushRequest.deviceToken, pushRequest.payload);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"푸시 전송 실패: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user