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

77 lines
2.4 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;
using NzbDrone.Core.Lifecycle;
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 IEnumerable<IJob> _jobs;
private readonly Logger _logger;
2013-03-25 03:51:32 +00:00
public JobRepository(IDatabase database, IEnumerable<IJob> jobs, Logger logger)
2013-03-24 04:16:00 +00:00
: base(database)
{
_jobs = jobs;
_logger = logger;
}
public JobDefinition GetDefinition(Type type)
{
2013-03-27 03:44:52 +00:00
return Query.Single(c => c.Type == type.FullName);
}
public IList<JobDefinition> GetPendingJobs()
{
2013-03-27 03:44:52 +00:00
return Query.Where(c => c.Enable == true && c.Interval != 2).ToList().Where(c => c.LastExecution < DateTime.Now.AddMinutes(-c.Interval)).ToList();
}
public void Handle(ApplicationStartedEvent message)
{
var currentJobs = All().ToList();
_logger.Debug("Initializing jobs. Available: {0} Existing:{1}", _jobs.Count(), currentJobs.Count());
foreach (var currentJob in currentJobs)
{
if (_jobs.All(c => c.GetType().ToString() != currentJob.Type))
{
_logger.Debug("Removing job from database '{0}'", currentJob.Name);
Delete(currentJob.Id);
}
}
foreach (var job in _jobs)
{
var jobDefinition = currentJobs.SingleOrDefault(c => c.Type == job.GetType().ToString());
if (jobDefinition == null)
{
jobDefinition = new JobDefinition
{
Type = job.GetType().ToString(),
LastExecution = DateTime.Now
};
}
jobDefinition.Enable = job.DefaultInterval.TotalSeconds > 0;
jobDefinition.Name = job.Name;
jobDefinition.Interval = Convert.ToInt32(job.DefaultInterval.TotalMinutes);
2013-03-24 04:16:00 +00:00
Upsert(jobDefinition);
}
}
}
2013-03-25 03:51:32 +00:00
}