DocSpace-client/common/services/ASC.Notify/NotifySender.cs

111 lines
3.3 KiB
C#
Raw Normal View History

2022-02-21 12:28:52 +00:00
namespace ASC.Notify;
2022-02-16 13:14:31 +00:00
[Singletone]
2022-02-21 12:28:52 +00:00
public class NotifySender : BackgroundService
2020-10-19 15:53:15 +00:00
{
2022-02-16 13:14:31 +00:00
private readonly DbWorker _db;
2022-02-21 12:28:52 +00:00
private readonly ILog _logger;
private readonly NotifyServiceCfg _notifyServiceCfg;
2022-02-16 13:14:31 +00:00
2022-02-21 12:28:52 +00:00
public NotifySender(
IOptions<NotifyServiceCfg> notifyServiceCfg,
DbWorker dbWorker,
IOptionsMonitor<ILog> options)
2022-02-16 13:14:31 +00:00
{
2022-02-21 12:28:52 +00:00
_logger = options.Get("ASC.NotifySender");
_notifyServiceCfg = notifyServiceCfg.Value;
2022-02-16 13:14:31 +00:00
_db = dbWorker;
}
2022-02-21 12:28:52 +00:00
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2022-02-16 13:14:31 +00:00
{
2022-02-21 12:28:52 +00:00
_logger.Info("Notify Sender Service running.");
2022-02-16 13:14:31 +00:00
2022-02-21 12:28:52 +00:00
while (!stoppingToken.IsCancellationRequested)
{
await ThreadManagerWork(stoppingToken);
}
_logger.Info("Notify Sender Service is stopping.");
2022-02-16 13:14:31 +00:00
}
2022-02-21 12:28:52 +00:00
private async Task ThreadManagerWork(CancellationToken stoppingToken)
2022-02-16 13:14:31 +00:00
{
2022-02-21 12:28:52 +00:00
var tasks = new List<Task>(_notifyServiceCfg.Process.MaxThreads);
2022-02-16 13:14:31 +00:00
2022-02-21 12:28:52 +00:00
try
2022-02-16 13:14:31 +00:00
{
2022-02-21 12:28:52 +00:00
if (tasks.Count < _notifyServiceCfg.Process.MaxThreads)
2022-02-16 13:14:31 +00:00
{
2022-02-21 12:28:52 +00:00
var messages = _db.GetMessages(_notifyServiceCfg.Process.BufferSize);
if (messages.Count > 0)
2022-02-16 13:14:31 +00:00
{
2022-02-21 12:28:52 +00:00
var t = new Task(() => SendMessages(messages, stoppingToken), stoppingToken, TaskCreationOptions.LongRunning);
tasks.Add(t);
t.Start(TaskScheduler.Default);
2022-02-16 13:14:31 +00:00
}
else
{
2022-02-21 12:28:52 +00:00
await Task.Delay(5000, stoppingToken);
2022-02-16 13:14:31 +00:00
}
}
2022-02-21 12:28:52 +00:00
else
2022-02-16 13:14:31 +00:00
{
2022-02-21 12:28:52 +00:00
await Task.WhenAny(tasks.ToArray()).ContinueWith(r => tasks.RemoveAll(a => a.IsCompleted));
2022-02-16 13:14:31 +00:00
}
2020-11-20 12:49:57 +00:00
}
2022-02-21 12:28:52 +00:00
catch (ThreadAbortException)
{
return;
}
catch (Exception e)
{
_logger.Error(e);
}
2022-02-16 13:14:31 +00:00
}
2020-11-20 12:49:57 +00:00
2022-02-21 12:28:52 +00:00
private void SendMessages(object messages, CancellationToken stoppingToken)
2022-02-16 13:14:31 +00:00
{
try
2020-11-20 12:49:57 +00:00
{
2022-02-16 13:14:31 +00:00
foreach (var m in (IDictionary<int, NotifyMessage>)messages)
2020-11-20 12:49:57 +00:00
{
2022-02-21 12:28:52 +00:00
if (stoppingToken.IsCancellationRequested)
2022-02-16 13:14:31 +00:00
{
return;
}
var result = MailSendingState.Sended;
try
{
2022-02-21 12:28:52 +00:00
var sender = _notifyServiceCfg.Senders.FirstOrDefault(r => r.Name == m.Value.SenderType);
2022-02-16 13:14:31 +00:00
if (sender != null)
{
sender.NotifySender.Send(m.Value);
}
else
{
result = MailSendingState.FatalError;
}
_logger.DebugFormat("Notify #{0} has been sent.", m.Key);
}
catch (Exception e)
{
result = MailSendingState.FatalError;
_logger.Error(e);
}
_db.SetState(m.Key, result);
2020-11-20 12:49:57 +00:00
}
}
2022-02-16 13:14:31 +00:00
catch (ThreadAbortException)
{
return;
}
catch (Exception e)
{
_logger.Error(e);
}
}
2022-02-21 12:28:52 +00:00
}