Skip to content
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
34 changes: 22 additions & 12 deletions Ticky.Internal/Services/Hosted/AbstractHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class AbstractHostedService<T> : IHostedService
private TimeSpan _untilStart;
private TimeSpan _frequency;

public AbstractHostedService(
protected AbstractHostedService(
IServiceScopeFactory serviceScopeFactory,
TimeSpan untilStart,
TimeSpan frequency
Expand All @@ -34,7 +34,7 @@ public Task StartAsync(CancellationToken cancellationToken)
{
using var scope = ServiceScopeFactory.CreateScope();

Logger.LogDebug($"{GetType().Name} starting.");
Logger.LogDebug("{Name} starting.", GetType().Name);

OnStart();

Expand All @@ -45,26 +45,33 @@ public Task StopAsync(CancellationToken cancellationToken)
{
using var scope = ServiceScopeFactory.CreateScope();

Logger.LogDebug($"{GetType().Name} stopping.");
Logger.LogDebug("{Name} stopping.", GetType().Name);

_timer!.Change(Timeout.Infinite, 0);
Dispose();
OnStop();
return Task.CompletedTask;
}

private void DoWork(object? state)
private async void DoWork(object? state)
{
var startedAt = DateTime.Now;
using var scope = ServiceScopeFactory.CreateScope();
try
{
var startedAt = DateTime.Now;
using var scope = ServiceScopeFactory.CreateScope();

Logger.LogDebug($"{GetType().Name} job running.");
Logger.LogDebug("{Name} job running.", GetType().Name);

OnRun();
await OnRun();

Logger.LogDebug(
$"{GetType().Name} job finished, next run will happen at {GetNextRunDateTime(startedAt)}."
);
Logger.LogDebug(
"{Name} job finished, next run will happen at {GetNextRunDateTime}.", GetType().Name, GetNextRunDateTime(startedAt)
);
}
catch (Exception ex)
{
Logger.LogError(ex, ex.Message);
Comment thread
dkorecko marked this conversation as resolved.
}
Comment thread
dkorecko marked this conversation as resolved.
}

private string GetNextRunDateTime(DateTime startedAt)
Expand All @@ -90,7 +97,10 @@ protected virtual void OnStart() { }

protected virtual void OnStop() { }

protected virtual void OnRun() { }
protected virtual Task OnRun()
{
return Task.CompletedTask;
}

public void Dispose()
{
Expand Down
8 changes: 3 additions & 5 deletions Ticky.Internal/Services/Hosted/CleanupHostedService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
namespace Ticky.Internal.Services.Hosted;

public class CleanupHostedService : AbstractHostedService<CleanupHostedService>
public class CleanupHostedService(IServiceScopeFactory serviceScopeFactory)
: AbstractHostedService<CleanupHostedService>(serviceScopeFactory, TimeSpan.FromMinutes(15), TimeSpan.FromHours(1))
{
public CleanupHostedService(IServiceScopeFactory serviceScopeFactory)
: base(serviceScopeFactory, TimeSpan.FromMinutes(15), TimeSpan.FromHours(1)) { }

protected override async void OnRun()
protected override async Task OnRun()
{
await CleanCodes();
await DeleteUnusedProfilePictures();
Expand Down
14 changes: 5 additions & 9 deletions Ticky.Internal/Services/Hosted/ReminderHostedService.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
namespace Ticky.Internal.Services.Hosted;

public class ReminderHostedService : AbstractHostedService<ReminderHostedService>
public class ReminderHostedService(IServiceScopeFactory serviceScopeFactory)
: AbstractHostedService<ReminderHostedService>(serviceScopeFactory,
TimeSpan.FromSeconds(Constants.Limits.MINIMUM_SECOND_HOSTED_SERVICE_DELAY),
TimeSpan.FromMinutes(2))
{
public ReminderHostedService(IServiceScopeFactory serviceScopeFactory)
: base(
serviceScopeFactory,
TimeSpan.FromSeconds(Constants.Limits.MINIMUM_SECOND_HOSTED_SERVICE_DELAY),
TimeSpan.FromMinutes(2)
) { }

protected override async void OnRun()
protected override async Task OnRun()
{
using var scope = ServiceScopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<DataContext>()!;
Expand Down
55 changes: 22 additions & 33 deletions Ticky.Internal/Services/Hosted/RepeatHostedService.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
namespace Ticky.Internal.Services.Hosted;

public class RepeatHostedService : AbstractHostedService<RepeatHostedService>
public class RepeatHostedService(IServiceScopeFactory serviceScopeFactory) : AbstractHostedService<RepeatHostedService>(
serviceScopeFactory,
TimeSpan.FromSeconds(Constants.Limits.MINIMUM_SECOND_HOSTED_SERVICE_DELAY),
TimeSpan.FromMinutes(2))
{
public RepeatHostedService(IServiceScopeFactory serviceScopeFactory)
: base(
serviceScopeFactory,
TimeSpan.FromSeconds(Constants.Limits.MINIMUM_SECOND_HOSTED_SERVICE_DELAY),
TimeSpan.FromMinutes(2)
) { }

protected override async void OnRun()
protected override async Task OnRun()
{
using var scope = ServiceScopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<DataContext>()!;
var logger = scope.ServiceProvider.GetRequiredService<ILogger<RepeatHostedService>>()!;
Comment thread
dkorecko marked this conversation as resolved.
var cardNumberingService =
scope.ServiceProvider.GetRequiredService<CardNumberingService>()!;

var allRepeatCards = await db
var allRepeatCards = await db.Cards.Where(x => x.RepeatInfo != null).ToListAsync();

var pendingNextRepeatCardIds = allRepeatCards
.Where(x => x.CalculateNextRepeat(DateTime.Now) < DateTime.Now)
.Select(x => x.Id)
.ToList();

Comment thread
dkorecko marked this conversation as resolved.
var populatedPendingRepeatCards = await db
.Cards.Include(x => x.Column)
.ThenInclude(x => x.Board)
.Include(x => x.Subtasks)
Expand All @@ -29,14 +32,10 @@ protected override async void OnRun()
.Include(x => x.LinkedIssuesOne)
.Include(x => x.LinkedIssuesTwo)
.ThenInclude(x => x.CardOne)
.Where(x => x.RepeatInfo != null)
.Where(x => pendingNextRepeatCardIds.Contains(x.Id))
.ToListAsync();

var pendingNextRepeatCards = allRepeatCards
.Where(x => x.CalculateNextRepeat(DateTime.Now) < DateTime.Now)
.ToList();

foreach (var card in pendingNextRepeatCards)
foreach (var card in populatedPendingRepeatCards)
{
card.RepeatInfo!.LastRepeat = DateTime.Now;

Expand Down Expand Up @@ -119,14 +118,9 @@ protected override async void OnRun()
? Constants.LINK_TYPE_PAIRS[Constants.REPEATED_KEY]
: Constants.LINK_TYPE_PAIRS.First(x => x.Value.Equals(Constants.REPEATED_KEY)).Key;

foreach (var linkedIssueOne in card.LinkedIssuesOne)
foreach (var linkedIssueOne in card.LinkedIssuesOne.Where(linkedIssueOne => !linkedIssueOne.Category.Equals(Constants.REPEATED_KEY)
&& !linkedIssueOne.Category.Equals(oppositeCategory)))
{
if (
linkedIssueOne.Category.Equals(Constants.REPEATED_KEY)
|| linkedIssueOne.Category.Equals(oppositeCategory)
)
continue;

newCard.LinkedIssuesOne.Add(
new()
{
Expand All @@ -137,14 +131,9 @@ protected override async void OnRun()
);
}

foreach (var linkedIssueTwo in card.LinkedIssuesTwo)
foreach (var linkedIssueTwo in card.LinkedIssuesTwo.Where(linkedIssueTwo => !linkedIssueTwo.Category.Equals(Constants.REPEATED_KEY)
&& !linkedIssueTwo.Category.Equals(oppositeCategory)))
{
if (
linkedIssueTwo.Category.Equals(Constants.REPEATED_KEY)
|| linkedIssueTwo.Category.Equals(oppositeCategory)
)
continue;

newCard.LinkedIssuesTwo.Add(
new()
{
Expand Down Expand Up @@ -189,17 +178,17 @@ protected override async void OnRun()
targetColumn.Cards.ForEach(x => x.Index++);
else if (
card.RepeatInfo.CardPlacement.Equals(CardPlacement.Bottom)
&& targetColumn.Cards.Any()
&& targetColumn.Cards.Count != 0
)
newCard.Index = targetColumn.Cards.Max(x => x.Index) + 1;
else
throw new Exception("Unsupported card placement type");
}

if (pendingNextRepeatCards.Any())
if (populatedPendingRepeatCards.Count != 0)
{
await db.SaveChangesAsync();
logger.LogInformation($"{pendingNextRepeatCards.Count()} cards have been repeated.");
logger.LogInformation("{Count} cards have been repeated.", populatedPendingRepeatCards.Count);
}
}
}
18 changes: 7 additions & 11 deletions Ticky.Internal/Services/Hosted/SnoozeHostedService.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
namespace Ticky.Internal.Services.Hosted;

public class SnoozeHostedService : AbstractHostedService<SnoozeHostedService>
public class SnoozeHostedService(IServiceScopeFactory serviceScopeFactory) : AbstractHostedService<SnoozeHostedService>(
serviceScopeFactory,
TimeSpan.FromSeconds(Constants.Limits.MINIMUM_SECOND_HOSTED_SERVICE_DELAY),
TimeSpan.FromMinutes(2))
{
public SnoozeHostedService(IServiceScopeFactory serviceScopeFactory)
: base(
serviceScopeFactory,
TimeSpan.FromSeconds(Constants.Limits.MINIMUM_SECOND_HOSTED_SERVICE_DELAY),
TimeSpan.FromMinutes(2)
) { }

protected override async void OnRun()
protected override async Task OnRun()
{
using var scope = ServiceScopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<DataContext>()!;
Expand All @@ -24,10 +20,10 @@ protected override async void OnRun()
card.SnoozedUntil = null;
}

if (expiredSnoozes.Any())
if (expiredSnoozes.Count != 0)
{
await db.SaveChangesAsync();
logger.LogInformation($"{expiredSnoozes.Count} cards unsnoozed.");
logger.LogInformation("{ExpiredSnoozesCount} cards unsnoozed.", expiredSnoozes.Count);
}
}
}
1 change: 0 additions & 1 deletion Ticky.Web/Components/Pages/UserSettings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@inject ILogger<UserSettings> _logger
@inject AvatarService _avatarService
@inject UserManager<User> _userManager
@inject ILogger<UserSettings> _logger
@inject NavigationManager _navigationManager
@inherits NotifiableBase
@implements IDisposable
Expand Down
Loading