Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional query to get jobs by ids #2161

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Hangfire.Core/Storage/IMonitoringApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public interface IMonitoringApi

JobList<ProcessingJobDto> ProcessingJobs(int from, int count);
JobList<ScheduledJobDto> ScheduledJobs(int from, int count);
JobList<ScheduledJobDto> ScheduledJobsByIds(long[] jobIds);
JobList<SucceededJobDto> SucceededJobs(int from, int count);
JobList<FailedJobDto> FailedJobs(int from, int count);
JobList<DeletedJobDto> DeletedJobs(int from, int count);
Expand Down
1 change: 1 addition & 0 deletions src/Hangfire.Core/Storage/JobStorageMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public abstract class JobStorageMonitor : IMonitoringApi
public abstract JobList<FetchedJobDto> FetchedJobs(string queue, int from, int perPage);
public abstract JobList<ProcessingJobDto> ProcessingJobs(int from, int count);
public abstract JobList<ScheduledJobDto> ScheduledJobs(int from, int count);
public abstract JobList<ScheduledJobDto> ScheduledJobsByIds(long[] jobIds);
public abstract JobList<SucceededJobDto> SucceededJobs(int from, int count);
public abstract JobList<FailedJobDto> FailedJobs(int from, int count);
public abstract JobList<DeletedJobDto> DeletedJobs(int from, int count);
Expand Down
42 changes: 42 additions & 0 deletions src/Hangfire.SqlServer/SqlServerMonitoringApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ public override JobList<ScheduledJobDto> ScheduledJobs(int @from, int count)
}));
}

public override JobList<ScheduledJobDto> ScheduledJobsByIds(long[] jobIds)
{
if (jobIds.Length == 0)
throw new InvalidOperationException("Sequence contains no elements");

return UseConnection(connection => GetJobsByIdsList(
connection,
jobIds,
ScheduledState.StateName,
(sqlJob, job, invocationData, loadException, stateData) => new ScheduledJobDto
{
Job = job,
LoadException = loadException,
InvocationData = invocationData,
InScheduledState = ScheduledState.StateName.Equals(sqlJob.StateName, StringComparison.OrdinalIgnoreCase),
EnqueueAt = JobHelper.DeserializeNullableDateTime(stateData["EnqueueAt"]) ?? DateTime.MinValue,
ScheduledAt = sqlJob.StateChanged,
StateData = stateData
}));
}

public override IDictionary<DateTime, long> SucceededByDatesCount()
{
return UseConnection(connection =>
Expand Down Expand Up @@ -626,6 +647,27 @@ private JobList<TDto> GetJobs<TDto>(
return DeserializeJobs(jobs, selector);
}

private JobList<TDto> GetJobsByIdsList<TDto>(
DbConnection connection,
long[] jobIds,
string stateName,
Func<SqlJob, Job, InvocationData, JobLoadException, SafeDictionary<string, string>, TDto> selector)
{
string jobsSql =
$@";select j.*, s.Reason as StateReason, s.Data as StateData, s.CreatedAt as StateChanged
from [{_storage.SchemaName}].Job j with (nolock, forceseek)
left join [{_storage.SchemaName}].State s with (nolock, forceseek) on j.StateId = s.Id and j.Id = s.JobId
where j.Id in @ids";

var jobs = connection.Query<SqlJob>(
jobsSql,
new { stateName = stateName , ids = jobIds },
commandTimeout: _storage.CommandTimeout)
.ToList();

return DeserializeJobs(jobs, selector);
}

private static JobList<TDto> DeserializeJobs<TDto>(
ICollection<SqlJob> jobs,
Func<SqlJob, Job, InvocationData, JobLoadException, SafeDictionary<string, string>, TDto> selector)
Expand Down