2013-02-18 08:59:43 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-04-24 03:56:00 +02:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-02-18 08:59:43 +01:00
|
|
|
using NzbDrone.Core.Datastore;
|
|
|
|
using NzbDrone.Core.Lifecycle;
|
|
|
|
|
2013-03-05 06:37:33 +01:00
|
|
|
namespace NzbDrone.Core.Jobs
|
2013-02-18 08:59:43 +01:00
|
|
|
{
|
2013-04-10 02:47:04 +02:00
|
|
|
public interface IJobRepository : IBasicRepository<JobDefinition>
|
2013-02-18 08:59:43 +01:00
|
|
|
{
|
|
|
|
IList<JobDefinition> GetPendingJobs();
|
|
|
|
JobDefinition GetDefinition(Type type);
|
|
|
|
}
|
|
|
|
|
2013-04-10 02:47:04 +02:00
|
|
|
public class JobRepository : BasicRepository<JobDefinition>, IJobRepository, IHandle<ApplicationStartedEvent>
|
2013-02-18 08:59:43 +01:00
|
|
|
{
|
|
|
|
private readonly IEnumerable<IJob> _jobs;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-03-25 04:51:32 +01:00
|
|
|
public JobRepository(IDatabase database, IEnumerable<IJob> jobs, Logger logger)
|
2013-03-24 05:16:00 +01:00
|
|
|
: base(database)
|
2013-02-18 08:59:43 +01:00
|
|
|
{
|
|
|
|
_jobs = jobs;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public JobDefinition GetDefinition(Type type)
|
|
|
|
{
|
2013-03-27 04:44:52 +01:00
|
|
|
return Query.Single(c => c.Type == type.FullName);
|
2013-02-18 08:59:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IList<JobDefinition> GetPendingJobs()
|
|
|
|
{
|
2013-03-27 04:44:52 +01:00
|
|
|
return Query.Where(c => c.Enable == true && c.Interval != 2).ToList().Where(c => c.LastExecution < DateTime.Now.AddMinutes(-c.Interval)).ToList();
|
2013-02-18 08:59:43 +01:00
|
|
|
}
|
|
|
|
|
2013-04-10 02:47:04 +02:00
|
|
|
public void Handle(ApplicationStartedEvent message)
|
2013-02-18 08:59:43 +01:00
|
|
|
{
|
2013-03-25 05:36:24 +01:00
|
|
|
var currentJobs = All().ToList();
|
2013-02-20 03:05:15 +01:00
|
|
|
_logger.Debug("Initializing jobs. Available: {0} Existing:{1}", _jobs.Count(), currentJobs.Count());
|
2013-02-18 08:59:43 +01:00
|
|
|
|
|
|
|
foreach (var currentJob in currentJobs)
|
|
|
|
{
|
2013-03-25 05:36:24 +01:00
|
|
|
if (_jobs.All(c => c.GetType().ToString() != currentJob.Type))
|
2013-02-18 08:59:43 +01:00
|
|
|
{
|
|
|
|
_logger.Debug("Removing job from database '{0}'", currentJob.Name);
|
2013-02-26 04:58:57 +01:00
|
|
|
Delete(currentJob.Id);
|
2013-02-18 08:59:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var job in _jobs)
|
|
|
|
{
|
2013-03-25 05:36:24 +01:00
|
|
|
var jobDefinition = currentJobs.SingleOrDefault(c => c.Type == job.GetType().ToString());
|
2013-02-18 08:59:43 +01:00
|
|
|
|
|
|
|
if (jobDefinition == null)
|
|
|
|
{
|
|
|
|
jobDefinition = new JobDefinition
|
2013-04-10 02:47:04 +02:00
|
|
|
{
|
|
|
|
Type = job.GetType().ToString(),
|
|
|
|
LastExecution = DateTime.Now
|
|
|
|
};
|
2013-02-18 08:59:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
jobDefinition.Enable = job.DefaultInterval.TotalSeconds > 0;
|
|
|
|
jobDefinition.Name = job.Name;
|
|
|
|
|
|
|
|
jobDefinition.Interval = Convert.ToInt32(job.DefaultInterval.TotalMinutes);
|
|
|
|
|
2013-03-24 05:16:00 +01:00
|
|
|
Upsert(jobDefinition);
|
2013-02-18 08:59:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-25 04:51:32 +01:00
|
|
|
}
|