rebus-org / Rebus

:bus: Simple and lean service bus implementation for .NET
https://mookid.dk/category/rebus
Other
2.27k stars 354 forks source link

How can I log which worker is processing which job? #879

Closed pulkitjainn closed 4 years ago

mookid8000 commented 4 years ago

Rebus defaults to name its workers on the form RebusBus <n> worker <m>, where n a number assigned to the bus when it starts, and m is the worker's number.

When the worker is created, its thread's name will be assigned the name of the worker, so you can access it like this:

var name = Thread.CurrentThread.Name;

// woohoo, I have the name now

Many logging frameworks offer the ability to output the thread name as part of the standard format output. E.g. with NLog, you simply put ${threadname} in the format string.

But please note that Rebus' workers are actually only used to execute the first part of receiving each message – after the first await that does something, which is async, the rest of the message handling is going to happen on the thread pool.

pulkitjainn commented 4 years ago

Thanks for such a quick response. But I want the name of the worker. Suppose I have 3 jobs to perform and I set 2 workers, then I want to know which task is being performed by which worker?

mookid8000 commented 4 years ago

I don't think I understand your question then.... what are "jobs", are they messages? And what do you actually mean by "workers"?

pulkitjainn commented 4 years ago

By jobs, I mean messages.

As of my understanding when I do something like this :
o.SetNumberOfWorkers(2); o.SetMaxParallelism(2);

I'll be able to two processes two messages parallelly at a time.

Now say I have 3 messages. Worker 1 is processing message 1, worker 2 is processing message 2 currently. Now when either of the workers will get free, message 3 will get processed. Here I want to log or know which worker would be processing message 3. I hope I am able to explain to you my query.

mookid8000 commented 4 years ago

Please read the wiki page about workers and parallelism 😄

It explains nicely what the terms "number of workers" and "max parallelism" mean with Rebus.

Spoiler alert: Your perception that Rebus' workers end up handling your messages is slightly imprecise, because it's only a rare special case that they end up doing that. In the general case, you don't know which thread will end up executing your message handlers, and you shouldn't care.

Unless of course you care.... which it seems like you so.... why do you care?

pulkitjainn commented 4 years ago

Okay, thank you so much!