Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
refactor(back-end): Improve the code of hosted services and correct s…
Browse files Browse the repository at this point in the history
…pelling errors

The constant 'DELAY_TIME' is renamed with the name of 'DelayTime', also the use of TimeConstants was replaced using the TimeSpan class.
Some methods were also refactored, extracting from these some functions.
  • Loading branch information
CarlosPavajeau committed Jan 22, 2021
1 parent 0c2cf0c commit b1736ed
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
13 changes: 4 additions & 9 deletions Kaizen/HostedServices/BackgroundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ namespace Kaizen.HostedServices
public abstract class BackgroundService : IHostedService, IDisposable
{
private Task _executingTask;
private readonly CancellationTokenSource _stoppingCts = new CancellationTokenSource();
private readonly CancellationTokenSource _stoppingCts = new();

protected abstract Task ExecuteAsync(CancellationToken cancellationToken);

public virtual Task StartAsync(CancellationToken cancellationToken)
{
_executingTask = ExecuteAsync(_stoppingCts.Token);

if (_executingTask.IsCompleted)
{
return _executingTask;
}

return Task.CompletedTask;
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
}

public virtual async Task StopAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -49,9 +44,9 @@ public void Dispose()

protected virtual void Dispose(bool disposing)
{
if (disposing && _stoppingCts is not null)
if (disposing)
{
_stoppingCts.Cancel();
_stoppingCts?.Cancel();
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Kaizen/HostedServices/EmployeeContractHostedService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Kaizen.Core.Defines;
using Kaizen.Core.Services;
using Kaizen.Domain.Entities;
using Kaizen.Domain.Repositories;
Expand All @@ -10,7 +10,7 @@ namespace Kaizen.HostedServices
{
public class EmployeeContractHostedService : BackgroundService
{
private static readonly int DELAY_TIME = 24 /*Hours*/ * TimeConstants.Minutes * TimeConstants.Seconds * TimeConstants.Milliseconds;
private static readonly int DelayTime = TimeSpan.FromDays(1.0).Milliseconds;

private readonly IEmployeesRepository _employeesRepository;
private readonly IMailService _mailService;
Expand All @@ -24,7 +24,7 @@ public EmployeeContractHostedService(IEmployeesRepository employeesRepository, I
_mailTemplate = mailTemplate;
}

protected async override Task ExecuteAsync(CancellationToken cancellationToken)
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
Expand All @@ -38,7 +38,7 @@ protected async override Task ExecuteAsync(CancellationToken cancellationToken)
await _mailService.SendEmailAsync(employee.User.Email, "Contrato a punto de vencer", mailMessage, true);
}

await Task.Delay(DELAY_TIME, cancellationToken);
await Task.Delay(DelayTime, cancellationToken);
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions Kaizen/HostedServices/OverdueBillsHostedService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Kaizen.Core.Defines;
using Kaizen.Domain.Entities;
using Kaizen.Domain.Repositories;
using Kaizen.Hubs;
Expand All @@ -11,7 +11,7 @@ namespace Kaizen.HostedServices
{
public class OverdueBillsHostedService : BackgroundService
{
private static readonly int DELAY_TIME = 24 /*Hours*/ * TimeConstants.Minutes * TimeConstants.Seconds * TimeConstants.Milliseconds;
private static readonly int DelayTime = TimeSpan.FromDays(1.0).Milliseconds;

private readonly IProductInvoicesRepository _productInvoicesRepository;
private readonly IServiceInvoicesRepository _serviceInvoicesRepository;
Expand All @@ -31,15 +31,15 @@ IHubContext<InvoiceHub> invoiceHub
_invoiceHub = invoiceHub;
}

protected async override Task ExecuteAsync(CancellationToken cancellationToken)
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
await FindAndReportExpiredProductInvoices(cancellationToken);

await FindAndReportExpiredServiceInvoices(cancellationToken);

await Task.Delay(DELAY_TIME, cancellationToken);
await Task.Delay(DelayTime, cancellationToken);
}
}

Expand All @@ -54,7 +54,7 @@ private async Task FindAndReportExpiredProductInvoices(CancellationToken cancell

await _unitWork.SaveAsync();

await _invoiceHub.Clients.Group("Administrator").SendAsync("OvedureProductBills", productInvoices, cancellationToken: cancellationToken);
await _invoiceHub.Clients.Group("Administrator").SendAsync("OverdueProductBills", productInvoices, cancellationToken: cancellationToken);
}

private async Task FindAndReportExpiredServiceInvoices(CancellationToken cancellationToken)
Expand All @@ -68,7 +68,7 @@ private async Task FindAndReportExpiredServiceInvoices(CancellationToken cancell

await _unitWork.SaveAsync();

await _invoiceHub.Clients.Group("Administrator").SendAsync("OvedureServiceBills", serviceInvoices, cancellationToken: cancellationToken);
await _invoiceHub.Clients.Group("Administrator").SendAsync("OverdueServiceBills", serviceInvoices, cancellationToken: cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Kaizen.Core.Defines;
using Kaizen.Core.Services;
using Kaizen.Domain.Entities;
using Kaizen.Domain.Repositories;
Expand All @@ -14,7 +13,7 @@ namespace Kaizen.HostedServices
{
public class PendingActivitiesToBeConfirmedHostedService : BackgroundService
{
private static readonly int DELAY_TIME = 24 /*Hours*/ * TimeConstants.Minutes * TimeConstants.Seconds * TimeConstants.Milliseconds;
private static readonly int DelayTime = TimeSpan.FromDays(1.0).Milliseconds;

private readonly IActivitiesRepository _activitiesRepository;
private readonly IMailService _mailService;
Expand All @@ -37,7 +36,7 @@ LinkGenerator generator
_generator = generator;
}

protected async override Task ExecuteAsync(CancellationToken cancellationToken)
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
Expand All @@ -51,19 +50,30 @@ protected async override Task ExecuteAsync(CancellationToken cancellationToken)

foreach (Activity activity in pendingActivities)
{
string activityConfirmationLink = _generator.GetUriByAction(_accessor.HttpContext, $"ConfirmActivity/{activity.Code}", "activity_schedule");
string activityRejectLink = _generator.GetUriByAction(_accessor.HttpContext, $"RejectActivity/{activity.Code}", "activity_schedule");
string changeDateLink = _generator.GetUriByAction(_accessor.HttpContext, $"ChangeDate/{activity.Code}", "activity_schedule");

string mailMessage = _mailTemplate.LoadTemplate("PendingActivityToBeConfirmed.html",
$"{activity.Client.FirstName} {activity.Client.SecondName} {activity.Client.LastName} {activity.Client.SecondLastName}",
$"{activity.Date}", activityConfirmationLink, activityRejectLink, changeDateLink);

await _mailService.SendEmailAsync(activity.Client.User.Email, "Actividad pendiente a confirmación", mailMessage, true);
await SendPendingActivityEmail(activity);
}

await Task.Delay(DELAY_TIME, cancellationToken);
await Task.Delay(DelayTime, cancellationToken);
}
}

private async Task SendPendingActivityEmail(Activity activity)
{
string activityConfirmationLink = GetActivityLink("ConfirmActivity", activity.Code);
string activityRejectLink = GetActivityLink("RejectActivity", activity.Code);
string changeDateLink = GetActivityLink("ChangeDate", activity.Code);

string mailMessage = _mailTemplate.LoadTemplate("PendingActivityToBeConfirmed.html",
$"{activity.Client.FirstName} {activity.Client.SecondName} {activity.Client.LastName} {activity.Client.SecondLastName}",
$"{activity.Date}", activityConfirmationLink, activityRejectLink, changeDateLink);

await _mailService.SendEmailAsync(activity.Client.User.Email, "Actividad pendiente a confirmación", mailMessage,
true);
}

private string GetActivityLink(string action, int activityCode)
{
return _generator.GetUriByAction(_accessor.HttpContext, $"{action}/{activityCode}", "activity_schedule");
}
}
}

0 comments on commit b1736ed

Please sign in to comment.