Skip to content

Commit

Permalink
Move job scheduler checks into it's own activity
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Oct 28, 2024
1 parent f3c520a commit fef5425
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
3 changes: 0 additions & 3 deletions src/Foundatio.Extensions.Hosting/Jobs/HostedJobService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Extensions.Hosting.Startup;
Expand Down Expand Up @@ -52,8 +51,6 @@ private async Task ExecuteAsync(CancellationToken stoppingToken)

try
{
using var activity = FoundatioDiagnostics.ActivitySource.StartActivity("Job: " + _jobOptions.Name);

await runner.RunAsync(stoppingToken).AnyContext();
#if NET8_0_OR_GREATER
await _stoppingCts.CancelAsync().AnyContext();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Caching;
Expand Down
32 changes: 18 additions & 14 deletions src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Extensions.Hosting.Startup;
using Foundatio.Messaging;
using Foundatio.Utility;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -14,11 +13,13 @@ public class ScheduledJobService : BackgroundService
{
private readonly IServiceProvider _serviceProvider;
private readonly JobManager _jobManager;
private readonly TimeProvider _timeProvider;

public ScheduledJobService(IServiceProvider serviceProvider, JobManager jobManager)
{
_serviceProvider = serviceProvider;
_jobManager = jobManager;
_timeProvider = _timeProvider = serviceProvider.GetService<TimeProvider>() ?? TimeProvider.System;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
Expand All @@ -33,25 +34,28 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
}

// delay until right after next minute starts to sync with cron schedules
await Task.Delay(TimeSpan.FromSeconds(60 - _timeProvider.GetUtcNow().UtcDateTime.Second));

while (!stoppingToken.IsCancellationRequested)
{
foreach (var jobToRun in _jobManager.Jobs)
var jobsToRun = new List<ScheduledJobRunner>();
using (var activity = FoundatioDiagnostics.ActivitySource.StartActivity("Job Scheduler"))
{
foreach (var job in _jobManager.Jobs)
if (await job.ShouldRunAsync())
jobsToRun.Add(job);
}

foreach (var jobToRun in jobsToRun)
{
using var activity = FoundatioDiagnostics.ActivitySource.StartActivity("Job: " + jobToRun.Options.Name);

if (await jobToRun.ShouldRunAsync())
{
await jobToRun.StartAsync(stoppingToken).AnyContext();
}
else
{
// don't record trace if we didn't run the job and we started the root activity
if (activity is { Parent: null })
activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;
}
await jobToRun.StartAsync(stoppingToken).AnyContext();
}

await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken).AnyContext();
// shortest cron schedule is 1 minute so only check every minute
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken).AnyContext();
}
}
}
2 changes: 2 additions & 0 deletions src/Foundatio/Jobs/IJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public static async Task<int> RunContinuousAsync(this IJob job, TimeSpan? interv

while (!cancellationToken.IsCancellationRequested)
{
using var activity = FoundatioDiagnostics.ActivitySource.StartActivity("Job: " + jobName);

var result = await job.TryRunAsync(cancellationToken).AnyContext();
logger.LogJobResult(result, jobName);

Expand Down
2 changes: 2 additions & 0 deletions src/Foundatio/Jobs/JobRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ await jobInstance.RunContinuousAsync(_options.Interval, _options.IterationLimit,
}
else
{
using var activity = FoundatioDiagnostics.ActivitySource.StartActivity("Job: " + _jobName);

var result = await job.TryRunAsync(cancellationToken).AnyContext();
_logger.LogJobResult(result, _jobName);

Expand Down

0 comments on commit fef5425

Please sign in to comment.