| 
									
										
										
										
											2013-05-10 16:53:50 -07:00
										 |  |  | using System; | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  | using System.Collections.Generic; | 
					
						
							|  |  |  | using System.Linq; | 
					
						
							|  |  |  | using NLog; | 
					
						
							|  |  |  | using NzbDrone.Common.Messaging; | 
					
						
							| 
									
										
										
										
											2013-08-27 23:51:42 -07:00
										 |  |  | using NzbDrone.Common.Messaging.Events; | 
					
						
							| 
									
										
										
										
											2013-08-05 22:22:58 -07:00
										 |  |  | using NzbDrone.Core.Configuration; | 
					
						
							|  |  |  | using NzbDrone.Core.Configuration.Events; | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  | using NzbDrone.Core.Indexers; | 
					
						
							| 
									
										
										
										
											2013-06-18 18:01:08 -07:00
										 |  |  | using NzbDrone.Core.Instrumentation.Commands; | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  | using NzbDrone.Core.Lifecycle; | 
					
						
							| 
									
										
										
										
											2013-05-12 19:52:55 -07:00
										 |  |  | using NzbDrone.Core.MediaFiles.Commands; | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  | using NzbDrone.Core.Providers; | 
					
						
							| 
									
										
										
										
											2013-06-02 22:41:24 -07:00
										 |  |  | using NzbDrone.Core.Tv.Commands; | 
					
						
							| 
									
										
										
										
											2013-05-19 17:30:02 -07:00
										 |  |  | using NzbDrone.Core.Update.Commands; | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace NzbDrone.Core.Jobs | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public interface ITaskManager | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         IList<ScheduledTask> GetPending(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 22:22:58 -07:00
										 |  |  |     public class TaskManager : ITaskManager, IHandle<ApplicationStartedEvent>, IHandleAsync<CommandExecutedEvent>, IHandleAsync<ConfigSavedEvent> | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         private readonly IScheduledTaskRepository _scheduledTaskRepository; | 
					
						
							| 
									
										
										
										
											2013-08-05 22:22:58 -07:00
										 |  |  |         private readonly IConfigService _configService; | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  |         private readonly Logger _logger; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 22:22:58 -07:00
										 |  |  |         public TaskManager(IScheduledTaskRepository scheduledTaskRepository, IConfigService configService, Logger logger) | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  |         { | 
					
						
							|  |  |  |             _scheduledTaskRepository = scheduledTaskRepository; | 
					
						
							| 
									
										
										
										
											2013-08-05 22:22:58 -07:00
										 |  |  |             _configService = configService; | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  |             _logger = logger; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         public IList<ScheduledTask> GetPending() | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-10 22:59:42 -07:00
										 |  |  |             return _scheduledTaskRepository.All().Where(c => c.LastExecution.AddMinutes(c.Interval) < DateTime.UtcNow).ToList(); | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         public void Handle(ApplicationStartedEvent message) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             var defaultTasks = new[] | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2013-08-05 22:22:58 -07:00
										 |  |  |                     new ScheduledTask{ Interval = _configService.RssSyncInterval, TypeName = typeof(RssSyncCommand).FullName}, | 
					
						
							| 
									
										
										
										
											2013-07-22 22:50:32 -07:00
										 |  |  |                     new ScheduledTask{ Interval = 12*60, TypeName = typeof(UpdateXemMappingsCommand).FullName}, | 
					
						
							| 
									
										
										
										
											2013-07-17 10:44:48 -07:00
										 |  |  |                     new ScheduledTask{ Interval = 12*60, TypeName = typeof(RefreshSeriesCommand).FullName}, | 
					
						
							| 
									
										
										
										
											2013-05-19 17:30:02 -07:00
										 |  |  |                     new ScheduledTask{ Interval = 1, TypeName = typeof(DownloadedEpisodesScanCommand).FullName}, | 
					
						
							| 
									
										
										
										
											2013-07-29 19:34:29 -07:00
										 |  |  |                     new ScheduledTask{ Interval = 60, TypeName = typeof(ApplicationUpdateCommand).FullName}, | 
					
						
							| 
									
										
										
										
											2013-06-18 18:01:08 -07:00
										 |  |  |                     new ScheduledTask{ Interval = 1*60, TypeName = typeof(TrimLogCommand).FullName} | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  |                 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             var currentTasks = _scheduledTaskRepository.All(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             _logger.Debug("Initializing jobs. Available: {0} Existing:{1}", defaultTasks.Count(), currentTasks.Count()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             foreach (var job in currentTasks) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2013-05-10 16:53:50 -07:00
										 |  |  |                 if (!defaultTasks.Any(c => c.TypeName == job.TypeName)) | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  |                 { | 
					
						
							| 
									
										
										
										
											2013-05-10 16:53:50 -07:00
										 |  |  |                     _logger.Debug("Removing job from database '{0}'", job.TypeName); | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  |                     _scheduledTaskRepository.Delete(job.Id); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             foreach (var defaultTask in defaultTasks) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2013-05-20 21:46:30 -07:00
										 |  |  |                 var currentDefinition = currentTasks.SingleOrDefault(c => c.TypeName == defaultTask.TypeName) ?? defaultTask; | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-20 21:46:30 -07:00
										 |  |  |                 currentDefinition.Interval = defaultTask.Interval; | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-20 21:46:30 -07:00
										 |  |  |                 _scheduledTaskRepository.Upsert(currentDefinition); | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-05-11 16:38:41 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         public void HandleAsync(CommandExecutedEvent message) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2013-05-12 17:36:23 -07:00
										 |  |  |             var scheduledTask = _scheduledTaskRepository.All().SingleOrDefault(c => c.TypeName == message.Command.GetType().FullName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (scheduledTask != null) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 _scheduledTaskRepository.SetLastExecutionTime(scheduledTask.Id, DateTime.UtcNow); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2013-05-11 16:38:41 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-08-05 22:22:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         public void HandleAsync(ConfigSavedEvent message) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             var rss = _scheduledTaskRepository.GetDefinition(typeof (RssSyncCommand)); | 
					
						
							|  |  |  |             rss.Interval = _configService.RssSyncInterval; | 
					
						
							|  |  |  |             _scheduledTaskRepository.Update(rss); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-05-08 23:38:20 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | } |