问题
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