mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
feat(archival): ✨ in DEBUG, purge existing queue on startup
ref https://github.com/HangfireIO/Hangfire/issues/394#issuecomment-179924221
This commit is contained in:
parent
e1adced026
commit
5a2cf4caf1
2 changed files with 31 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
|||
using FilterLists.Archival.Application.Commands;
|
||||
using FilterLists.Archival.Infrastructure;
|
||||
using FilterLists.Archival.Infrastructure.Scheduling;
|
||||
using Hangfire;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
|
@ -31,6 +32,7 @@ public static void UseApplication(this IApplicationBuilder app)
|
|||
private static void ScheduleArchival()
|
||||
{
|
||||
#if DEBUG
|
||||
JobStorage.Current?.GetMonitoringApi()?.PurgeJobs();
|
||||
new EnqueueArchiveAllLists.Command().EnqueueBackgroundJob();
|
||||
#else
|
||||
new EnqueueArchiveAllLists.Command().AddOrUpdateRecurringJob(Hangfire.Cron.Daily);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Hangfire;
|
||||
using Hangfire.Storage;
|
||||
|
||||
namespace FilterLists.Archival.Infrastructure.Scheduling
|
||||
{
|
||||
public static class HangfireExtensions
|
||||
{
|
||||
// https://github.com/HangfireIO/Hangfire/issues/394#issuecomment-179924221
|
||||
public static void PurgeJobs(this IMonitoringApi monitoringApi)
|
||||
{
|
||||
var toDelete = new List<string>();
|
||||
foreach (var queue in monitoringApi.Queues())
|
||||
{
|
||||
for (var i = 0; i < Math.Ceiling(queue.Length / 1000d); i++)
|
||||
{
|
||||
toDelete.AddRange(monitoringApi.EnqueuedJobs(queue.Name, 1000 * i, 1000).Select(x => x.Key));
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var jobId in toDelete)
|
||||
{
|
||||
BackgroundJob.Delete(jobId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue