2022-03-15 18:00:53 +00:00
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
//
|
|
|
|
// This program is a free software product.
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
|
|
|
|
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
|
|
|
|
// any third-party rights.
|
|
|
|
//
|
|
|
|
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
//
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
//
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
//
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
//
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
|
|
|
2022-02-16 13:14:31 +00:00
|
|
|
namespace ASC.Notify;
|
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
[Singletone]
|
2022-02-16 13:14:31 +00:00
|
|
|
public class DbWorker
|
2019-07-29 09:15:48 +00:00
|
|
|
{
|
2022-02-16 13:14:31 +00:00
|
|
|
private readonly object _syncRoot = new object();
|
2022-07-28 12:29:03 +00:00
|
|
|
|
2022-02-16 14:41:00 +00:00
|
|
|
private readonly IServiceScopeFactory _serviceScopeFactory;
|
2022-04-15 09:08:06 +00:00
|
|
|
private readonly NotifyServiceCfg _notifyServiceCfg;
|
2022-02-16 13:14:31 +00:00
|
|
|
|
2022-05-13 19:14:00 +00:00
|
|
|
public DbWorker(IServiceScopeFactory serviceScopeFactory, IOptions<NotifyServiceCfg> notifyServiceCfg)
|
2019-07-29 09:15:48 +00:00
|
|
|
{
|
2022-02-16 14:41:00 +00:00
|
|
|
_serviceScopeFactory = serviceScopeFactory;
|
2022-04-15 09:08:06 +00:00
|
|
|
_notifyServiceCfg = notifyServiceCfg.Value;
|
2022-02-16 13:14:31 +00:00
|
|
|
}
|
2019-07-29 09:15:48 +00:00
|
|
|
|
2022-02-16 13:14:31 +00:00
|
|
|
public int SaveMessage(NotifyMessage m)
|
|
|
|
{
|
2022-02-16 14:41:00 +00:00
|
|
|
using var scope = _serviceScopeFactory.CreateScope();
|
2022-05-13 19:14:00 +00:00
|
|
|
|
|
|
|
var _mapper = scope.ServiceProvider.GetRequiredService<IMapper>();
|
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
using var dbContext = scope.ServiceProvider.GetService<IDbContextFactory<NotifyDbContext>>().CreateDbContext();
|
2019-07-29 09:15:48 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
var strategy = dbContext.Database.CreateExecutionStrategy();
|
2022-02-16 13:14:31 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
strategy.Execute(() =>
|
|
|
|
{
|
|
|
|
using var tx = dbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted);
|
2022-02-16 13:14:31 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
var notifyQueue = _mapper.Map<NotifyMessage, NotifyQueue>(m);
|
2022-02-16 13:14:31 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
notifyQueue = dbContext.NotifyQueue.Add(notifyQueue).Entity;
|
|
|
|
dbContext.SaveChanges();
|
2022-02-16 13:14:31 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
var id = notifyQueue.NotifyId;
|
2022-02-16 13:14:31 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
var info = new NotifyInfo
|
|
|
|
{
|
|
|
|
NotifyId = id,
|
|
|
|
State = 0,
|
|
|
|
Attempts = 0,
|
|
|
|
ModifyDate = DateTime.UtcNow,
|
|
|
|
Priority = m.Priority
|
|
|
|
};
|
|
|
|
|
|
|
|
dbContext.NotifyInfo.Add(info);
|
|
|
|
dbContext.SaveChanges();
|
2019-07-29 09:15:48 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
tx.Commit();
|
|
|
|
});
|
2022-07-28 12:29:03 +00:00
|
|
|
|
2022-02-16 13:14:31 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IDictionary<int, NotifyMessage> GetMessages(int count)
|
|
|
|
{
|
|
|
|
lock (_syncRoot)
|
2019-07-29 09:15:48 +00:00
|
|
|
{
|
2022-02-16 14:41:00 +00:00
|
|
|
using var scope = _serviceScopeFactory.CreateScope();
|
2022-05-13 19:14:00 +00:00
|
|
|
|
|
|
|
var _mapper = scope.ServiceProvider.GetRequiredService<IMapper>();
|
|
|
|
|
2022-07-28 12:29:03 +00:00
|
|
|
using var dbContext = scope.ServiceProvider.GetService<IDbContextFactory<NotifyDbContext>>().CreateDbContext();
|
|
|
|
|
2022-02-16 13:14:31 +00:00
|
|
|
var q = dbContext.NotifyQueue
|
|
|
|
.Join(dbContext.NotifyInfo, r => r.NotifyId, r => r.NotifyId, (queue, info) => new { queue, info })
|
2022-04-15 09:08:06 +00:00
|
|
|
.Where(r => r.info.State == (int)MailSendingState.NotSended || r.info.State == (int)MailSendingState.Error && r.info.ModifyDate < DateTime.UtcNow - TimeSpan.Parse(_notifyServiceCfg.Process.AttemptsInterval))
|
2022-02-16 13:14:31 +00:00
|
|
|
.OrderBy(i => i.info.Priority)
|
|
|
|
.ThenBy(i => i.info.NotifyId)
|
|
|
|
.Take(count);
|
2019-12-10 13:42:29 +00:00
|
|
|
|
|
|
|
|
2022-02-16 13:14:31 +00:00
|
|
|
var messages = q
|
|
|
|
.ToDictionary(
|
|
|
|
r => r.queue.NotifyId,
|
|
|
|
r =>
|
|
|
|
{
|
2022-02-18 13:56:23 +00:00
|
|
|
var res = _mapper.Map<NotifyQueue, NotifyMessage>(r.queue);
|
|
|
|
|
2022-02-16 13:14:31 +00:00
|
|
|
try
|
|
|
|
{
|
2022-05-13 19:14:00 +00:00
|
|
|
res.Attachments = JsonConvert.DeserializeObject<NotifyMessageAttachment[]>(r.queue.Attachments);
|
2022-02-16 13:14:31 +00:00
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
2019-08-15 15:08:40 +00:00
|
|
|
|
2022-02-16 13:14:31 +00:00
|
|
|
}
|
2022-02-18 13:56:23 +00:00
|
|
|
|
2022-02-16 13:14:31 +00:00
|
|
|
return res;
|
|
|
|
});
|
2019-12-10 13:42:29 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
var strategy = dbContext.Database.CreateExecutionStrategy();
|
2019-07-29 09:15:48 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
strategy.Execute(() =>
|
2019-07-29 09:15:48 +00:00
|
|
|
{
|
2022-05-25 13:55:38 +00:00
|
|
|
using var tx = dbContext.Database.BeginTransaction();
|
2019-12-10 13:42:29 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
var info = dbContext.NotifyInfo.Where(r => messages.Keys.Any(a => a == r.NotifyId)).ToList();
|
|
|
|
|
|
|
|
foreach (var i in info)
|
|
|
|
{
|
|
|
|
i.State = (int)MailSendingState.Sending;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
tx.Commit();
|
|
|
|
});
|
2019-08-15 15:08:40 +00:00
|
|
|
|
2022-02-16 13:14:31 +00:00
|
|
|
return messages;
|
2019-07-29 09:15:48 +00:00
|
|
|
}
|
2022-02-16 13:14:31 +00:00
|
|
|
}
|
2019-07-29 09:15:48 +00:00
|
|
|
|
2022-02-16 13:14:31 +00:00
|
|
|
public void ResetStates()
|
|
|
|
{
|
2022-02-16 14:41:00 +00:00
|
|
|
using var scope = _serviceScopeFactory.CreateScope();
|
2022-07-28 12:29:03 +00:00
|
|
|
using var dbContext = scope.ServiceProvider.GetService<IDbContextFactory<NotifyDbContext>>().CreateDbContext();
|
2022-02-16 13:14:31 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
var strategy = dbContext.Database.CreateExecutionStrategy();
|
2022-02-16 13:14:31 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
strategy.Execute(() =>
|
2019-07-29 09:15:48 +00:00
|
|
|
{
|
2022-05-25 13:55:38 +00:00
|
|
|
var tr = dbContext.Database.BeginTransaction();
|
|
|
|
var info = dbContext.NotifyInfo.Where(r => r.State == 1).ToList();
|
|
|
|
|
|
|
|
foreach (var i in info)
|
|
|
|
{
|
|
|
|
i.State = 0;
|
|
|
|
}
|
2019-12-10 13:42:29 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
dbContext.SaveChanges();
|
|
|
|
tr.Commit();
|
|
|
|
});
|
2022-02-16 13:14:31 +00:00
|
|
|
}
|
2019-12-10 13:42:29 +00:00
|
|
|
|
2022-02-16 13:14:31 +00:00
|
|
|
public void SetState(int id, MailSendingState result)
|
|
|
|
{
|
2022-02-16 14:41:00 +00:00
|
|
|
using var scope = _serviceScopeFactory.CreateScope();
|
2022-07-28 12:29:03 +00:00
|
|
|
using var dbContext = scope.ServiceProvider.GetService<IDbContextFactory<NotifyDbContext>>().CreateDbContext();
|
2022-02-16 13:14:31 +00:00
|
|
|
using var tx = dbContext.Database.BeginTransaction();
|
2019-12-10 13:42:29 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
var strategy = dbContext.Database.CreateExecutionStrategy();
|
|
|
|
|
|
|
|
strategy.Execute(() =>
|
2019-07-29 09:15:48 +00:00
|
|
|
{
|
2022-05-25 13:55:38 +00:00
|
|
|
if (result == MailSendingState.Sended)
|
|
|
|
{
|
|
|
|
var d = dbContext.NotifyInfo.Where(r => r.NotifyId == id).FirstOrDefault();
|
|
|
|
dbContext.NotifyInfo.Remove(d);
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
}
|
|
|
|
else
|
2019-07-29 09:15:48 +00:00
|
|
|
{
|
2022-05-25 13:55:38 +00:00
|
|
|
if (result == MailSendingState.Error)
|
2019-07-29 09:15:48 +00:00
|
|
|
{
|
2022-05-25 13:55:38 +00:00
|
|
|
var attempts = dbContext.NotifyInfo.Where(r => r.NotifyId == id).Select(r => r.Attempts).FirstOrDefault();
|
|
|
|
if (_notifyServiceCfg.Process.MaxAttempts <= attempts + 1)
|
|
|
|
{
|
|
|
|
result = MailSendingState.FatalError;
|
|
|
|
}
|
2019-07-29 09:15:48 +00:00
|
|
|
}
|
2019-12-10 13:42:29 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
var info = dbContext.NotifyInfo
|
|
|
|
.Where(r => r.NotifyId == id)
|
|
|
|
.ToList();
|
2019-12-10 13:42:29 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
foreach (var i in info)
|
|
|
|
{
|
|
|
|
i.State = (int)result;
|
|
|
|
i.Attempts += 1;
|
|
|
|
i.ModifyDate = DateTime.UtcNow;
|
|
|
|
}
|
2019-12-10 13:42:29 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
dbContext.SaveChanges();
|
|
|
|
}
|
2022-02-16 13:14:31 +00:00
|
|
|
|
2022-05-25 13:55:38 +00:00
|
|
|
tx.Commit();
|
|
|
|
});
|
2019-07-29 09:15:48 +00:00
|
|
|
}
|
2022-02-16 13:14:31 +00:00
|
|
|
}
|