From 3fe89a2f1c1541d552b60ba7c7a90853aa9c675c Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Sat, 1 Sep 2018 12:43:52 -0500 Subject: [PATCH] prioritize retrying failed snaps --- .../Snapshot/SnapshotService.cs | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/FilterLists.Services/Snapshot/SnapshotService.cs b/src/FilterLists.Services/Snapshot/SnapshotService.cs index 539b1f971..e099e3902 100644 --- a/src/FilterLists.Services/Snapshot/SnapshotService.cs +++ b/src/FilterLists.Services/Snapshot/SnapshotService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Threading.Tasks; using AutoMapper; using AutoMapper.QueryableExtensions; @@ -13,6 +14,26 @@ namespace FilterLists.Services.Snapshot { public class SnapshotService : Service { + private readonly Expression> ifLastSnapFailed = + l => l.Snapshots + .OrderByDescending(s => s.CreatedDateUtc) + .FirstOrDefault() + .WasUpdated && + !l.Snapshots + .OrderByDescending(s => s.CreatedDateUtc) + .FirstOrDefault() + .WasSuccessful; + + private readonly Expression> isNotWaybackViewUrlWithSuccessfulSnap = + l => !(l.ViewUrl.StartsWith(WaybackService.WaybackMachineUrlPrefix) && + l.Snapshots.Any(s => s.WasSuccessful)); + + private readonly Expression> lastSnapTimestamp = + l => l.Snapshots + .Select(s => s.CreatedDateUtc) + .OrderByDescending(d => d) + .FirstOrDefault(); + private string uaString; public SnapshotService(FilterListsDbContext dbContext, IConfigurationProvider mapConfig) @@ -39,13 +60,10 @@ private async Task CleanupFailedSnapshots() private async Task> GetListsToCapture(int batchSize) => await DbContext .FilterLists - .Where(l => !(l.ViewUrl.StartsWith(WaybackService.WaybackMachineUrlPrefix) && - l.Snapshots.Any(s => s.WasSuccessful))) + .Where(isNotWaybackViewUrlWithSuccessfulSnap) .OrderBy(l => l.Snapshots.Any()) - .ThenBy(l => l.Snapshots - .Select(s => s.CreatedDateUtc) - .OrderByDescending(d => d) - .FirstOrDefault()) + .ThenByDescending(ifLastSnapFailed) + .ThenBy(lastSnapTimestamp) .Take(batchSize) .ProjectTo(MapConfig) .ToListAsync(); @@ -60,8 +78,7 @@ private async Task> CreateAndSaveSnaps(IEnumerable CreateSnaps(IEnumerable lists) where TSnap : Snapshot, new() => - lists.Select(l => - Activator.CreateInstance(typeof(TSnap), DbContext, l, uaString) as TSnap); + lists.Select(l => Activator.CreateInstance(typeof(TSnap), DbContext, l, uaString) as TSnap); private static async Task SaveSnaps(IEnumerable snaps) {