Skip to content

Commit 62b61ea

Browse files
fix: default cron was invalid + changed some logging
1 parent b34c8f1 commit 62b61ea

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

AniWorldReminder_API/Classes/UpsertEpisodeInfoJob.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Hangfire;
22
using MethodTimer;
3+
using Mysqlx.Prepare;
4+
using System.Reflection;
35
using System.Text;
46

57
namespace AniWorldReminder_API.Classes
@@ -14,12 +16,19 @@ public class UpsertEpisodeInfoJob(ILogger<UpsertEpisodeInfoJob> logger, IStreami
1416
[Time]
1517
public async Task ExecuteAsync()
1618
{
19+
MethodBase? methodBase = typeof(UpsertEpisodeInfoJob).GetMethod(nameof(ExecuteAsync));
20+
21+
if (methodBase is not null)
22+
{
23+
MethodTimeLogger.LogExecution(methodBase);
24+
}
25+
1726
TelegramBotSettingsModel? botSettings = SettingsHelper.ReadSettings<TelegramBotSettingsModel>();
1827
AppSettingsModel? appSettings = SettingsHelper.ReadSettings<AppSettingsModel>();
1928

2029
try
2130
{
22-
await CheckForNewEpisodesAsync(botSettings, appSettings);
31+
await CheckForNewEpisodesAsync(botSettings, appSettings);
2332
}
2433
catch (Exception ex)
2534
{

AniWorldReminder_API/Misc/MethodTimeLogger.cs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,54 @@ public static void Log(MethodBase methodBase, TimeSpan elapsed, string message)
1616
ILogger logger = (loggerFactory ?? LoggerFactory.Create(_ => { }))
1717
.CreateLogger(methodBase.DeclaringType?.FullName ?? "MethodTimer");
1818

19-
logger.LogInformation(
20-
"Method {MethodName} took {ElapsedMs} ms. {Message}",
21-
$"{methodBase.DeclaringType?.Name}.{methodBase.Name}",
22-
elapsed.TotalMilliseconds,
23-
message);
19+
string additionalInfo = $" Additional Info: {message}";
20+
string duration = $" Runtime: {elapsed:mm}m {elapsed:ss}s.";
21+
22+
Type? type = methodBase.DeclaringType;
23+
24+
if (type == null)
25+
return;
26+
27+
Type? @interface = type.GetInterfaces()
28+
.FirstOrDefault(i => type.GetInterfaceMap(i).TargetMethods.Any(m => m.DeclaringType == type));
29+
30+
string info = "Executed ";
31+
32+
if (@interface is not null && @interface.FullName == "Quartz.IJob" && methodBase.Name == "Execute")
33+
{
34+
info += $"CronJob: ";
35+
}
36+
37+
logger.LogInformation($"{DateTime.Now} | " + info + "{Class}.{Method}.{Duration}{Message}",
38+
methodBase.DeclaringType!.Name,
39+
methodBase.Name,
40+
(elapsed.Seconds > 0 ? duration : ""),
41+
(string.IsNullOrEmpty(message) ? "" : additionalInfo));
42+
}
43+
44+
public static void LogExecution(MethodBase methodBase)
45+
{
46+
Type? type = methodBase.DeclaringType;
47+
48+
if (type == null)
49+
return;
50+
51+
Type? @interface = type.GetInterfaces()
52+
.FirstOrDefault(i => type.GetInterfaceMap(i).TargetMethods.Any(m => m.DeclaringType == type));
53+
54+
string info = "Started ";
55+
56+
if (@interface is not null && @interface.FullName == "Quartz.IJob" && methodBase.Name == "Execute")
57+
{
58+
info += $"CronJob: ";
59+
}
60+
61+
ILogger logger = (loggerFactory ?? LoggerFactory.Create(_ => { }))
62+
.CreateLogger(methodBase.DeclaringType?.FullName ?? "MethodTimer");
63+
64+
logger.LogInformation($"{DateTime.Now} | " + info + "{Class}.{Method}.",
65+
methodBase.DeclaringType!.Name,
66+
methodBase.Name);
2467
}
2568
}
2669
}

AniWorldReminder_API/Models/AppSettingsModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class AppSettingsModel
1111
public bool EnableEpisodeReminderJob { get; set; } = true;
1212

1313
[JsonPropertyName("EpisodeReminderCron")]
14-
public string EpisodeReminderCron { get; set; } = "*/15 * * * *";
14+
public string EpisodeReminderCron { get; set; } = "0 * * * *";
1515

1616
[JsonPropertyName("EnableHangfireDashboard")]
1717
public bool EnableHangfireDashboard { get; set; }

AniWorldReminder_API/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public static async Task Main(string[] args)
178178
recurringJobManager.AddOrUpdate<UpsertEpisodeInfoJob>(
179179
"episode-reminder-scan",
180180
job => job.ExecuteAsync(),
181-
appSettings?.EpisodeReminderCron ?? "*/60 * * * *",
181+
appSettings?.EpisodeReminderCron,
182182
new RecurringJobOptions
183183
{
184184
TimeZone = TimeZoneInfo.Local

0 commit comments

Comments
 (0)