public static void PurgeOrfanJobsExceptList(this IMonitoringApi monitor, List <string> exceptions) { var toDelete = new List <string>(); foreach (QueueWithTopEnqueuedJobsDto queue in monitor.Queues()) { for (var i = 0; i < Math.Ceiling(queue.Length / 1000d); i++) { monitor.EnqueuedJobs(queue.Name, 1000 * i, 1000) .ForEach(x => { if (!exceptions.Contains(x.Key)) { toDelete.Add(x.Key); } else { Console.WriteLine("x.Key " + x.Key + " remains."); } } ); } } foreach (var jobId in toDelete) { BackgroundJob.Delete(jobId); } }
/// <summary> /// Removes all recurring jobs from queue /// </summary> public static void DeleteAllEnqueuedJobs(IMonitoringApi monitoringApi, string queue = "default") { var enqueuedJobs = monitoringApi.EnqueuedJobs(queue, 0, Int32.MaxValue).ToList(); foreach (var enqueuedJob in enqueuedJobs) { BackgroundJob.Delete(enqueuedJob.Key); } }
// 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); } }
public static void PurgeJobs(this IMonitoringApi monitor) { var toDelete = new List <string>(); foreach (QueueWithTopEnqueuedJobsDto queue in monitor.Queues()) { for (var i = 0; i < Math.Ceiling(queue.Length / 1000d); i++) { monitor.EnqueuedJobs(queue.Name, 1000 * i, 1000) .ForEach(x => toDelete.Add(x.Key)); } } foreach (var jobId in toDelete) { BackgroundJob.Delete(jobId); } }
private HangfireJob CreateJobCache() { if (_jobCache.HasJobs) { return(_jobCache); } //else _monitor.EnqueuedJobs("default", 0, int.MaxValue).Select(x => x.Value) .ForEach(x => _jobCache.AddJob(x, HangfireJob.QUEUED)); _monitor.ScheduledJobs(0, int.MaxValue).Select(x => x.Value) .ForEach(x => _jobCache.AddJob(x, HangfireJob.QUEUED)); _monitor.FailedJobs(0, int.MaxValue).Select(x => x.Value) .ForEach(x => _jobCache.AddJob(x, HangfireJob.FAILED)); _monitor.SucceededJobs(0, int.MaxValue).Select(x => x.Value) .ForEach(x => _jobCache.AddJob(x, HangfireJob.SUCCEEDED)); _monitor.ProcessingJobs(0, int.MaxValue).Select(x => x.Value) .ForEach(x => _jobCache.AddJob(x, HangfireJob.PROCESSING)); return(_jobCache); }
/// <summary> /// check Job is enqueued or processing /// </summary> /// <param name="actionName">full action name</param> /// <returns></returns> public static bool IsRunning(IMonitoringApi api, string actionName) { var queues = api.Queues(); foreach (var queue in queues) { var queueName = queue.Name; var enqueuedCount = Convert.ToInt32(api.EnqueuedCount(queueName)); var enqueuedJobs = api.EnqueuedJobs(queueName, 0, enqueuedCount); if (enqueuedJobs.Any(job => job.Value.Job.Method.GetFullActionName() == actionName)) { return(true); } } var processingCount = Convert.ToInt32(api.ProcessingCount()); var processingJobs = api.ProcessingJobs(0, processingCount); return(processingJobs.Any(job => job.Value.Job.Method.GetFullActionName() == actionName)); }