Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jobs #564

Merged
merged 3 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 63 additions & 64 deletions ShareBook/ShareBook.Api/Controllers/OperationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,78 +15,77 @@
using System.Reflection;
using System.Threading.Tasks;

namespace ShareBook.Api.Controllers
namespace ShareBook.Api.Controllers;

[Route("api/[controller]")]
[EnableCors("AllowAllHeaders")]
public class OperationsController : Controller
{
[Route("api/[controller]")]
[EnableCors("AllowAllHeaders")]
public class OperationsController : Controller
{

protected IJobExecutor _executor;
protected string _validToken;
readonly IEmailService _emailService;
private readonly IWebHostEnvironment _env;
private readonly IMemoryCache _cache;
protected IJobExecutor _executor;
protected string _validToken;
readonly IEmailService _emailService;
private readonly IWebHostEnvironment _env;
private readonly IMemoryCache _cache;

public OperationsController(IJobExecutor executor, IOptions<ServerSettings> settings, IEmailService emailService, IWebHostEnvironment env, IMemoryCache memoryCache)
{
_executor = executor;
_validToken = settings.Value.JobExecutorToken;
_emailService = emailService;
_env = env;
_cache = memoryCache;
}

[HttpGet]
[Authorize("Bearer")]
[AuthorizationFilter(Permissions.Permission.ApproveBook)] // adm
[Route("ForceException")]
public IActionResult ForceException()
{
_ = 1 / Convert.ToInt32("Teste");
return BadRequest();
}
public OperationsController(IJobExecutor executor, IOptions<ServerSettings> settings, IEmailService emailService, IWebHostEnvironment env, IMemoryCache memoryCache)
{
_executor = executor;
_validToken = settings.Value.JobExecutorToken;
_emailService = emailService;
_env = env;
_cache = memoryCache;
}

[HttpGet("Ping")]
public IActionResult Ping()
{
var ass = Assembly.GetEntryAssembly();
var result = new
{
Service = ass.GetName().Name.ToString(),
Version = ass.GetName().Version.ToString(),
DotNetVersion = System.Environment.Version.ToString(),
BuildLinkerTime = ass.GetLinkerTime().ToString("dd/MM/yyyy HH:mm:ss:fff z"),
Env = _env.EnvironmentName,
TimeZone = TimeZoneInfo.Local.DisplayName,
System.Runtime.InteropServices.RuntimeInformation.OSDescription,
MemoryCacheCount = ((MemoryCache)_cache).Count
};
return Ok(result);
}
[HttpGet]
[Authorize("Bearer")]
[AuthorizationFilter(Permissions.Permission.ApproveBook)] // adm
[Route("ForceException")]
public IActionResult ForceException()
{
_ = 1 / Convert.ToInt32("Teste");
return BadRequest();
}

[HttpGet("JobExecutor")]
[Throttle(Name = "JobExecutor", Seconds = 5, VaryByIp = false)]
public async Task<IActionResult> ExecutorAsync()
[HttpGet("Ping")]
public IActionResult Ping()
{
var ass = Assembly.GetEntryAssembly();
var result = new
{
if (!_IsValidJobToken())
return Unauthorized();
else
return Ok(await _executor.ExecuteAsync());
}
Service = ass.GetName().Name.ToString(),
Version = ass.GetName().Version.ToString(),
DotNetVersion = System.Environment.Version.ToString(),
BuildLinkerTime = ass.GetLinkerTime().ToString("dd/MM/yyyy HH:mm:ss:fff z"),
Env = _env.EnvironmentName,
TimeZone = TimeZoneInfo.Local.DisplayName,
System.Runtime.InteropServices.RuntimeInformation.OSDescription,
MemoryCacheCount = ((MemoryCache)_cache).Count
};
return Ok(result);
}

[HttpPost("EmailTest")]
[Authorize("Bearer")]
[AuthorizationFilter(Permissions.Permission.ApproveBook)] // adm
public async Task<IActionResult> EmailTestAsync([FromBody] EmailTestVM emailVM)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
[HttpGet("JobExecutor")]
[Throttle(Name = "JobExecutor", Seconds = 5, VaryByIp = false)]
public async Task<IActionResult> ExecutorAsync()
{
if (!_IsValidJobToken())
return Unauthorized();
else
return Ok(await _executor.ExecuteAsync());
}

await _emailService.TestAsync(emailVM.Email, emailVM.Name);
return Ok();
}
[HttpPost("EmailTest")]
[Authorize("Bearer")]
[AuthorizationFilter(Permissions.Permission.ApproveBook)] // adm
public async Task<IActionResult> EmailTestAsync([FromBody] EmailTestVM emailVM)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);

protected bool _IsValidJobToken() => Request.Headers["Authorization"].ToString() == _validToken;
await _emailService.TestAsync(emailVM.Email, emailVM.Name);
return Ok();
}

protected bool _IsValidJobToken() => Request.Headers["Authorization"].ToString() == _validToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task NotCancellingAnyBook()

Assert.True(result.IsSuccess);
Assert.Equal("Encontradas 0 doações abandonadas com mais de 90 dias de atraso.\n\n", result.Details);
Assert.Equal(CancelAbandonedDonations.JobName, result.JobName);
Assert.Equal("CancelAbandonedDonations", result.JobName);

_mockBookService.Verify(c => c.GetBooksChooseDateIsLateAsync(), Times.Once);
_mockBookUserService.Verify(c => c.CancelAsync(It.IsAny<BookCancelationDTO>()), Times.Never);
Expand All @@ -69,7 +69,6 @@ public async Task Cancelling2Of4AbandonedBooks()

Assert.True(result.IsSuccess);
Assert.Equal("Encontradas 2 doações abandonadas com mais de 90 dias de atraso.\n\nDoação do livro Lord of the Rings foi cancelada.\nDoação do livro Lord of the Rings foi cancelada.\n", result.Details);
Assert.Equal(CancelAbandonedDonations.JobName, result.JobName);

_mockBookService.Verify(c => c.GetBooksChooseDateIsLateAsync(), Times.Once);
_mockBookUserService.Verify(c => c.CancelAsync(It.IsAny<BookCancelationDTO>()), Times.Exactly(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ public async Task SendReminderToTheUser()

Assert.True(result.IsSuccess);
Assert.Equal("Lembrete amigável enviado para 'TestUser' referente ao livro 'Lord of the Rings'.", result.Details);
Assert.Equal(ChooseDateReminder.JobName, result.JobName);

_mockBookService.Verify(c => c.GetBooksChooseDateIsTodayAsync(), Times.Once);
_mockEmailTemplate.Verify(c => c.GenerateHtmlFromTemplateAsync(It.Is<string>(v => v.Equals("ChooseDateReminderTemplate")), It.IsAny<object>()), Times.Once);
_mockEmailService.Verify(c => c.SendAsync(It.Is<string>(v => v.Equals(_user.Email)), It.Is<string>(v => v.Equals(_user.Name)), It.Is<string>(v => v.Equals(HtmlMock)), It.Is<string>(v => v.Equals(ChooseDateReminder.EmailSubject)), It.Is<bool>((v) => !v), It.Is<bool>((v) => v)), Times.Once);
//_mockEmailService.Verify(c => c.SendAsync(It.Is<string>(v => v.Equals(_user.Email)), It.Is<string>(v => v.Equals(_user.Name)), It.Is<string>(v => v.Equals(HtmlMock)), It.Is<bool>((v) => !v), It.Is<bool>((v) => v)), Times.Once);

_mockBookService.VerifyNoOtherCalls();
_mockEmailService.VerifyNoOtherCalls();
//_mockEmailService.VerifyNoOtherCalls();
_mockEmailTemplate.VerifyNoOtherCalls();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public async Task SendSoftEmailToTheUserAndToAdmins_1BookLate()

Assert.True(result.IsSuccess);
Assert.Equal($"Encontradas 1 doações em atraso de 1 doadores distintos.E-mail enviado para o usuário: {_softUser.Name}", result.Details);
Assert.Equal(LateDonationNotification.JobName, result.JobName);

_mockConfiguration.Verify(c => c[LateDonationNotification.ConfigMaxLateDonationDaysKey], Times.Once);
_mockEmailTemplate.Verify(c => c.GenerateHtmlFromTemplateAsync(It.Is<string>(v => v.Equals(LateDonationNotification.EmailTemplateName)), It.IsAny<object>()), Times.Once);
Expand Down Expand Up @@ -78,7 +77,6 @@ public async Task SendHardEmailToTheUserAndToAdmins_1BookLate()

Assert.True(result.IsSuccess);
Assert.Equal($"Encontradas 1 doações em atraso de 1 doadores distintos.E-mail enviado para o usuário: {_hardUser.Name}", result.Details);
Assert.Equal(LateDonationNotification.JobName, result.JobName);

_mockConfiguration.Verify(c => c[LateDonationNotification.ConfigMaxLateDonationDaysKey], Times.Once);
_mockEmailTemplate.Verify(c => c.GenerateHtmlFromTemplateAsync(It.Is<string>(v => v.Equals(LateDonationNotification.EmailTemplateName)), It.IsAny<object>()), Times.Once);
Expand Down Expand Up @@ -106,7 +104,6 @@ public async Task NotSendAnyEmail_0BooksLate()

Assert.True(result.IsSuccess);
Assert.Equal("Encontradas 0 doações em atraso de 0 doadores distintos.", result.Details);
Assert.Equal(LateDonationNotification.JobName, result.JobName);

_mockConfiguration.Verify(c => c[LateDonationNotification.ConfigMaxLateDonationDaysKey], Times.Once);
_mockBookService.Verify(c => c.GetBooksChooseDateIsLateAsync(), Times.Once);
Expand Down
9 changes: 4 additions & 5 deletions ShareBook/Sharebook.Jobs/Executor/IJobExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using ShareBook.Domain.Common;
using System.Threading.Tasks;

namespace Sharebook.Jobs
namespace Sharebook.Jobs;

public interface IJobExecutor
{
public interface IJobExecutor
{
Task<JobExecutorResult> ExecuteAsync();
}
Task<JobExecutorResult> ExecuteAsync();
}
89 changes: 44 additions & 45 deletions ShareBook/Sharebook.Jobs/Jobs/0 - CancelAbandonedDonations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,58 @@
using System.Linq;
using System.Threading.Tasks;

namespace Sharebook.Jobs
namespace Sharebook.Jobs;

public class CancelAbandonedDonations : GenericJob, IJob
{
public class CancelAbandonedDonations : GenericJob, IJob
{
private readonly IBookService _bookService;
private readonly IBookUserService _bookUserService;
private readonly int _maxLateDonationDaysAutoCancel;
private readonly IConfiguration _configuration;
public new const string JobName = "CancelAbandonedDonations";
private readonly IBookService _bookService;
private readonly IBookUserService _bookUserService;
private readonly int _maxLateDonationDaysAutoCancel;
private readonly IConfiguration _configuration;

public CancelAbandonedDonations(IJobHistoryRepository jobHistoryRepo, IBookService bookService, IBookUserService bookUserService, IConfiguration configuration) : base(jobHistoryRepo)
{
Description = "Cancela as doações abandonadas.";
Interval = Interval.Dayly;
Active = true;
BestTimeToExecute = new TimeSpan(6, 0, 0);
public CancelAbandonedDonations(IJobHistoryRepository jobHistoryRepo, IBookService bookService, IBookUserService bookUserService, IConfiguration configuration) : base(jobHistoryRepo)
{
JobName = "CancelAbandonedDonations";
Description = "Cancela as doações abandonadas.";
Interval = Interval.Dayly;
Active = true;
BestTimeToExecute = new TimeSpan(6, 0, 0);

_bookService = bookService;
_bookUserService = bookUserService;
_bookService = bookService;
_bookUserService = bookUserService;

_configuration = configuration;
_maxLateDonationDaysAutoCancel = int.Parse(_configuration["SharebookSettings:MaxLateDonationDaysAutoCancel"]);
}
_configuration = configuration;
_maxLateDonationDaysAutoCancel = int.Parse(_configuration["SharebookSettings:MaxLateDonationDaysAutoCancel"]);
}

public override async Task<JobHistory> WorkAsync()
{
var booksLate = await _bookService.GetBooksChooseDateIsLateAsync();
public override async Task<JobHistory> WorkAsync()
{
var booksLate = await _bookService.GetBooksChooseDateIsLateAsync();

var refDate = DateTime.Today.AddDays(_maxLateDonationDaysAutoCancel * -1);
var booksAbandoned = booksLate.Where(b => b.ChooseDate < refDate).ToList();
var refDate = DateTime.Today.AddDays(_maxLateDonationDaysAutoCancel * -1);
var booksAbandoned = booksLate.Where(b => b.ChooseDate < refDate).ToList();

var details = $"Encontradas {booksAbandoned.Count} doações abandonadas com mais de {_maxLateDonationDaysAutoCancel} dias de atraso.\n\n";

foreach(var book in booksAbandoned)
var details = $"Encontradas {booksAbandoned.Count} doações abandonadas com mais de {_maxLateDonationDaysAutoCancel} dias de atraso.\n\n";

foreach(var book in booksAbandoned)
{
var dto = new BookCancelationDTO
{
var dto = new BookCancelationDTO
{
Book = book,
CanceledBy = "ShareBot",
Reason = $"Cancelamento automático de doação abandonada. Com mais de {_maxLateDonationDaysAutoCancel} dias de atraso.",
};
Book = book,
CanceledBy = "ShareBot",
Reason = $"Cancelamento automático de doação abandonada. Com mais de {_maxLateDonationDaysAutoCancel} dias de atraso.",
};

await _bookUserService.CancelAsync(dto);
details += $"Doação do livro {book.Title} foi cancelada.\n";
}
return new JobHistory()
{
JobName = JobName,
IsSuccess = true,
Details = details
};
}
await _bookUserService.CancelAsync(dto);
details += $"Doação do livro {book.Title} foi cancelada.\n";
}

return new JobHistory()
{
JobName = JobName,
IsSuccess = true,
Details = details
};
}

}
}
Loading
Loading