-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cronjob): use attribute to get expressions and add job
- Loading branch information
1 parent
1a822ae
commit 7f3cc2e
Showing
4 changed files
with
93 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Reflection; | ||
using Cronos; | ||
|
||
namespace GZCTF.Services.CronJob; | ||
|
||
[AttributeUsage(AttributeTargets.Method)] | ||
public class CronJobAttribute(string expression) : Attribute | ||
{ | ||
public CronExpression Expression { get; } = CronExpression.Parse(expression); | ||
} | ||
|
||
public class CronJobNotFoundException(string message) : Exception(message); | ||
|
||
public static class CronJobExtensions | ||
{ | ||
public static (string, CronJobEntry) ToEntry(this CronJob job) | ||
{ | ||
var method = job.Method; | ||
var attr = method.GetCustomAttribute<CronJobAttribute>() ?? throw new CronJobNotFoundException(method.Name); | ||
return (method.Name, new CronJobEntry(job, attr.Expression)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Threading.Channels; | ||
using GZCTF.Repositories; | ||
using GZCTF.Repositories.Interface; | ||
using GZCTF.Services.Cache; | ||
using Microsoft.Extensions.Caching.Distributed; | ||
// ReSharper disable UnusedMember.Global | ||
|
||
namespace GZCTF.Services.CronJob; | ||
|
||
public static class DefaultCronJobs | ||
{ | ||
[CronJob("*/3 * * * *")] | ||
public static async Task ContainerChecker(AsyncServiceScope scope, ILogger<CronJobService> logger) | ||
{ | ||
logger.SystemLog($"Executing {nameof(ContainerChecker)}", TaskStatus.Pending, LogLevel.Debug); | ||
|
||
var containerRepo = scope.ServiceProvider.GetRequiredService<IContainerRepository>(); | ||
|
||
foreach (Models.Data.Container container in await containerRepo.GetDyingContainers()) | ||
{ | ||
await containerRepo.DestroyContainer(container); | ||
logger.SystemLog( | ||
Program.StaticLocalizer[nameof(Resources.Program.CronJob_RemoveExpiredContainer), | ||
container.ContainerId], | ||
TaskStatus.Success, LogLevel.Debug); | ||
} | ||
} | ||
|
||
[CronJob("*/10 * * * *")] | ||
public static async Task BootstrapCache(AsyncServiceScope scope, ILogger<CronJobService> logger) | ||
{ | ||
logger.SystemLog($"Executing {nameof(BootstrapCache)}", TaskStatus.Pending, LogLevel.Debug); | ||
|
||
var gameRepo = scope.ServiceProvider.GetRequiredService<IGameRepository>(); | ||
var upcoming = await gameRepo.GetUpcomingGames(); | ||
|
||
if (upcoming.Length <= 0) | ||
return; | ||
|
||
var channelWriter = scope.ServiceProvider.GetRequiredService<ChannelWriter<CacheRequest>>(); | ||
var cache = scope.ServiceProvider.GetRequiredService<IDistributedCache>(); | ||
|
||
foreach (var game in upcoming) | ||
{ | ||
var key = CacheKey.ScoreBoard(game); | ||
var value = await cache.GetAsync(key); | ||
if (value is not null) | ||
continue; | ||
|
||
await channelWriter.WriteAsync(ScoreboardCacheHandler.MakeCacheRequest(game)); | ||
logger.SystemLog(Program.StaticLocalizer[nameof(Resources.Program.CronJob_BootstrapRankingCache), key], | ||
TaskStatus.Success, | ||
LogLevel.Debug); | ||
} | ||
} | ||
} |