update task triggers

This commit is contained in:
Luke Pulverenti 2015-07-28 08:33:30 -04:00
parent ba2574a03b
commit 3799ad5940
19 changed files with 127 additions and 94 deletions

View File

@ -312,7 +312,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
trigger.Triggered -= trigger_Triggered;
trigger.Triggered += trigger_Triggered;
trigger.Start(isApplicationStartup);
trigger.Start(LastExecutionResult, isApplicationStartup);
}
}
@ -340,7 +340,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks
await Task.Delay(1000).ConfigureAwait(false);
trigger.Start(false);
trigger.Start(LastExecutionResult, false);
}
private Task _currentTask;

View File

@ -45,9 +45,6 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
// Until we can vary these default triggers per server and MBT, we need something that makes sense for both
return new ITaskTrigger[] {
// At startup
new StartupTrigger {DelayMs = 60000},
// Every so often
new IntervalTrigger { Interval = TimeSpan.FromHours(24)}
};

View File

@ -42,9 +42,6 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks
// Until we can vary these default triggers per server and MBT, we need something that makes sense for both
return new ITaskTrigger[] {
// At startup
new StartupTrigger {DelayMs = 30000},
// Every so often
new IntervalTrigger { Interval = TimeSpan.FromHours(24)}
};

View File

@ -1,6 +1,7 @@
using System;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Tasks;
using System;
using System.Threading;
using MediaBrowser.Model.Events;
namespace MediaBrowser.Common.ScheduledTasks
{
@ -32,8 +33,9 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <summary>
/// Stars waiting for the trigger action
/// </summary>
/// <param name="lastResult">The last result.</param>
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
public void Start(bool isApplicationStartup)
public void Start(TaskResult lastResult, bool isApplicationStartup)
{
DisposeTimer();

View File

@ -1,5 +1,6 @@
using System;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Tasks;
using System;
namespace MediaBrowser.Common.ScheduledTasks
{
@ -16,8 +17,9 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <summary>
/// Stars waiting for the trigger action
/// </summary>
/// <param name="lastResult">The last result.</param>
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
void Start(bool isApplicationStartup);
void Start(TaskResult lastResult, bool isApplicationStartup);
/// <summary>
/// Stops waiting for the trigger action

View File

@ -1,6 +1,7 @@
using System;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Tasks;
using System;
using System.Threading;
using MediaBrowser.Model.Events;
namespace MediaBrowser.Common.ScheduledTasks
{
@ -32,12 +33,27 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <summary>
/// Stars waiting for the trigger action
/// </summary>
/// <param name="lastResult">The last result.</param>
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
public void Start(bool isApplicationStartup)
public void Start(TaskResult lastResult, bool isApplicationStartup)
{
DisposeTimer();
Timer = new Timer(state => OnTriggered(), null, Interval, TimeSpan.FromMilliseconds(-1));
var triggerDate = lastResult != null ?
lastResult.EndTimeUtc.Add(Interval) :
DateTime.UtcNow.Add(Interval);
if (DateTime.UtcNow > triggerDate)
{
triggerDate = DateTime.UtcNow;
if (isApplicationStartup)
{
triggerDate = triggerDate.AddMinutes(1);
}
}
Timer = new Timer(state => OnTriggered(), null, triggerDate - DateTime.UtcNow, TimeSpan.FromMilliseconds(-1));
}
/// <summary>

View File

@ -1,6 +1,7 @@
using System;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Tasks;
using System;
using System.Threading.Tasks;
using MediaBrowser.Model.Events;
namespace MediaBrowser.Common.ScheduledTasks
{
@ -27,8 +28,9 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <summary>
/// Stars waiting for the trigger action
/// </summary>
/// <param name="lastResult">The last result.</param>
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
public async void Start(bool isApplicationStartup)
public async void Start(TaskResult lastResult, bool isApplicationStartup)
{
if (isApplicationStartup)
{

View File

@ -1,8 +1,8 @@
using MediaBrowser.Model.Tasks;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Tasks;
using Microsoft.Win32;
using System;
using System.Threading.Tasks;
using MediaBrowser.Model.Events;
namespace MediaBrowser.Common.ScheduledTasks
{
@ -28,8 +28,9 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <summary>
/// Stars waiting for the trigger action
/// </summary>
/// <param name="lastResult">The last result.</param>
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
public void Start(bool isApplicationStartup)
public void Start(TaskResult lastResult, bool isApplicationStartup)
{
switch (SystemEvent)
{

View File

@ -1,6 +1,7 @@
using System;
using System.Threading;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Tasks;
namespace MediaBrowser.Common.ScheduledTasks
{
@ -38,8 +39,9 @@ namespace MediaBrowser.Common.ScheduledTasks
/// <summary>
/// Stars waiting for the trigger action
/// </summary>
/// <param name="lastResult">The last result.</param>
/// <param name="isApplicationStartup">if set to <c>true</c> [is application startup].</param>
public void Start(bool isApplicationStartup)
public void Start(TaskResult lastResult, bool isApplicationStartup)
{
DisposeTimer();

View File

@ -1115,6 +1115,18 @@ namespace MediaBrowser.Controller.Entities
return value.Value <= maxAllowedRating.Value;
}
public int? GetParentalRatingValue()
{
var rating = CustomRatingForComparison;
if (string.IsNullOrWhiteSpace(rating))
{
rating = OfficialRatingForComparison;
}
return LocalizationManager.GetRatingLevel(rating);
}
private bool IsVisibleViaTags(User user)
{
var hasTags = this as IHasTags;

View File

@ -94,6 +94,7 @@ namespace MediaBrowser.Controller.Entities
public string[] ChannelIds { get; set; }
internal List<Guid> ItemIdsFromPersonFilters { get; set; }
public int? MaxParentalRating { get; set; }
public InternalItemsQuery()
{

View File

@ -5,12 +5,12 @@ using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Controller.Sorting;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Querying;
namespace MediaBrowser.Controller.Library
{

View File

@ -42,10 +42,6 @@ namespace MediaBrowser.Server.Implementations.Channels
{
return new ITaskTrigger[]
{
new StartupTrigger{DelayMs = 10000},
new SystemEventTrigger{ SystemEvent = SystemEvent.WakeFromSleep},
new IntervalTrigger{ Interval = TimeSpan.FromHours(24)}
};
}

View File

@ -13,6 +13,7 @@ using MediaBrowser.Controller.Localization;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Sorting;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.LiveTv;
@ -772,9 +773,21 @@ namespace MediaBrowser.Server.Implementations.LiveTv
MaxStartDate = query.MaxStartDate,
ChannelIds = query.ChannelIds,
IsMovie = query.IsMovie,
IsSports = query.IsSports
IsSports = query.IsSports,
Genres = query.Genres
};
var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId);
if (user != null)
{
internalQuery.MaxParentalRating = user.Policy.MaxParentalRating;
if (user.Policy.BlockUnratedItems.Contains(UnratedItem.LiveTvProgram))
{
internalQuery.HasParentalRating = true;
}
}
if (query.HasAired.HasValue)
{
if (query.HasAired.Value)
@ -789,20 +802,6 @@ namespace MediaBrowser.Server.Implementations.LiveTv
IEnumerable<LiveTvProgram> programs = _libraryManager.GetItems(internalQuery).Items.Cast<LiveTvProgram>();
// Apply genre filter
if (query.Genres.Length > 0)
{
programs = programs.Where(p => p.Genres.Any(g => query.Genres.Contains(g, StringComparer.OrdinalIgnoreCase)));
}
var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId);
if (user != null)
{
// Avoid implicitly captured closure
var currentUser = user;
programs = programs.Where(i => i.IsVisible(currentUser));
}
programs = _libraryManager.Sort(programs, user, query.SortBy, query.SortOrder ?? SortOrder.Ascending)
.Cast<LiveTvProgram>();
@ -860,13 +859,18 @@ namespace MediaBrowser.Server.Implementations.LiveTv
}
}
IEnumerable<LiveTvProgram> programs = _libraryManager.GetItems(internalQuery).Items.Cast<LiveTvProgram>();
var user = _userManager.GetUserById(query.UserId);
if (user != null)
{
internalQuery.MaxParentalRating = user.Policy.MaxParentalRating;
// Avoid implicitly captured closure
var currentUser = user;
programs = programs.Where(i => i.IsVisible(currentUser));
if (user.Policy.BlockUnratedItems.Contains(UnratedItem.LiveTvProgram))
{
internalQuery.HasParentalRating = true;
}
}
IEnumerable<LiveTvProgram> programs = _libraryManager.GetItems(internalQuery).Items.Cast<LiveTvProgram>();
var programList = programs.ToList();

View File

@ -2,7 +2,6 @@
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
@ -47,11 +46,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv
{
return new ITaskTrigger[]
{
new StartupTrigger(),
new SystemEventTrigger{ SystemEvent = SystemEvent.WakeFromSleep},
new IntervalTrigger{ Interval = TimeSpan.FromHours(4)}
new IntervalTrigger{ Interval = TimeSpan.FromHours(12)}
};
}

View File

@ -157,6 +157,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
_connection.AddColumn(_logger, "TypedBaseItems", "PremiereDate", "DATETIME");
_connection.AddColumn(_logger, "TypedBaseItems", "ProductionYear", "INT");
_connection.AddColumn(_logger, "TypedBaseItems", "ParentId", "GUID");
_connection.AddColumn(_logger, "TypedBaseItems", "Genres", "Text");
_connection.AddColumn(_logger, "TypedBaseItems", "ParentalRatingValue", "INT");
PrepareStatements();
@ -197,10 +199,12 @@ namespace MediaBrowser.Server.Implementations.Persistence
"ParentIndexNumber",
"PremiereDate",
"ProductionYear",
"ParentId"
"ParentId",
"Genres",
"ParentalRatingValue"
};
_saveItemCommand = _connection.CreateCommand();
_saveItemCommand.CommandText = "replace into TypedBaseItems (" + string.Join(",", saveColumns.ToArray()) + ") values (@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19, @20, @21, @22)";
_saveItemCommand.CommandText = "replace into TypedBaseItems (" + string.Join(",", saveColumns.ToArray()) + ") values (@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19, @20, @21, @22, @23, @24)";
for (var i = 1; i <= saveColumns.Count; i++)
{
_saveItemCommand.Parameters.Add(_saveItemCommand, "@" + i.ToString(CultureInfo.InvariantCulture));
@ -343,6 +347,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
_saveItemCommand.GetParameter(index++).Value = item.ParentId;
}
_saveItemCommand.GetParameter(index++).Value = string.Join("|", item.Genres.ToArray());
_saveItemCommand.GetParameter(index++).Value = item.GetParentalRatingValue();
_saveItemCommand.Transaction = transaction;
_saveItemCommand.ExecuteNonQuery();
@ -938,6 +945,38 @@ namespace MediaBrowser.Server.Implementations.Persistence
cmd.Parameters.Add(cmd, "@NameContains", DbType.String).Value = "%" + query.NameContains + "%";
}
if (query.Genres.Length > 0)
{
var genres = new List<string>();
var index = 0;
foreach (var genre in query.Genres)
{
genres.Add("Genres like @Genres" + index);
cmd.Parameters.Add(cmd, "@Genres" + index, DbType.String).Value = "%" + genre + "%";
index++;
}
var genreCaluse = "(" + string.Join(" OR ", genres.ToArray()) + ")";
whereClauses.Add(genreCaluse);
}
if (query.MaxParentalRating.HasValue)
{
whereClauses.Add("(ParentalRatingValue is NULL OR ParentalRatingValue<=@MaxParentalRating)");
cmd.Parameters.Add(cmd, "@MaxParentalRating", DbType.Int32).Value = query.MaxParentalRating.Value;
}
if (query.HasParentalRating.HasValue)
{
if (query.HasParentalRating.Value)
{
whereClauses.Add("ParentalRatingValue NOT NULL");
}
else
{
whereClauses.Add("ParentalRatingValue IS NULL");
}
}
if (addPaging)
{
if (query.StartIndex.HasValue && query.StartIndex.Value > 0)
@ -1021,31 +1060,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
return new[] { value };
}
public IEnumerable<Guid> GetItemIdsOfType(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
CheckDisposed();
using (var cmd = _connection.CreateCommand())
{
cmd.CommandText = "select guid from TypedBaseItems where type = @type";
cmd.Parameters.Add(cmd, "@type", DbType.String).Value = type.FullName;
using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.SingleResult))
{
while (reader.Read())
{
yield return reader.GetGuid(0);
}
}
}
}
public async Task DeleteItem(Guid id, CancellationToken cancellationToken)
{
if (id == Guid.Empty)

View File

@ -1,6 +1,5 @@
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Tasks;
using MediaBrowser.Server.Implementations.Library;
using System;
using System.Collections.Generic;
@ -38,8 +37,6 @@ namespace MediaBrowser.Server.Implementations.ScheduledTasks
new StartupTrigger(),
new SystemEventTrigger{ SystemEvent = SystemEvent.WakeFromSleep},
new IntervalTrigger{ Interval = TimeSpan.FromHours(6)}
};
}

View File

@ -70,8 +70,7 @@ namespace MediaBrowser.Server.Implementations.Sync
{
return new ITaskTrigger[]
{
new IntervalTrigger { Interval = TimeSpan.FromHours(3) },
new StartupTrigger{ DelayMs = Convert.ToInt32(TimeSpan.FromMinutes(5).TotalMilliseconds)}
new IntervalTrigger { Interval = TimeSpan.FromHours(3) }
};
}

View File

@ -143,7 +143,6 @@ namespace MediaBrowser.Server.Implementations.UserViews
SpecialFolder.TvFavoriteEpisodes,
SpecialFolder.TvFavoriteSeries,
SpecialFolder.TvGenres,
SpecialFolder.TvGenre,
SpecialFolder.TvLatest,
SpecialFolder.TvNextUp,
SpecialFolder.TvResume,
@ -152,14 +151,12 @@ namespace MediaBrowser.Server.Implementations.UserViews
SpecialFolder.MovieCollections,
SpecialFolder.MovieFavorites,
SpecialFolder.MovieGenres,
SpecialFolder.MovieGenre,
SpecialFolder.MovieLatest,
SpecialFolder.MovieMovies,
SpecialFolder.MovieResume,
SpecialFolder.GameFavorites,
SpecialFolder.GameGenres,
SpecialFolder.GameGenre,
SpecialFolder.GameSystems,
SpecialFolder.LatestGames,
SpecialFolder.RecentlyPlayedGames,
@ -168,7 +165,6 @@ namespace MediaBrowser.Server.Implementations.UserViews
SpecialFolder.MusicAlbumArtists,
SpecialFolder.MusicAlbums,
SpecialFolder.MusicGenres,
SpecialFolder.MusicGenre,
SpecialFolder.MusicLatest,
SpecialFolder.MusicPlaylists,
SpecialFolder.MusicSongs,