2011-01-31 07:42:44 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Timers ;
2011-02-09 01:23:01 +00:00
using NLog ;
2011-01-31 07:42:44 +00:00
using NzbDrone.Core.Model.Notification ;
namespace NzbDrone.Core.Providers
{
2011-04-08 15:20:45 +00:00
public class TimerProvider
2011-01-31 07:42:44 +00:00
{
2011-02-25 01:18:22 +00:00
private readonly IRssSyncProvider _rssSyncProvider ;
2011-04-08 23:55:23 +00:00
private readonly SeriesProvider _seriesProvider ;
2011-02-25 01:18:22 +00:00
private readonly ISeasonProvider _seasonProvider ;
private readonly IEpisodeProvider _episodeProvider ;
private readonly IMediaFileProvider _mediaFileProvider ;
2011-02-10 06:51:04 +00:00
private Timer _rssSyncTimer ;
private Timer _minuteTimer ;
2011-02-09 01:23:01 +00:00
private DateTime _rssSyncNextInterval ;
2011-02-10 07:18:07 +00:00
private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
2011-02-09 01:23:01 +00:00
2011-04-08 23:55:23 +00:00
public TimerProvider ( IRssSyncProvider rssSyncProvider , SeriesProvider seriesProvider , ISeasonProvider seasonProvider , IEpisodeProvider episodeProvider , IMediaFileProvider mediaFileProvider )
2011-02-09 01:23:01 +00:00
{
_rssSyncProvider = rssSyncProvider ;
2011-02-10 06:51:04 +00:00
_seriesProvider = seriesProvider ;
_seasonProvider = seasonProvider ;
_episodeProvider = episodeProvider ;
2011-02-25 01:18:22 +00:00
_mediaFileProvider = mediaFileProvider ;
2011-02-10 06:51:04 +00:00
_rssSyncTimer = new Timer ( ) ;
_minuteTimer = new Timer ( 60000 ) ;
2011-02-09 01:23:01 +00:00
}
2011-01-31 07:42:44 +00:00
2011-04-08 15:20:45 +00:00
#region TimerProvider Members
2011-01-31 07:42:44 +00:00
2011-04-08 15:20:45 +00:00
public virtual void ResetRssSyncTimer ( )
2011-02-09 01:23:01 +00:00
{
double interval = _rssSyncTimer . Interval ;
2011-02-16 04:44:19 +00:00
_rssSyncTimer . Interval = interval ;
2011-02-09 01:23:01 +00:00
}
2011-04-08 15:20:45 +00:00
public virtual void StartRssSyncTimer ( )
2011-02-09 01:23:01 +00:00
{
2011-02-10 06:51:04 +00:00
if ( _rssSyncTimer . Interval < 900000 ) //If Timer is less than 15 minutes, throw an error! This should also be handled when saving the config, though a user could by-pass it by editing the DB directly... TNO (Trust No One)
2011-02-09 01:23:01 +00:00
{
Logger . Error ( "RSS Sync Frequency is invalid, please set the interval first" ) ;
throw new InvalidOperationException ( "RSS Sync Frequency Invalid" ) ;
}
2011-02-16 04:44:19 +00:00
_rssSyncTimer . Elapsed + = new ElapsedEventHandler ( RunRssSync ) ;
2011-02-09 01:23:01 +00:00
_rssSyncTimer . Start ( ) ;
_rssSyncNextInterval = DateTime . Now . AddMilliseconds ( _rssSyncTimer . Interval ) ;
}
2011-04-08 15:20:45 +00:00
public virtual void StopRssSyncTimer ( )
2011-02-09 01:23:01 +00:00
{
_rssSyncTimer . Stop ( ) ;
}
2011-04-08 15:20:45 +00:00
public virtual void SetRssSyncTimer ( int minutes )
2011-01-31 07:42:44 +00:00
{
2011-02-16 04:44:19 +00:00
long ms = minutes * 60 * 1000 ;
2011-02-09 01:23:01 +00:00
_rssSyncTimer . Interval = ms ;
2011-01-31 07:42:44 +00:00
}
2011-04-08 15:20:45 +00:00
public virtual TimeSpan RssSyncTimeLeft ( )
2011-01-31 07:42:44 +00:00
{
2011-02-09 01:23:01 +00:00
return _rssSyncNextInterval . Subtract ( DateTime . Now ) ;
2011-01-31 07:42:44 +00:00
}
2011-04-08 15:20:45 +00:00
public virtual DateTime NextRssSyncTime ( )
2011-01-31 07:42:44 +00:00
{
2011-02-09 01:23:01 +00:00
return _rssSyncNextInterval ;
2011-01-31 07:42:44 +00:00
}
2011-04-08 15:20:45 +00:00
public virtual void StartMinuteTimer ( )
2011-02-10 06:51:04 +00:00
{
_minuteTimer . Elapsed + = new ElapsedEventHandler ( MinuteTimer_Elapsed ) ;
_minuteTimer . Start ( ) ;
}
2011-04-08 15:20:45 +00:00
public virtual void StopMinuteTimer ( )
2011-02-10 06:51:04 +00:00
{
_minuteTimer . Stop ( ) ;
}
2011-01-31 07:42:44 +00:00
#endregion
2011-02-10 06:51:04 +00:00
private void RunRssSync ( object obj , ElapsedEventArgs args )
2011-02-09 01:23:01 +00:00
{
2011-02-10 07:18:07 +00:00
_rssSyncNextInterval = DateTime . Now . AddMilliseconds ( _rssSyncTimer . Interval ) ;
2011-02-16 04:44:19 +00:00
_rssSyncProvider . Begin ( ) ;
2011-02-09 01:23:01 +00:00
}
2011-02-10 06:51:04 +00:00
private void MinuteTimer_Elapsed ( object obj , ElapsedEventArgs args )
{
//Check to see if anything should be run at this time, if so run it
var now = DateTime . Now ;
//Daily (Except Sunday) 03:00 - Update the lastest season for all TV Shows
if ( now . Hour = = 3 & & now . Minute = = 0 & & now . DayOfWeek ! = DayOfWeek . Sunday )
{
foreach ( var series in _seriesProvider . GetAllSeries ( ) )
{
var season = _seasonProvider . GetLatestSeason ( series . SeriesId ) ;
_episodeProvider . RefreshEpisodeInfo ( season ) ;
}
}
//Sunday 03:00 - Update all TV Shows
if ( now . Hour = = 3 & & now . Minute = = 0 & & now . DayOfWeek = = DayOfWeek . Sunday )
{
foreach ( var series in _seriesProvider . GetAllSeries ( ) )
{
_episodeProvider . RefreshEpisodeInfo ( series . SeriesId ) ;
}
}
2011-02-25 01:18:22 +00:00
//Daily 00:00 (Midnight) - Cleanup (removed) EpisodeFiles + Scan for New EpisodeFiles
if ( now . Hour = = 0 & & now . Minute = = 0 )
{
foreach ( var series in _seriesProvider . GetAllSeries ( ) )
{
_mediaFileProvider . CleanUp ( series . EpisodeFiles ) ;
_mediaFileProvider . Scan ( series ) ;
}
}
2011-02-10 06:51:04 +00:00
throw new NotImplementedException ( ) ;
}
2011-01-31 07:42:44 +00:00
}
}