< Summary

Information
Class: Services.HangfireService.HangfireService
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/HangfireService/HangfireService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 82
Coverable lines: 82
Total lines: 126
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 46
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%210%
GetAvailableQueues()100%210%
GetEnqueuedJobs(...)0%156120%
GetProcessingJobs(...)0%110100%
GetSucceededJobs()0%156120%
GetDeletedJobs()0%156120%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/HangfireService/HangfireService.cs

#LineLine coverage
 1using Hangfire;
 2using Hangfire.Storage;
 3using Services.HangfireService.Models;
 4
 5namespace Services.HangfireService;
 6
 7public class HangfireService : IHangfireService
 8{
 9    private readonly IMonitoringApi _monitoringApi;
 10    private const int MaxRecords = 1000; // Максимальное количество записей для выборки
 11
 012    public HangfireService()
 13    {
 014        _monitoringApi = JobStorage.Current.GetMonitoringApi();
 015    }
 16
 17    public IEnumerable<string> GetAvailableQueues()
 18    {
 019        return _monitoringApi.Queues().Select(q => q.Name);
 20    }
 21
 22    public List<JobDto> GetEnqueuedJobs(string queueName)
 23    {
 024        var enqueuedJobs = _monitoringApi.EnqueuedJobs(queueName, 0, MaxRecords);
 025        var result = new List<JobDto>();
 26
 027        foreach (var job in enqueuedJobs)
 28        {
 029            var jobId = job.Key;
 030            var stateData = JobStorage.Current.GetConnection().GetStateData(jobId);
 31
 032            if (stateData != null && stateData.Name == "Deleted")
 33                continue;
 34
 035            result.Add(new JobDto
 036            {
 037                Id = jobId,
 038                MethodName = job.Value.Job?.Method?.Name,
 039                EnqueuedAt = job.Value.EnqueuedAt,
 040                Args = job.Value.Job?.Args,
 041                State = "Enqueued"
 042            });
 43        }
 44
 045        return result
 046            .OrderByDescending(j => j.EnqueuedAt)
 047            .Take(MaxRecords)
 048            .ToList();
 49    }
 50
 51    public List<JobDto> GetProcessingJobs(string queueName)
 52    {
 053        var processingJobs = _monitoringApi.ProcessingJobs(0, MaxRecords)
 054            .Select(job => new JobDto
 055            {
 056                Id = job.Key,
 057                MethodName = job.Value.Job?.Method?.Name ?? "Unknown",
 058                StartedAt = job.Value.StartedAt,
 059                ServerId = job.Value.ServerId,
 060                Args = job.Value.Job?.Args,
 061                State = "Processing",
 062                Queue = job.Value.Job?.Queue
 063            })
 064            .OrderByDescending(j => j.StartedAt)
 065            .Take(MaxRecords)
 066            .ToList();
 67
 068        return processingJobs;
 69    }
 70
 71    public List<JobDto> GetSucceededJobs()
 72    {
 073        var failedJobs = _monitoringApi.FailedJobs(0, MaxRecords)
 074            .Select(j => new JobDto
 075            {
 076                Id = j.Key,
 077                MethodName = j.Value.Job?.Method?.Name,
 078                SucceededAt = j.Value.FailedAt,
 079                Duration = 0,
 080                Result = j.Value.ExceptionMessage,
 081                Args = j.Value.Job?.Args,
 082                State = "Failed"
 083            });
 84
 085        var succeededJobs = _monitoringApi.SucceededJobs(0, MaxRecords)
 086            .Select(j => new JobDto
 087            {
 088                Id = j.Key,
 089                MethodName = j.Value.Job?.Method?.Name,
 090                SucceededAt = j.Value.SucceededAt,
 091                Duration = j.Value.TotalDuration,
 092                Result = (string)j.Value.Result,
 093                Args = j.Value.Job?.Args,
 094                State = "Succeeded"
 095            });
 96
 097        return succeededJobs
 098            .Concat(failedJobs)
 099            .OrderByDescending(j => j.SucceededAt)
 0100            .Take(MaxRecords)
 0101            .ToList();
 102    }
 103
 104    public List<JobDto> GetDeletedJobs()
 105    {
 0106        var deletedJobs = _monitoringApi.DeletedJobs(0, MaxRecords)
 0107            .Select(job =>
 0108            {
 0109                var jobData = JobStorage.Current.GetConnection().GetJobData(job.Key);
 0110                return new JobDto
 0111                {
 0112                    Id = job.Key,
 0113                    MethodName = jobData?.Job?.Method?.Name,
 0114                    DeletedAt = job.Value.DeletedAt,
 0115                    Args = jobData?.Job?.Args,
 0116                    State = "Deleted",
 0117                    Result = job.Value.LoadException?.Message,
 0118                };
 0119            })
 0120            .OrderByDescending(j => j.DeletedAt)
 0121            .Take(MaxRecords)
 0122            .ToList();
 123
 0124        return deletedJobs;
 125    }
 126}