mirror of
https://github.com/Radarr/Radarr
synced 2024-12-30 20:05:41 +00:00
4bb4faf626
Still need to remove System.Data.Sqlite, prefer an option in OrmLite to pluralize table names.
76 lines
No EOL
2.3 KiB
C#
76 lines
No EOL
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using NLog;
|
|
using NzbDrone.Core.Datastore;
|
|
using NzbDrone.Core.Lifecycle;
|
|
|
|
namespace NzbDrone.Core.Jobs
|
|
{
|
|
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;
|
|
|
|
public JobRepository(IDbConnection database, IEnumerable<IJob> jobs, Logger logger)
|
|
: base(database)
|
|
{
|
|
_jobs = jobs;
|
|
_logger = logger;
|
|
}
|
|
|
|
public JobDefinition GetDefinition(Type type)
|
|
{
|
|
return Single(c => c.Type == type.FullName);
|
|
}
|
|
|
|
|
|
public IList<JobDefinition> GetPendingJobs()
|
|
{
|
|
return Where(c => c.Enable && c.LastExecution < DateTime.Now.AddMinutes(-c.Interval)).ToList();
|
|
}
|
|
|
|
public void Init()
|
|
{
|
|
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);
|
|
|
|
Upsert(jobDefinition);
|
|
}
|
|
}
|
|
}
|
|
} |