2013-02-18 07:59:43 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Datastore;
|
|
|
|
using NzbDrone.Core.Lifecycle;
|
|
|
|
|
2013-03-05 05:37:33 +00:00
|
|
|
namespace NzbDrone.Core.Jobs
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
|
|
|
public interface IJobRepository : IInitializable, IBasicRepository<JobDefinition>
|
|
|
|
{
|
|
|
|
IList<JobDefinition> GetPendingJobs();
|
|
|
|
JobDefinition GetDefinition(Type type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class JobRepository : BasicRepository<JobDefinition>, IJobRepository
|
|
|
|
{
|
|
|
|
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)
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
|
|
|
_jobs = jobs;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public JobDefinition GetDefinition(Type type)
|
|
|
|
{
|
2013-03-25 04:46:51 +00:00
|
|
|
return Queryable().Single(c => c.Type == type.FullName);
|
2013-02-18 07:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IList<JobDefinition> GetPendingJobs()
|
|
|
|
{
|
2013-03-27 00:48:07 +00:00
|
|
|
return Queryable().Where(c => c.Enable == true && c.Interval != 2).ToList().Where(c => c.LastExecution < DateTime.Now.AddMinutes(-c.Interval)).ToList();
|
2013-02-18 07:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
{
|
2013-03-25 04:36:24 +00:00
|
|
|
var currentJobs = All().ToList();
|
2013-02-20 02:05:15 +00:00
|
|
|
_logger.Debug("Initializing jobs. Available: {0} Existing:{1}", _jobs.Count(), currentJobs.Count());
|
2013-02-18 07:59:43 +00:00
|
|
|
|
|
|
|
foreach (var currentJob in currentJobs)
|
|
|
|
{
|
2013-03-25 04:36:24 +00:00
|
|
|
if (_jobs.All(c => c.GetType().ToString() != currentJob.Type))
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
|
|
|
_logger.Debug("Removing job from database '{0}'", currentJob.Name);
|
2013-02-26 03:58:57 +00:00
|
|
|
Delete(currentJob.Id);
|
2013-02-18 07:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var job in _jobs)
|
|
|
|
{
|
2013-03-25 04:36:24 +00:00
|
|
|
var jobDefinition = currentJobs.SingleOrDefault(c => c.Type == job.GetType().ToString());
|
2013-02-18 07:59:43 +00:00
|
|
|
|
|
|
|
if (jobDefinition == null)
|
|
|
|
{
|
|
|
|
jobDefinition = new JobDefinition
|
|
|
|
{
|
2013-03-25 04:36:24 +00:00
|
|
|
Type = job.GetType().ToString(),
|
2013-02-18 07:59:43 +00:00
|
|
|
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-02-18 07:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-25 03:51:32 +00:00
|
|
|
}
|