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

Minor logging additions #176

Merged
merged 3 commits into from
Jan 23, 2025
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
17 changes: 15 additions & 2 deletions src/NCronJob/Execution/StartupJobManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using Microsoft.Extensions.Logging;
using System.Globalization;
using System.Text;

namespace NCronJob;

internal class StartupJobManager(
internal partial class StartupJobManager(
JobRegistry jobRegistry,
JobProcessor jobProcessor,
JobExecutionProgressObserver observer)
JobExecutionProgressObserver observer,
TimeProvider timeProvider,
ILogger<StartupJobManager> logger)
{
public async Task ProcessStartupJobs(CancellationToken stopToken)
{
Expand All @@ -17,6 +20,8 @@ public async Task ProcessStartupJobs(CancellationToken stopToken)
return;
}

LogStartupJobsStart(logger, timeProvider.GetUtcNow());

List<JobRun> jobRuns = [];
var startupTasks = startupJobs.Select(definition =>
{
Expand All @@ -27,6 +32,8 @@ public async Task ProcessStartupJobs(CancellationToken stopToken)

await Task.WhenAll(startupTasks).ConfigureAwait(false);

LogStartupJobsCompletion(logger, timeProvider.GetUtcNow());

Exception[] faults = jobRuns
.Where(jr => jr.JobDefinition.ShouldCrashOnStartupFailure == true && jr.CurrentState.Type == JobStateType.Faulted)
.Select(jr => jr.CurrentState.Fault)
Expand All @@ -50,4 +57,10 @@ public async Task ProcessStartupJobs(CancellationToken stopToken)

private async Task CreateExecutionTask(JobRun job, CancellationToken stopToken) =>
await jobProcessor.ProcessJobAsync(job, stopToken).ConfigureAwait(false);

[LoggerMessage(LogLevel.Information, "Triggering startup jobs execution at {at:o}")]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As they're only going to be logged once, I figured using a Information log level would work. Thoughts?

private static partial void LogStartupJobsStart(ILogger logger, DateTimeOffset at);

[LoggerMessage(LogLevel.Information, "Completed startup jobs execution at {at:o}")]
private static partial void LogStartupJobsCompletion(ILogger logger, DateTimeOffset at);
}
2 changes: 1 addition & 1 deletion src/NCronJob/Scheduler/JobWorker.LogMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace NCronJob;

internal sealed partial class JobWorker
{
[LoggerMessage(LogLevel.Trace, "Next run of job '{JobType}' is at {NextRun}")]
[LoggerMessage(LogLevel.Trace, "Next run of job '{JobType}' is at {NextRun:o}")]
private partial void LogNextJobRun(Type jobType, DateTimeOffset nextRun);

[LoggerMessage(LogLevel.Debug, "Running job '{JobType}'.")]
Expand Down
4 changes: 2 additions & 2 deletions src/NCronJob/Scheduler/QueueWorker.LogMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ internal sealed partial class QueueWorker
private partial void LogCancellationRequestedInJob();

[LoggerMessage(LogLevel.Information, "Job added to queue: {JobType} at {RunAt:o}")]
private partial void LogJobAddedToQueue(string jobType, DateTime? runAt);
private partial void LogJobAddedToQueue(string jobType, DateTimeOffset? runAt);

[LoggerMessage(LogLevel.Information, "Job removed from queue: {JobType} at {RunAt:o}")]
private partial void LogJobRemovedFromQueue(string jobType, DateTime? runAt);
private partial void LogJobRemovedFromQueue(string jobType, DateTimeOffset? runAt);
}
4 changes: 2 additions & 2 deletions src/NCronJob/Scheduler/QueueWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ private void HandleUpdate(object? sender, NotifyCollectionChangedEventArgs e)
case NotifyCollectionChangedAction.Add:
foreach (JobRun job in e.NewItems!)
{
LogJobAddedToQueue(job.JobDefinition.Type.Name, job.RunAt?.LocalDateTime);
LogJobAddedToQueue(job.JobDefinition.Type.Name, job.RunAt);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good catch

}
break;
case NotifyCollectionChangedAction.Remove:
foreach (JobRun job in e.OldItems!)
{
LogJobRemovedFromQueue(job.JobDefinition.Type.Name, job.RunAt?.LocalDateTime);
LogJobRemovedFromQueue(job.JobDefinition.Type.Name, job.RunAt);
}
break;
case NotifyCollectionChangedAction.Replace:
Expand Down
Loading