sonarr-repo-only/NzbDrone.Core/Jobs/JobRepository.cs

79 lines
2.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common.Messaging;
using NzbDrone.Core.Datastore;
2013-05-07 00:39:33 +00:00
using NzbDrone.Core.Indexers;
using NzbDrone.Core.Lifecycle;
2013-05-07 00:39:33 +00:00
using NzbDrone.Core.Providers;
2013-03-05 05:37:33 +00:00
namespace NzbDrone.Core.Jobs
{
public interface IJobRepository : IBasicRepository<JobDefinition>
{
IList<JobDefinition> GetPendingJobs();
JobDefinition GetDefinition(Type type);
}
public class JobRepository : BasicRepository<JobDefinition>, IJobRepository, IHandle<ApplicationStartedEvent>
{
private readonly Logger _logger;
2013-05-07 00:39:33 +00:00
public JobRepository(IDatabase database, Logger logger, IMessageAggregator messageAggregator)
2013-05-05 21:24:33 +00:00
: base(database, messageAggregator)
{
_logger = logger;
}
public JobDefinition GetDefinition(Type type)
{
2013-05-07 00:39:33 +00:00
return Query.Single(c => c.Name == type.FullName);
}
public IList<JobDefinition> GetPendingJobs()
{
2013-05-07 00:39:33 +00:00
return Query.Where(c => c.Interval != 0).ToList().Where(c => c.LastExecution < DateTime.Now.AddMinutes(-c.Interval)).ToList();
}
public void Handle(ApplicationStartedEvent message)
{
2013-05-07 04:31:55 +00:00
/* var currentJobs = All().ToList();
2013-05-07 00:39:33 +00:00
var timers = new[]
{
new JobDefinition{ Interval = 25, Name = typeof(RssSyncCommand).FullName},
new JobDefinition{ Interval = 24*60, Name = typeof(UpdateXemMappings).FullName}
};
_logger.Debug("Initializing jobs. Available: {0} Existing:{1}", timers.Count(), currentJobs.Count());
foreach (var job in currentJobs)
{
2013-05-07 00:39:33 +00:00
if (!timers.Any(c => c.Name == job.Name))
{
2013-05-07 00:39:33 +00:00
_logger.Debug("Removing job from database '{0}'", job.Name);
Delete(job.Id);
}
}
2013-05-07 00:39:33 +00:00
foreach (var job in timers)
{
2013-05-07 00:39:33 +00:00
var currentDefinition = currentJobs.SingleOrDefault(c => c.Name == job.GetType().ToString());
2013-05-07 00:39:33 +00:00
if (currentDefinition == null)
{
2013-05-07 00:39:33 +00:00
currentDefinition = job;
}
2013-05-07 00:39:33 +00:00
currentDefinition.Interval = job.Interval;
2013-05-07 00:39:33 +00:00
Upsert(currentDefinition);
2013-05-07 04:31:55 +00:00
}*/
}
}
2013-03-25 03:51:32 +00:00
}