diff --git a/samples/Foundatio.HostingSample/Program.cs b/samples/Foundatio.HostingSample/Program.cs index 725fddb2..258d0b7d 100644 --- a/samples/Foundatio.HostingSample/Program.cs +++ b/samples/Foundatio.HostingSample/Program.cs @@ -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; @@ -77,6 +78,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) { // will shutdown the host if no jobs are running s.AddJobLifetimeService(); + s.AddSingleton(_ => 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 diff --git a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs b/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs index 0cb90f7b..589c1697 100644 --- a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs +++ b/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobManager.cs @@ -41,11 +41,12 @@ public ScheduledJobManager(IServiceProvider serviceProvider, ILoggerFactory logg _serviceProvider = serviceProvider; _loggerFactory = loggerFactory; var cacheClient = serviceProvider.GetService(); + bool hasCacheClient = cacheClient != null; + _cacheClient = cacheClient ?? new InMemoryCacheClient(o => o.LoggerFactory(loggerFactory)); _jobs.AddRange(serviceProvider.GetServices().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(string cronSchedule, Action configure = null) where TJob : class, IJob