2018-03-14 20:41:36 +00:00
|
|
|
using System;
|
2013-05-09 06:38:20 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2014-05-30 17:34:38 +00:00
|
|
|
using NzbDrone.Core.Backup;
|
2013-08-06 05:22:58 +00:00
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
using NzbDrone.Core.Configuration.Events;
|
2013-10-22 07:31:36 +00:00
|
|
|
using NzbDrone.Core.Download;
|
2014-02-26 05:40:47 +00:00
|
|
|
using NzbDrone.Core.HealthCheck;
|
2013-09-18 05:28:05 +00:00
|
|
|
using NzbDrone.Core.Housekeeping;
|
2013-05-09 06:38:20 +00:00
|
|
|
using NzbDrone.Core.Indexers;
|
|
|
|
using NzbDrone.Core.Lifecycle;
|
2013-05-13 02:52:55 +00:00
|
|
|
using NzbDrone.Core.MediaFiles.Commands;
|
2015-01-16 00:30:09 +00:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
2013-09-11 06:33:47 +00:00
|
|
|
using NzbDrone.Core.Messaging.Events;
|
2017-01-23 13:04:01 +00:00
|
|
|
using NzbDrone.Core.NetImport;
|
2018-03-14 20:41:36 +00:00
|
|
|
using NzbDrone.Core.Movies.Commands;
|
2013-05-20 00:30:02 +00:00
|
|
|
using NzbDrone.Core.Update.Commands;
|
2017-03-10 23:17:09 +00:00
|
|
|
using NzbDrone.Core.MetadataSource.PreDB;
|
2013-05-09 06:38:20 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Jobs
|
|
|
|
{
|
|
|
|
public interface ITaskManager
|
|
|
|
{
|
|
|
|
IList<ScheduledTask> GetPending();
|
2014-09-12 06:06:11 +00:00
|
|
|
List<ScheduledTask> GetAll();
|
2015-02-22 21:09:06 +00:00
|
|
|
DateTime GetNextExecution(Type type);
|
2013-05-09 06:38:20 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 04:52:02 +00:00
|
|
|
public class TaskManager : ITaskManager, IHandle<ApplicationStartedEvent>, IHandle<CommandExecutedEvent>, IHandleAsync<ConfigSavedEvent>
|
2013-05-09 06:38:20 +00:00
|
|
|
{
|
|
|
|
private readonly IScheduledTaskRepository _scheduledTaskRepository;
|
2013-08-06 05:22:58 +00:00
|
|
|
private readonly IConfigService _configService;
|
2017-01-22 17:24:47 +00:00
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
2013-05-09 06:38:20 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2017-01-22 17:24:47 +00:00
|
|
|
public TaskManager(IScheduledTaskRepository scheduledTaskRepository, IConfigService configService, IConfigFileProvider configFileProvider, Logger logger)
|
2013-05-09 06:38:20 +00:00
|
|
|
{
|
|
|
|
_scheduledTaskRepository = scheduledTaskRepository;
|
2013-08-06 05:22:58 +00:00
|
|
|
_configService = configService;
|
2017-01-22 17:24:47 +00:00
|
|
|
_configFileProvider = configFileProvider;
|
2013-05-09 06:38:20 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IList<ScheduledTask> GetPending()
|
|
|
|
{
|
2014-03-14 09:41:56 +00:00
|
|
|
return _scheduledTaskRepository.All()
|
|
|
|
.Where(c => c.Interval > 0 && c.LastExecution.AddMinutes(c.Interval) < DateTime.UtcNow)
|
|
|
|
.ToList();
|
2013-05-09 06:38:20 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 06:06:11 +00:00
|
|
|
public List<ScheduledTask> GetAll()
|
|
|
|
{
|
|
|
|
return _scheduledTaskRepository.All().ToList();
|
|
|
|
}
|
|
|
|
|
2015-02-22 21:09:06 +00:00
|
|
|
public DateTime GetNextExecution(Type type)
|
|
|
|
{
|
|
|
|
var scheduledTask = _scheduledTaskRepository.All().Single(v => v.TypeName == type.FullName);
|
|
|
|
return scheduledTask.LastExecution.AddMinutes(scheduledTask.Interval);
|
|
|
|
}
|
|
|
|
|
2013-05-09 06:38:20 +00:00
|
|
|
public void Handle(ApplicationStartedEvent message)
|
|
|
|
{
|
2018-11-23 07:03:32 +00:00
|
|
|
int updateInterval = 6 * 60;
|
2017-01-22 17:24:47 +00:00
|
|
|
|
|
|
|
if (_configFileProvider.Branch == "nightly")
|
|
|
|
{
|
|
|
|
updateInterval = 30;
|
|
|
|
}
|
|
|
|
|
2013-05-09 06:38:20 +00:00
|
|
|
var defaultTasks = new[]
|
|
|
|
{
|
2017-03-10 23:17:09 +00:00
|
|
|
new ScheduledTask{ Interval = 1*60, TypeName = typeof(PreDBSyncCommand).FullName},
|
2015-01-16 00:30:09 +00:00
|
|
|
new ScheduledTask{ Interval = 5, TypeName = typeof(MessagingCleanupCommand).FullName},
|
2017-01-22 17:24:47 +00:00
|
|
|
new ScheduledTask{ Interval = updateInterval, TypeName = typeof(ApplicationUpdateCommand).FullName},
|
2017-01-18 19:02:04 +00:00
|
|
|
// new ScheduledTask{ Interval = 3*60, TypeName = typeof(UpdateSceneMappingCommand).FullName},
|
2014-04-10 00:15:13 +00:00
|
|
|
new ScheduledTask{ Interval = 6*60, TypeName = typeof(CheckHealthCommand).FullName},
|
2017-01-17 11:55:32 +00:00
|
|
|
new ScheduledTask{ Interval = 24*60, TypeName = typeof(RefreshMovieCommand).FullName},
|
2013-10-22 07:31:36 +00:00
|
|
|
new ScheduledTask{ Interval = 24*60, TypeName = typeof(HousekeepingCommand).FullName},
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
new ScheduledTask
|
|
|
|
{
|
|
|
|
Interval = GetBackupInterval(),
|
|
|
|
TypeName = typeof(BackupCommand).FullName
|
|
|
|
},
|
2014-03-14 09:41:56 +00:00
|
|
|
|
|
|
|
new ScheduledTask
|
|
|
|
{
|
2015-02-07 01:42:30 +00:00
|
|
|
Interval = GetRssSyncInterval(),
|
2014-03-14 09:41:56 +00:00
|
|
|
TypeName = typeof(RssSyncCommand).FullName
|
|
|
|
},
|
|
|
|
|
2017-01-28 19:59:21 +00:00
|
|
|
new ScheduledTask
|
|
|
|
{
|
|
|
|
Interval = GetNetImportSyncInterval(),
|
|
|
|
TypeName = typeof(NetImportSyncCommand).FullName
|
|
|
|
},
|
|
|
|
|
2019-05-02 10:51:21 +00:00
|
|
|
new ScheduledTask
|
|
|
|
{
|
|
|
|
Interval = Math.Max(_configService.CheckForFinishedDownloadInterval, 1),
|
|
|
|
TypeName = typeof(CheckForFinishedDownloadCommand).FullName
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
2013-05-09 06:38:20 +00:00
|
|
|
};
|
|
|
|
|
2014-02-05 05:52:53 +00:00
|
|
|
var currentTasks = _scheduledTaskRepository.All().ToList();
|
2013-05-09 06:38:20 +00:00
|
|
|
|
2014-08-02 01:08:02 +00:00
|
|
|
_logger.Trace("Initializing jobs. Available: {0} Existing: {1}", defaultTasks.Count(), currentTasks.Count());
|
2013-05-09 06:38:20 +00:00
|
|
|
|
|
|
|
foreach (var job in currentTasks)
|
|
|
|
{
|
2013-05-10 23:53:50 +00:00
|
|
|
if (!defaultTasks.Any(c => c.TypeName == job.TypeName))
|
2013-05-09 06:38:20 +00:00
|
|
|
{
|
2014-08-02 01:08:02 +00:00
|
|
|
_logger.Trace("Removing job from database '{0}'", job.TypeName);
|
2013-05-09 06:38:20 +00:00
|
|
|
_scheduledTaskRepository.Delete(job.Id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var defaultTask in defaultTasks)
|
|
|
|
{
|
2013-05-21 04:46:30 +00:00
|
|
|
var currentDefinition = currentTasks.SingleOrDefault(c => c.TypeName == defaultTask.TypeName) ?? defaultTask;
|
2013-05-09 06:38:20 +00:00
|
|
|
|
2013-05-21 04:46:30 +00:00
|
|
|
currentDefinition.Interval = defaultTask.Interval;
|
2013-05-09 06:38:20 +00:00
|
|
|
|
2014-02-05 05:52:53 +00:00
|
|
|
if (currentDefinition.Id == 0)
|
|
|
|
{
|
|
|
|
currentDefinition.LastExecution = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
|
2013-05-21 04:46:30 +00:00
|
|
|
_scheduledTaskRepository.Upsert(currentDefinition);
|
2013-05-09 06:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-11 23:38:41 +00:00
|
|
|
|
2018-11-23 07:03:32 +00:00
|
|
|
private int GetBackupInterval()
|
|
|
|
{
|
|
|
|
var interval = _configService.BackupInterval;
|
|
|
|
return interval * 60 * 24;
|
|
|
|
}
|
|
|
|
|
2015-02-07 01:42:30 +00:00
|
|
|
private int GetRssSyncInterval()
|
|
|
|
{
|
|
|
|
var interval = _configService.RssSyncInterval;
|
|
|
|
|
2017-05-13 19:22:55 +00:00
|
|
|
if (interval > 0 && interval < 10)
|
2015-02-07 01:42:30 +00:00
|
|
|
{
|
2017-05-13 19:22:55 +00:00
|
|
|
return 10;
|
2015-02-07 01:42:30 +00:00
|
|
|
}
|
|
|
|
|
2015-12-20 02:02:38 +00:00
|
|
|
if (interval < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-07 01:42:30 +00:00
|
|
|
return interval;
|
|
|
|
}
|
|
|
|
|
2017-01-28 19:59:21 +00:00
|
|
|
private int GetNetImportSyncInterval()
|
|
|
|
{
|
|
|
|
var interval = _configService.NetImportSyncInterval;
|
|
|
|
|
|
|
|
if (interval > 0 && interval < 10)
|
|
|
|
{
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interval < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return interval;
|
|
|
|
}
|
|
|
|
|
2013-09-17 04:52:02 +00:00
|
|
|
public void Handle(CommandExecutedEvent message)
|
2013-05-11 23:38:41 +00:00
|
|
|
{
|
2015-01-16 00:30:09 +00:00
|
|
|
var scheduledTask = _scheduledTaskRepository.All().SingleOrDefault(c => c.TypeName == message.Command.Body.GetType().FullName);
|
2013-05-13 00:36:23 +00:00
|
|
|
|
2015-07-31 16:26:06 +00:00
|
|
|
if (scheduledTask != null && message.Command.Body.UpdateScheduledTask)
|
2013-05-13 00:36:23 +00:00
|
|
|
{
|
2014-08-02 01:08:02 +00:00
|
|
|
_logger.Trace("Updating last run time for: {0}", scheduledTask.TypeName);
|
2013-05-13 00:36:23 +00:00
|
|
|
_scheduledTaskRepository.SetLastExecutionTime(scheduledTask.Id, DateTime.UtcNow);
|
|
|
|
}
|
2013-05-11 23:38:41 +00:00
|
|
|
}
|
2013-08-06 05:22:58 +00:00
|
|
|
|
|
|
|
public void HandleAsync(ConfigSavedEvent message)
|
|
|
|
{
|
2013-09-12 22:41:50 +00:00
|
|
|
var rss = _scheduledTaskRepository.GetDefinition(typeof(RssSyncCommand));
|
2013-08-06 05:22:58 +00:00
|
|
|
rss.Interval = _configService.RssSyncInterval;
|
2014-03-14 09:41:56 +00:00
|
|
|
|
2017-01-28 20:30:46 +00:00
|
|
|
var netImport = _scheduledTaskRepository.GetDefinition(typeof(NetImportSyncCommand));
|
|
|
|
netImport.Interval = _configService.NetImportSyncInterval;
|
|
|
|
|
2019-05-02 10:51:21 +00:00
|
|
|
var checkForFinishedDownloads = _scheduledTaskRepository.GetDefinition(typeof(CheckForFinishedDownloadCommand));
|
|
|
|
checkForFinishedDownloads.Interval = _configService.CheckForFinishedDownloadInterval;
|
|
|
|
|
2018-11-23 07:03:32 +00:00
|
|
|
_scheduledTaskRepository.UpdateMany(new List<ScheduledTask> { rss, netImport, checkForFinishedDownloads });
|
2013-08-06 05:22:58 +00:00
|
|
|
}
|
2013-05-09 06:38:20 +00:00
|
|
|
}
|
2013-09-17 04:52:02 +00:00
|
|
|
}
|