Hangfire icon indicating copy to clipboard operation
Hangfire copied to clipboard

The queue name of the job triggered by requeue button in the dashboard is always default

Open JasonLiuLab opened this issue 5 years ago • 5 comments

Hi guys,

I found the queue name of the job tiggered by requeue button in the dashboard is always default.

image

image

Because after receiving the requeue request, it always uses the default no-argument constructor to create a new EnqueuedState

https://github.com/HangfireIO/Hangfire/blob/88504456704b6fa590a25a694f1e877b922a9da1/src/Hangfire.Core/Dashboard/DashboardRoutes.cs#L122-L125

https://github.com/HangfireIO/Hangfire/blob/88504456704b6fa590a25a694f1e877b922a9da1/src/Hangfire.Core/Dashboard/DashboardRoutes.cs#L192-L196

https://github.com/HangfireIO/Hangfire/blob/88504456704b6fa590a25a694f1e877b922a9da1/src/Hangfire.Core/States/EnqueuedState.cs#L89-L92

So I had to create an attribute and apply it to each method of jobs to solve this issue?

    internal class RequeueJobIssueFixAttribute : JobFilterAttribute, IApplyStateFilter
    {
        public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
        {
            var status = context.NewState as EnqueuedState;
            if (status == null)
            {
                return;
            }

            if (status.Queue != EnqueuedState.DefaultQueue)
            {
                return;
            }

            var jobDetails = context.Storage.GetMonitoringApi().JobDetails(context.BackgroundJob.Id);
            var queueName = jobDetails.History.FirstOrDefault(d => d.StateName == EnqueuedState.StateName)?.Data["Queue"];
            if (!string.IsNullOrEmpty(queueName))
            {
                status.Queue = queueName;
            }
        }

        public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
        {
            return;
        }
    }

Hope this helps, Will you guys fix it at next release? So we needn't to use this attribute in every method.

Thanks

JasonLiuLab avatar Jul 13 '21 11:07 JasonLiuLab

You can register that attribute globally using GlobalJobFilters. It will be applied to all the jobs automatically. See this article for more details: https://earwicker.com/Hangfire_A_Tale_of_Several_Queues.html

I'm not sure if that behavior is going to be changed in future releases.

Frazi1 avatar Jul 13 '21 12:07 Frazi1

Hi Frazi1, thanks for your response and the article is helpful, then requeue to default queue is this a bug or is it designed in this way?

JasonLiuLab avatar Jul 13 '21 15:07 JasonLiuLab

I believe there is a conversation with @odinserj somewhere either in GitHub issues or in https://discuss.hangfire.io/ where he confirmed it to be the expected behavior.

Also, checkout https://github.com/HangfireIO/Hangfire/releases/tag/v1.8.0-beta3 release. It mentions the following change:

Added – Introduce the Job.Queue property, so jobs now can have their own queue specified.

With that new feature, Hangfire's behavior may be changed. But I haven't tested it myself.

Frazi1 avatar Jul 13 '21 16:07 Frazi1

got it, thank you.

JasonLiuLab avatar Jul 14 '21 05:07 JasonLiuLab

I get the same problem now. the code here, create a new State instance instead of the original State when reschedule job. Is there any reason to discard the original State ?

v2codes avatar Dec 02 '21 14:12 v2codes