fedora-infra/fedmsg

unable to configure topic in consumer

Opened this issue · 2 comments

i created myself a consumer using @ralphbean gist: https://gist.github.com/ralphbean/3766683

it has topic hardcoded:

    # I'm only interested in messages from FAS
    topic = "org.fedoraproject.stg.fas*"

but i wanted it to be configurable.

they only way i'm sure method is called is by defining __init__ and assign value there:

class MyTestConsumer(fedmsg.consumers.FedmsgConsumer):

    def __init__(self, hub):
        super(fedmsg.consumers.FedmsgConsumer, self).__init__(hub)
        topic = self.hub.config.get('my_consumer_topic')

however, this emits error, like half of the __init__ is not called:

Traceback (most recent call last):
  File "/usr/share/python2.7/site-packages/moksha/hub/api/consumer.py", line 198, in _work
  File "/usr/share/python2.7/site-packages/fedmsg/consumers/__init__.py", line 271, in post_consume
  File "/usr/share/python2.7/site-packages/fedmsg/consumers/__init__.py", line 275, in save_status
AttributeError: 'CVS2JiraConsumer' object has no attribute 'status_filename'

so like fedmsg/consumers/__init__.py:L114 never called.

and if i look fedmsg/consumers/__init__.py:97-98 seems that __init__ is invoking itself? i'm no Python guru, what's the trick there.

anywho, perhaps add support for calling consumer init, so could do extra initiaization required by consumer?

class MyTestConsumer(fedmsg.consumers.FedmsgConsumer):
    config_key = "cvs2jira_consumer_enabled"

    def configure(self):
        topic = self.hub.config.get('my_consumer_topic')
        pass

and if i look fedmsg/consumers/init.py:97-98 seems that init is invoking itself? i'm no Python guru, what's the trick there.

The call to super gets the parent class (in this case it's just moksha.hub.api.consumer.Consumer) and calls its __init__ method. I think your problem is in

class MyTestConsumer(fedmsg.consumers.FedmsgConsumer):

    def __init__(self, hub):
        super(fedmsg.consumers.FedmsgConsumer, self).__init__(hub)
        topic = self.hub.config.get('my_consumer_topic')

Try replacing super(fedmsg.consumers.FedmsgConsumer, self).__init__(hub) with super(MyTestConsumer, self).__init__(hub). In your current snippet you're calling the parent of FedmsgConsumer rather than the parent of your class (FedmsgConsumer).

yeah, i invoked parent constructor wrong way, but nevertheless, i think the separate configure method would be useful, in the related PR. more cleaner code and api.