diff --git a/src/Umbraco.Infrastructure/Scheduling/BackgroundTaskRunner.cs b/src/Umbraco.Infrastructure/Scheduling/BackgroundTaskRunner.cs index 2cda289591..3a68d83fff 100644 --- a/src/Umbraco.Infrastructure/Scheduling/BackgroundTaskRunner.cs +++ b/src/Umbraco.Infrastructure/Scheduling/BackgroundTaskRunner.cs @@ -81,7 +81,7 @@ namespace Umbraco.Web.Scheduling private readonly string _logPrefix; private readonly BackgroundTaskRunnerOptions _options; private readonly ILogger _logger; - private readonly IApplicationShutdownRegistry _hostingEnvironment; + private readonly IApplicationShutdownRegistry _applicationShutdownRegistry; private readonly object _locker = new object(); private readonly BufferBlock _tasks = new BufferBlock(new DataflowBlockOptions()); @@ -103,10 +103,10 @@ namespace Umbraco.Web.Scheduling /// Initializes a new instance of the class. /// /// A logger. - /// The hosting environment + /// The hosting environment /// An optional main domain hook. - public BackgroundTaskRunner(ILogger logger, IApplicationShutdownRegistry hostingEnvironment, MainDomHook hook = null) - : this(typeof(T).FullName, new BackgroundTaskRunnerOptions(), logger, hostingEnvironment, hook) + public BackgroundTaskRunner(ILogger logger, IApplicationShutdownRegistry applicationShutdownRegistry, MainDomHook hook = null) + : this(typeof(T).FullName, new BackgroundTaskRunnerOptions(), logger, applicationShutdownRegistry, hook) { } /// @@ -114,10 +114,10 @@ namespace Umbraco.Web.Scheduling /// /// The name of the runner. /// A logger. - /// The hosting environment + /// The hosting environment /// An optional main domain hook. - public BackgroundTaskRunner(string name, ILogger logger, IApplicationShutdownRegistry hostingEnvironment, MainDomHook hook = null) - : this(name, new BackgroundTaskRunnerOptions(), logger, hostingEnvironment, hook) + public BackgroundTaskRunner(string name, ILogger logger, IApplicationShutdownRegistry applicationShutdownRegistry, MainDomHook hook = null) + : this(name, new BackgroundTaskRunnerOptions(), logger, applicationShutdownRegistry, hook) { } /// @@ -125,10 +125,10 @@ namespace Umbraco.Web.Scheduling /// /// The set of options. /// A logger. - /// The hosting environment + /// The hosting environment /// An optional main domain hook. - public BackgroundTaskRunner(BackgroundTaskRunnerOptions options, ILogger logger, IApplicationShutdownRegistry hostingEnvironment, MainDomHook hook = null) - : this(typeof(T).FullName, options, logger, hostingEnvironment, hook) + public BackgroundTaskRunner(BackgroundTaskRunnerOptions options, ILogger logger, IApplicationShutdownRegistry applicationShutdownRegistry, MainDomHook hook = null) + : this(typeof(T).FullName, options, logger, applicationShutdownRegistry, hook) { } /// @@ -137,17 +137,17 @@ namespace Umbraco.Web.Scheduling /// The name of the runner. /// The set of options. /// A logger. - /// The hosting environment + /// The hosting environment /// An optional main domain hook. - public BackgroundTaskRunner(string name, BackgroundTaskRunnerOptions options, ILogger logger, IApplicationShutdownRegistry hostingEnvironment, MainDomHook hook = null) + public BackgroundTaskRunner(string name, BackgroundTaskRunnerOptions options, ILogger logger, IApplicationShutdownRegistry applicationShutdownRegistry, MainDomHook hook = null) { _options = options ?? throw new ArgumentNullException(nameof(options)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - _hostingEnvironment = hostingEnvironment; + _applicationShutdownRegistry = applicationShutdownRegistry; _logPrefix = "[" + name + "] "; if (options.Hosted) - _hostingEnvironment.RegisterObject(this); + _applicationShutdownRegistry.RegisterObject(this); if (hook != null) _completed = _terminated = hook.Register() == false; @@ -736,7 +736,7 @@ namespace Umbraco.Web.Scheduling /// /// "When the application manager needs to stop a registered object, it will call the Stop method." /// The application manager will call the Stop method to ask a registered object to un-register. During - /// processing of the Stop method, the registered object must call the HostingEnvironment.UnregisterObject method. + /// processing of the Stop method, the registered object must call the applicationShutdownRegistry.UnregisterObject method. /// public void Stop(bool immediate) => StopInternal(immediate); @@ -811,7 +811,7 @@ namespace Umbraco.Web.Scheduling if (immediate) { //only unregister when it's the final call, else we won't be notified of the final call - _hostingEnvironment.UnregisterObject(this); + _applicationShutdownRegistry.UnregisterObject(this); } if (_terminated) return; // already taken care of diff --git a/src/Umbraco.Infrastructure/Scheduling/SchedulerComponent.cs b/src/Umbraco.Infrastructure/Scheduling/SchedulerComponent.cs index 094258b881..141a1b062e 100644 --- a/src/Umbraco.Infrastructure/Scheduling/SchedulerComponent.cs +++ b/src/Umbraco.Infrastructure/Scheduling/SchedulerComponent.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Threading; using Umbraco.Core; using Umbraco.Core.Composing; @@ -30,17 +31,17 @@ namespace Umbraco.Web.Scheduling private readonly IContentService _contentService; private readonly IAuditService _auditService; private readonly IProfilingLogger _logger; - private readonly IApplicationShutdownRegistry _hostingEnvironment; + private readonly IApplicationShutdownRegistry _applicationShutdownRegistry; private readonly IScopeProvider _scopeProvider; private readonly HealthCheckCollection _healthChecks; private readonly HealthCheckNotificationMethodCollection _notifications; private readonly IUmbracoContextFactory _umbracoContextFactory; private readonly IHealthChecksSettings _healthChecksSettingsConfig; - private readonly IIOHelper _ioHelper; private readonly IServerMessenger _serverMessenger; private readonly IRequestAccessor _requestAccessor; private readonly ILoggingSettings _loggingSettings; private readonly IKeepAliveSettings _keepAliveSettings; + private readonly IHostingEnvironment _hostingEnvironment; private BackgroundTaskRunner _keepAliveRunner; private BackgroundTaskRunner _publishingRunner; @@ -56,9 +57,10 @@ namespace Umbraco.Web.Scheduling IContentService contentService, IAuditService auditService, HealthCheckCollection healthChecks, HealthCheckNotificationMethodCollection notifications, IScopeProvider scopeProvider, IUmbracoContextFactory umbracoContextFactory, IProfilingLogger logger, - IApplicationShutdownRegistry hostingEnvironment, IHealthChecksSettings healthChecksSettingsConfig, - IIOHelper ioHelper, IServerMessenger serverMessenger, IRequestAccessor requestAccessor, - ILoggingSettings loggingSettings, IKeepAliveSettings keepAliveSettings) + IApplicationShutdownRegistry applicationShutdownRegistry, IHealthChecksSettings healthChecksSettingsConfig, + IServerMessenger serverMessenger, IRequestAccessor requestAccessor, + ILoggingSettings loggingSettings, IKeepAliveSettings keepAliveSettings, + IHostingEnvironment hostingEnvironment) { _runtime = runtime; _mainDom = mainDom; @@ -67,27 +69,27 @@ namespace Umbraco.Web.Scheduling _auditService = auditService; _scopeProvider = scopeProvider; _logger = logger; - _hostingEnvironment = hostingEnvironment; + _applicationShutdownRegistry = applicationShutdownRegistry; _umbracoContextFactory = umbracoContextFactory; _healthChecks = healthChecks; _notifications = notifications; _healthChecksSettingsConfig = healthChecksSettingsConfig ?? throw new ArgumentNullException(nameof(healthChecksSettingsConfig)); - _ioHelper = ioHelper; _serverMessenger = serverMessenger; _requestAccessor = requestAccessor; _loggingSettings = loggingSettings; _keepAliveSettings = keepAliveSettings; + _hostingEnvironment = hostingEnvironment; } public void Initialize() { // backgrounds runners are web aware, if the app domain dies, these tasks will wind down correctly - _keepAliveRunner = new BackgroundTaskRunner("KeepAlive", _logger, _hostingEnvironment); - _publishingRunner = new BackgroundTaskRunner("ScheduledPublishing", _logger, _hostingEnvironment); - _scrubberRunner = new BackgroundTaskRunner("LogScrubber", _logger, _hostingEnvironment); - _fileCleanupRunner = new BackgroundTaskRunner("TempFileCleanup", _logger, _hostingEnvironment); - _healthCheckRunner = new BackgroundTaskRunner("HealthCheckNotifier", _logger, _hostingEnvironment); + _keepAliveRunner = new BackgroundTaskRunner("KeepAlive", _logger, _applicationShutdownRegistry); + _publishingRunner = new BackgroundTaskRunner("ScheduledPublishing", _logger, _applicationShutdownRegistry); + _scrubberRunner = new BackgroundTaskRunner("LogScrubber", _logger, _applicationShutdownRegistry); + _fileCleanupRunner = new BackgroundTaskRunner("TempFileCleanup", _logger, _applicationShutdownRegistry); + _healthCheckRunner = new BackgroundTaskRunner("HealthCheckNotifier", _logger, _applicationShutdownRegistry); // we will start the whole process when a successful request is made _requestAccessor.RouteAttempt += RegisterBackgroundTasksOnce; @@ -190,10 +192,22 @@ namespace Umbraco.Web.Scheduling private IBackgroundTask RegisterTempFileCleanup() { + + var tempFolderPaths = new[] + { + _hostingEnvironment.MapPathContentRoot(Constants.SystemDirectories.TempFileUploads) + }; + + foreach (var tempFolderPath in tempFolderPaths) + { + //ensure it exists + Directory.CreateDirectory(tempFolderPath); + } + // temp file cleanup, will run on all servers - even though file upload should only be handled on the master, this will // ensure that in the case it happes on replicas that they are cleaned up. var task = new TempFileCleanup(_fileCleanupRunner, DefaultDelayMilliseconds, OneHourMilliseconds, - new[] { new DirectoryInfo(_ioHelper.MapPath(Constants.SystemDirectories.TempFileUploads)) }, + tempFolderPaths.Select(x=>new DirectoryInfo(x)), TimeSpan.FromDays(1), //files that are over a day old _mainDom, _logger); _scrubberRunner.TryAdd(task);