parent
fa8f67d7fe
commit
32431540c5
@ -1,78 +1,35 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using NLog;
|
|
||||||
using NzbDrone.Common.Messaging;
|
using NzbDrone.Common.Messaging;
|
||||||
using NzbDrone.Core.Datastore;
|
using NzbDrone.Core.Datastore;
|
||||||
using NzbDrone.Core.Indexers;
|
|
||||||
using NzbDrone.Core.Lifecycle;
|
|
||||||
using NzbDrone.Core.Providers;
|
|
||||||
|
|
||||||
namespace NzbDrone.Core.Jobs
|
namespace NzbDrone.Core.Jobs
|
||||||
{
|
{
|
||||||
public interface IJobRepository : IBasicRepository<JobDefinition>
|
public interface IScheduledTaskRepository : IBasicRepository<ScheduledTask>
|
||||||
{
|
{
|
||||||
IList<JobDefinition> GetPendingJobs();
|
IList<ScheduledTask> GetPendingJobs();
|
||||||
JobDefinition GetDefinition(Type type);
|
ScheduledTask GetDefinition(Type type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class JobRepository : BasicRepository<JobDefinition>, IJobRepository, IHandle<ApplicationStartedEvent>
|
|
||||||
|
public class ScheduledTaskRepository : BasicRepository<ScheduledTask>, IScheduledTaskRepository
|
||||||
{
|
{
|
||||||
private readonly Logger _logger;
|
|
||||||
|
|
||||||
public JobRepository(IDatabase database, Logger logger, IMessageAggregator messageAggregator)
|
public ScheduledTaskRepository(IDatabase database, IMessageAggregator messageAggregator)
|
||||||
: base(database, messageAggregator)
|
: base(database, messageAggregator)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JobDefinition GetDefinition(Type type)
|
public ScheduledTask GetDefinition(Type type)
|
||||||
{
|
{
|
||||||
return Query.Single(c => c.Name == type.FullName);
|
return Query.Single(c => c.Name == type.FullName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public IList<JobDefinition> GetPendingJobs()
|
public IList<ScheduledTask> GetPendingJobs()
|
||||||
{
|
{
|
||||||
return Query.Where(c => c.Interval != 0).ToList().Where(c => c.LastExecution < DateTime.Now.AddMinutes(-c.Interval)).ToList();
|
return Query.Where(c => c.Interval != 0).ToList().Where(c => c.LastExecution < DateTime.Now.AddMinutes(-c.Interval)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(ApplicationStartedEvent message)
|
|
||||||
{
|
|
||||||
/* var currentJobs = All().ToList();
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
if (!timers.Any(c => c.Name == job.Name))
|
|
||||||
{
|
|
||||||
_logger.Debug("Removing job from database '{0}'", job.Name);
|
|
||||||
Delete(job.Id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var job in timers)
|
|
||||||
{
|
|
||||||
var currentDefinition = currentJobs.SingleOrDefault(c => c.Name == job.GetType().ToString());
|
|
||||||
|
|
||||||
if (currentDefinition == null)
|
|
||||||
{
|
|
||||||
currentDefinition = job;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentDefinition.Interval = job.Interval;
|
|
||||||
|
|
||||||
Upsert(currentDefinition);
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,69 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using NLog;
|
||||||
|
using NzbDrone.Common.Messaging;
|
||||||
|
using NzbDrone.Core.Indexers;
|
||||||
|
using NzbDrone.Core.Lifecycle;
|
||||||
|
using NzbDrone.Core.Providers;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Jobs
|
||||||
|
{
|
||||||
|
public interface ITaskManager
|
||||||
|
{
|
||||||
|
IList<ScheduledTask> GetPending();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TaskManager : IHandle<ApplicationStartedEvent>, ITaskManager
|
||||||
|
{
|
||||||
|
private readonly IScheduledTaskRepository _scheduledTaskRepository;
|
||||||
|
private readonly Logger _logger;
|
||||||
|
|
||||||
|
public TaskManager(IScheduledTaskRepository scheduledTaskRepository, Logger logger)
|
||||||
|
{
|
||||||
|
_scheduledTaskRepository = scheduledTaskRepository;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public IList<ScheduledTask> GetPending()
|
||||||
|
{
|
||||||
|
return _scheduledTaskRepository.GetPendingJobs();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Handle(ApplicationStartedEvent message)
|
||||||
|
{
|
||||||
|
var defaultTasks = new[]
|
||||||
|
{
|
||||||
|
new ScheduledTask{ Interval = 25, Name = typeof(RssSyncCommand).FullName},
|
||||||
|
new ScheduledTask{ Interval = 24*60, Name = typeof(UpdateXemMappings).FullName}
|
||||||
|
};
|
||||||
|
|
||||||
|
var currentTasks = _scheduledTaskRepository.All();
|
||||||
|
|
||||||
|
|
||||||
|
_logger.Debug("Initializing jobs. Available: {0} Existing:{1}", defaultTasks.Count(), currentTasks.Count());
|
||||||
|
|
||||||
|
|
||||||
|
foreach (var job in currentTasks)
|
||||||
|
{
|
||||||
|
if (!defaultTasks.Any(c => c.Name == job.Name))
|
||||||
|
{
|
||||||
|
_logger.Debug("Removing job from database '{0}'", job.Name);
|
||||||
|
_scheduledTaskRepository.Delete(job.Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var defaultTask in defaultTasks)
|
||||||
|
{
|
||||||
|
var currentDefinition = currentTasks.SingleOrDefault(c => c.Name == defaultTask.Name);
|
||||||
|
|
||||||
|
if (currentDefinition == null)
|
||||||
|
{
|
||||||
|
currentDefinition = defaultTask;
|
||||||
|
_scheduledTaskRepository.Upsert(currentDefinition);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue