Pause the worker from getting messages from the queue

匆匆过客 提交于 2019-12-11 07:47:05

问题


I have been to trying find out if there is any way to pause the worker from taking more jobs/messages from the Rebus queue? I want to be able to "disable" a worker through my GUI so that it finishes the job its currently working on (if any) and then stops taking more jobs. Then if I want it to start taking jobs again, I signal this via the gui.

My worker setup for Rebus looks like this:

private void SetupRebus(int numberOfWorkers, string messageName)
    {
        _adapter = new BuiltinContainerAdapter();

        Configure.With(_adapter)
            .Logging(l => l.Log4Net())
            .Transport(t => t.UseSqlServer(_connectionString, _inputQueue, "error")
                .EnsureTableIsCreated())
            .Events(x => x.AfterMessage += ((bus, exception, message) => SendWorkerFinishedJob(exception, message)))
            .Events(x => x.BeforeMessage += (bus, message) => SignalWorkerStartedJob(message))
            .Behavior(x => x.SetMaxRetriesFor<Exception>(0))
            .CreateBus().Start(numberOfWorkers);

        Log.Info(string.Format("Starting to listen for messages of type {0} from queue {1} queue in {2}", messageName, _inputQueue, _connectionString));
    }

回答1:


Unfortunately, it's not readily supported by the public API - I've had a few requests for this functionality though, so you might see it exposed in a future version.

You can do this at the moment though: ((RebusBus)bus).SetNumberOfWorkers(0) (i.e. cast the IBus instance to RebusBus and change the number of worker threads), which will block until the number of workers has been adjusted to the desired number. All active workers will finish handling the messages they're working on.

This way, you can actually achieve what you're after. It's just not an official feature of Rebus (yet), but it might be in the future. I can guarantee, though, that the ability to adjust the number of workers at runtime will not go away.



来源:https://stackoverflow.com/questions/26481076/pause-the-worker-from-getting-messages-from-the-queue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!