Skip to content

Commit

Permalink
Pass correct cache client instance to job runner
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Jul 26, 2024
1 parent b454c26 commit 53e89b1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions samples/Foundatio.HostingSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Caching;
using Foundatio.Extensions.Hosting.Jobs;
using Foundatio.Extensions.Hosting.Startup;
using Foundatio.Jobs;
Expand Down Expand Up @@ -77,6 +78,7 @@ public static IHostBuilder CreateHostBuilder(string[] args)
{
// will shutdown the host if no jobs are running
s.AddJobLifetimeService();
s.AddSingleton<ICacheClient>(_ => new InMemoryCacheClient());

// inserts a startup action that does not complete until the critical health checks are healthy
// gets inserted as 1st startup action so that any other startup actions dont run until the critical resources are available
Expand Down
5 changes: 3 additions & 2 deletions src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ public ScheduledJobManager(IServiceProvider serviceProvider, ILoggerFactory logg
_serviceProvider = serviceProvider;
_loggerFactory = loggerFactory;
var cacheClient = serviceProvider.GetService<ICacheClient>();
bool hasCacheClient = cacheClient != null;
_cacheClient = cacheClient ?? new InMemoryCacheClient(o => o.LoggerFactory(loggerFactory));
_jobs.AddRange(serviceProvider.GetServices<ScheduledJobRegistration>().Select(j => new ScheduledJobRunner(j.Options, serviceProvider, _cacheClient, loggerFactory)));
_jobsArray = _jobs.ToArray();
if (_jobs.Any(j => j.Options.IsDistributed && cacheClient == null))
if (_jobs.Any(j => j.Options.IsDistributed && !hasCacheClient))
throw new ArgumentException("A distributed cache client is required to run distributed jobs.");
_cacheClient = cacheClient ?? new InMemoryCacheClient(o => o.LoggerFactory(loggerFactory));
}

public void AddOrUpdate<TJob>(string cronSchedule, Action<ScheduledJobOptionsBuilder> configure = null) where TJob : class, IJob
Expand Down

0 comments on commit 53e89b1

Please sign in to comment.