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

How to retrieve jobs from hangfire with a given tag id? #59

Closed
scrawny-5 opened this issue Jan 21, 2023 · 3 comments
Closed

How to retrieve jobs from hangfire with a given tag id? #59

scrawny-5 opened this issue Jan 21, 2023 · 3 comments

Comments

@scrawny-5
Copy link

Does anyone know how can I retrieve jobs from Hangfire with a given tag id after using this extension? For instance, using a Where lambda expression or similar. I need to find out the status of all jobs with a certain tag.

(apologies in advance if this is not the right forum to ask)

@erwin-faceit
Copy link
Contributor

You can try to use the same code as the monitoring pages are using?

using (var tagStorage = new TagsStorage(Storage))
{
var monitor = tagStorage.GetMonitoringApi();
if (tagsListStyle == TagsListStyle.Dropdown && tags.Length == 1)
{
relatedTags = monitor.SearchRelatedTags(tags.First()).ToList();
}
pager = new Pager(from, perPage, monitor.GetJobCount(tags, state));
if (pager.TotalPageCount == 0)
{
matchingJobs = new JobList<MatchingJobDto>(Enumerable.Empty<KeyValuePair<string, MatchingJobDto>>());
}
else
{
matchingJobs = monitor.GetMatchingJobs(tags, pager.FromRecord, pager.RecordsPerPage, state);
matchingStates = monitor.GetJobStateCount(tags);
}
}

Replace the variable Storage on line 37 with JobStorage.Current, and let me know how it goes.

@scrawny-5
Copy link
Author

Thanks for the suggestion.

I can't seem to make use of the TagsStorage class as it's marked as internal.

@erwin-faceit
Copy link
Contributor

You're absolutely right! Sorry about that. HangFire.Tags uses a mechanic where you're Razor cshtml page inherits TagsRazorPage. From there you can get an instance of the ITagsServiceStorage interface, using the property TagContext.TagsServiceStorage.

The reason that's it this closed and difficult is the fact that you can have multiple dashboards on different url's, which makes the slug important when retrieving the correct ITagsServiceStorage.

So, I've added an extension method to the latest beta version (1.9.0-beta.2) of HangFire.Tags, which also requires an upgrade to HangFire 1.8.2.

var tagstorage = JobStorage.Current.GetTagsMonitoringApi();
var matchingjobs = tagstorage.GetMatchingJobs(new[] { "tag1" }, 0, 10);

I hope this helps solving your problem. Sorry for leaving this issue open this long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants