Skip to content

Commit

Permalink
Add additional way to get scheduled jobs by ids
Browse files Browse the repository at this point in the history
Change parameter type, introduce exception for 0 length

Simplify get by id function

Remove gap
  • Loading branch information
Aistis Olendra committed Feb 3, 2023
1 parent e9d018c commit a1ad060
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
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

1 comment on commit a1ad060

@burningice2866
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auch, thats gonna break all 3rdparty storage providers

Please sign in to comment.