2013-02-18 07:59:43 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-04-24 01:56:00 +00:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-02-18 07:59:43 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
2013-05-07 00:39:33 +00:00
|
|
|
using NzbDrone.Core.Indexers;
|
2013-02-18 07:59:43 +00:00
|
|
|
using NzbDrone.Core.Lifecycle;
|
2013-05-07 00:39:33 +00:00
|
|
|
using NzbDrone.Core.Providers;
|
2013-02-18 07:59:43 +00:00
|
|
|
|
2013-03-05 05:37:33 +00:00
|
|
|
namespace NzbDrone.Core.Jobs
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
2013-04-10 00:47:04 +00:00
|
|
|
public interface IJobRepository : IBasicRepository<JobDefinition>
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
|
|
|
IList<JobDefinition> GetPendingJobs();
|
|
|
|
JobDefinition GetDefinition(Type type);
|
|
|
|
}
|
|
|
|
|
2013-04-10 00:47:04 +00:00
|
|
|
public class JobRepository : BasicRepository<JobDefinition>, IJobRepository, IHandle<ApplicationStartedEvent>
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
|
|
|
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)
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public JobDefinition GetDefinition(Type type)
|
|
|
|
{
|
2013-05-07 00:39:33 +00:00
|
|
|
return Query.Single(c => c.Name == type.FullName);
|
2013-02-18 07:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
2013-02-18 07:59:43 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 00:47:04 +00:00
|
|
|
public void Handle(ApplicationStartedEvent message)
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
2013-05-07 04:31:55 +00:00
|
|
|
/* var currentJobs = All().ToList();
|
2013-02-18 07:59:43 +00:00
|
|
|
|
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-02-18 07:59:43 +00:00
|
|
|
{
|
2013-05-07 00:39:33 +00:00
|
|
|
if (!timers.Any(c => c.Name == job.Name))
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
2013-05-07 00:39:33 +00:00
|
|
|
_logger.Debug("Removing job from database '{0}'", job.Name);
|
|
|
|
Delete(job.Id);
|
2013-02-18 07:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 00:39:33 +00:00
|
|
|
foreach (var job in timers)
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
2013-05-07 00:39:33 +00:00
|
|
|
var currentDefinition = currentJobs.SingleOrDefault(c => c.Name == job.GetType().ToString());
|
2013-02-18 07:59:43 +00:00
|
|
|
|
2013-05-07 00:39:33 +00:00
|
|
|
if (currentDefinition == null)
|
2013-02-18 07:59:43 +00:00
|
|
|
{
|
2013-05-07 00:39:33 +00:00
|
|
|
currentDefinition = job;
|
2013-02-18 07:59:43 +00:00
|
|
|
}
|
|
|
|
|
2013-05-07 00:39:33 +00:00
|
|
|
currentDefinition.Interval = job.Interval;
|
2013-02-18 07:59:43 +00:00
|
|
|
|
2013-05-07 00:39:33 +00:00
|
|
|
Upsert(currentDefinition);
|
2013-05-07 04:31:55 +00:00
|
|
|
}*/
|
2013-02-18 07:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-25 03:51:32 +00:00
|
|
|
}
|