Not calling UnsubscribeAction delegate but gracefully exiting TopShelf

71 views
Skip to first unread message

Mike Bridge

unread,
May 14, 2015, 6:10:00 PM5/14/15
to masstrans...@googlegroups.com
To keep some of my consumers' subscriptions active when I exit my TopShelf console app, I'm not calling UnsubscribeAction on the buses.  This works fine, except that my console doesn't exit cleanly when I hit "Ctrl-C"---it just hangs.  I call ".Dispose()" on all the buses, but that doesn't seem to make a difference.  I have to kill the whole shell.

Is there a way to keep these registered in RabbitMQ but also shut down cleanly?

Travis Smith

unread,
May 14, 2015, 7:27:05 PM5/14/15
to masstrans...@googlegroups.com
The subscription configuration should accept a "Permanent()" option. I don't have VS open in front of me, so if you don't find I can take a look a bit later and point you to the right place to enable it. 

-Travis

On Thu, May 14, 2015 at 6:10 PM, Mike Bridge <mi...@mailoutinteractive.com> wrote:
To keep some of my consumers' subscriptions active when I exit my TopShelf console app, I'm not calling UnsubscribeAction on the buses.  This works fine, except that my console doesn't exit cleanly when I hit "Ctrl-C"---it just hangs.  I call ".Dispose()" on all the buses, but that doesn't seem to make a difference.  I have to kill the whole shell.

Is there a way to keep these registered in RabbitMQ but also shut down cleanly?

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/5603db5a-1666-404b-8409-ffc46cda0575%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike Bridge

unread,
May 15, 2015, 12:29:40 PM5/15/15
to masstrans...@googlegroups.com
Thanks---it could be that the documentation is just out of sync.   In the http://masstransit.readthedocs.org/en/latest/overview/subscriptions.html it says "if you are using a permanent subscription and don’t want unsubscribe don’t call the delegate."  However, in http://masstransit.readthedocs.org/en/latest/configuration/sub_config_api.html it says "If this is to be a permanent subscription, it needs to be established during configuration."

Anyway, I added a delegate to call sbc => sbc.Subscribe(s=> s.Consumer<T>().Permanent()) but I don't see a way to get the unsubscribe token if I do it this way (and it it hangs if I've used the consumer):

    var serviceBus = ServiceBusFactory.New(sbc =>
    {
                
        sbc.UseRabbitMq(
               // ...
        );
        sbc.ReceiveFrom(_baseUrl + queueName);
        sbc => sbc.Subscribe(s=> s.Consumer<T>().Permanent()) 

    }

It possible to get an unsubscribe token if I set up the Subscribe in a delegate inside the configuration?  I have been subscribing using the SubscribeConsumer call instead:

            var unsubscribeAction = serviceBus.SubscribeConsumer<T>();

Thanks,

-Mike

Mike Bridge

unread,
May 20, 2015, 11:33:33 AM5/20/15
to masstrans...@googlegroups.com
I'm still unclear on how I should create a subscription as either permanent or temporary, and clean up either type of subscription properly---I can't see a way to do it from the documentation.  Using .Permanent() as described below still causes my console to hang on termination.

-Mike

Travis Smith

unread,
May 20, 2015, 11:42:53 AM5/20/15
to masstrans...@googlegroups.com
Do you have long running consumers when Dispose() is called on your Bus instance? The bus will wait for them to finish before cleaning up. 

-Travis

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.

Mike Bridge

unread,
May 20, 2015, 11:59:04 AM5/20/15
to masstrans...@googlegroups.com
The call to .Consume() completes before I try to terminate the app.  I'll see if I can write a minimal demo app to reproduce the issue.

-Mike

--
You received this message because you are subscribed to a topic in the Google Groups "masstransit-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/masstransit-discuss/LsF15P3DDJ8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to masstransit-dis...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

Chris Patterson

unread,
May 20, 2015, 11:44:44 PM5/20/15
to masstrans...@googlegroups.com
There are dozens of samples that exit cleanly, many using Topshelf. You must call .Dispose() on the IServiceBus interface for every bus in the process, otherwise it will not exit until all threads are completed.


Mike Bridge

unread,
May 21, 2015, 3:04:24 PM5/21/15
to masstrans...@googlegroups.com
Thanks!  I finally traced this down to one bus that I had not disposed of properly.

It would have been clearer if this were part of the documentation---the only mention of the necessity of Dispose this is buried in the tutorial: http://masstransit.readthedocs.org/en/latest/search.html?q=Dispose

The documentation on subscriptions could also use some attention.  Creating a Permanent/Transient subscription is described in two different places and it is wasn't clear to me that they are two unrelated ways of doing the same thing:


I went down the wrong rabbit hole fiddling with combinations of ServiceBusConfigurators and UnsubscribeTokens.  Unfortunately, the "Transient" option isn't mentioned anywhere---I only discovered it by accident in IntelliSense after I realized that the Permanent() option did nothing (it's the default)---I spent a lot of time thinking that omitting it was supposed to create a transient subscription.

If it helps someone else, here's a more concise description of what I missed from the documentation:

- When terminating a subscription has been created on a pre-existing bus, you *may* continue to queue messages on the message queue by not calling the UnsubscribeToken that was generated during Subscription.  If you would rather that all messages be discarded while the consumer is inactive, then you should call the UnsubscribeToken.
e.g.:
                 var myServiceBus = ServiceBusFactory.New(sbc =>
            { 
                // ...
            }
            var unsubscribeToken = myServiceBus.Subscribe<T>();
         
            // later:
            unsubscribeToken(); // optional 
            myServiceBus.Dispose(); // mandatory


- When a subscription is created at the same time as the bus, you may configure the subscription to be temporary or permanent using a ServiceBusConfigurator (no UnsubscribeToken is necessary).

            var myServiceBus = ServiceBusFactory.New(sbc =>
            { 
                 s => s.Consumer<MyConsumer>().Permanent(); // Message Queue keeps listening after the service bus is terminated. Permanent is the default, and therefore optional.
            }
            // OR
            var myServiceBus = ServiceBusFactory.New(sbc =>
            { 
                 s => s.Consumer<MyConsumer>().Transient(); // Message Queue stops listening after the service bus is terminated
            }

            // later
            myServiceBus.Dispose(): // mandatory
            // No UnsubscribeToken call is necessary

- Lastly, always call the Dispose() method on the bus when you're done.

Cheers!

-Mike

-Travis

To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

-- 

Dru Sellers

unread,
May 22, 2015, 5:12:17 PM5/22/15
to masstrans...@googlegroups.com
The docs are all in the repository, since you have most recently felt this pain, I'd invite you to submit a pull request with these words of advice. :)

-d


-Travis

To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.

-- 

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-dis...@googlegroups.com.

To post to this group, send email to masstrans...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages