Post Run Hook Not Executing in Cement 2.7.x #328

Closed
fxstein opened this Issue Sep 10, 2015 · 6 comments

Comments

Projects
None yet
2 participants
@fxstein
Contributor

fxstein commented Sep 10, 2015

Ran into a strange situation when defining a custom base controller.

Running the app:

with PubKeyApp() as app:
    app.run()

    try:
        app.run_forever() # custom function added in my class 
    except (KeyboardInterrupt, SystemExit, CaughtSignal):
        app.log.info('Shutting down pubkey REST server')

Simple base controller:

class PubKeyBaseController(CementBaseController):
    class Meta:
        label = 'base'
        description = "pubkey - Public Key distribution made easy"

        arguments = [
            (['-a', '--auto'],
             dict(action='store_true', help='auto detect IP address')),
            ]

Part of the App class:

class PubKeyApp(CementApp):
    class Meta:
        label = 'pubkey'
        config_defaults = defaults

        base_controller = 'base'
        config_handler = 'config_handler'
        handlers = [PubKeyBaseController, PubKeyConfigHandler]

        arguments_override_config = True

    def setup(self):
        # always run core setup first
        super(PubKeyApp, self).setup()

        self.log.debug('running setup()')

        self._loop = asyncio.get_event_loop()

        hook.register('post_run', self._post_run)
        hook.register('pre_close', self._pre_close)

    def _post_run(self, app):
        self.log.debug('running _post_run()')
        self._loop.run_until_complete(self._init(self._loop))

    def _pre_close(self, app):
        self.log.debug('running _pre_close()')
        self._loop.run_until_complete(self._finish())

    @asyncio.coroutine
    def _init(self, loop):
        self.log.debug('running _init()')
...

On 2.6.x here is a sample output of a simple app that registers multiple hooks - one of them a post_run hook and executes the hook as expected.

...
2015-09-09 15:58:52,937 (DEBUG) cement.ext.ext_plugin : plugin config dir /etc/pubkey/plugins.d does not exist.
2015-09-09 15:58:52,937 (DEBUG) cement.ext.ext_plugin : plugin config dir /Users/xxxxxxx/.pubkey/plugins.d does not exist.
2015-09-09 15:58:52,937 (DEBUG) cement.core.foundation : setting up pubkey.arg handler
2015-09-09 15:58:52,938 (DEBUG) cement.core.foundation : setting up pubkey.output handler
2015-09-09 15:58:52,938 (DEBUG) cement.core.foundation : setting up application controllers
2015-09-09 15:58:52,938 (DEBUG) cement.core.handler : merging config defaults from '<__main__.PubKeyBaseController object at 0x101de7f98>' into section 'controller.base'
2015-09-09 15:58:52,938 (DEBUG) cement.core.hook : running hook 'post_setup' (<function add_handler_override_options at 0x101d586a8>) from cement.core.foundation
2015-09-09 15:58:52,938 (DEBUG) pubkey : running setup()
2015-09-09 15:58:52,939 (DEBUG) cement.core.hook : registering hook '_post_run' from __main__ into hooks['post_run']
2015-09-09 15:58:52,939 (DEBUG) cement.core.hook : registering hook '_pre_close' from __main__ into hooks['pre_close']
2015-09-09 15:58:52,939 (DEBUG) pubkey : collecting arguments/commands for <__main__.PubKeyBaseController object at 0x101de7f98>
2015-09-09 15:58:52,940 (DEBUG) cement.core.hook : running hook 'post_argument_parsing' (<function handler_override at 0x101d9aa60>) from cement.core.foundation
2015-09-09 15:58:52,940 (DEBUG) cement.core.hook : running hook 'post_run' (<bound method PubKeyApp._post_run of <__main__.PubKeyApp object at 0x101db73c8>>) from __main__
2015-09-09 15:58:52,940 (DEBUG) pubkey : running _post_run()
2015-09-09 15:58:52,941 (DEBUG) pubkey : running _init()
2015-09-09 15:58:52,943 (INFO) pubkey : pubkey file used: /Users/xxxxx/.ssh/id_rsa.pub
2015-09-09 15:58:52,943 (INFO) pubkey : pubkey:

2015-09-09 15:58:52,948 (INFO) pubkey : pubkey server started at http://localhost:1080/
2015-09-09 15:58:52,948 (DEBUG) pubkey : run_forever()
^C2015-09-09 15:58:56,612 (DEBUG) cement.core.foundation : Caught signal 2
2015-09-09 15:58:56,612 (INFO) pubkey : Shutting down pubkey REST server
2015-09-09 15:58:56,613 (DEBUG) cement.core.hook : running hook 'pre_close' (<bound method PubKeyApp._pre_close of <__main__.PubKeyApp object at 0x101db73c8>>) from __main__
2015-09-09 15:58:56,613 (DEBUG) pubkey : running _pre_close()
2015-09-09 15:58:56,613 (DEBUG) pubkey : running _finish()
2015-09-09 15:58:56,716 (DEBUG) cement.core.foundation : closing the application

On the latest 2.7.x installed from github repo the same program without changes still registers the post_run hook but it never gets called (should come right after post_argument_parsing) - hence missing a significant portion of this particular app.

....
2015-09-09 16:39:08,780 (DEBUG) cement.ext.ext_plugin : plugin config dir /etc/pubkey/plugins.d does not exist.
2015-09-09 16:39:08,780 (DEBUG) cement.ext.ext_plugin : plugin config dir /Users/xxxxxxx/.pubkey/plugins.d does not exist.
2015-09-09 16:39:08,780 (DEBUG) cement.core.foundation : setting up pubkey.arg handler
2015-09-09 16:39:08,781 (DEBUG) cement.core.foundation : setting up pubkey.output handler
2015-09-09 16:39:08,781 (DEBUG) cement.core.foundation : setting up application controllers
2015-09-09 16:39:08,781 (DEBUG) cement.core.handler : merging config defaults from '<__main__.PubKeyBaseController object at 0x105b3f748>' into section 'controller.base'
2015-09-09 16:39:08,781 (DEBUG) cement.core.hook : running hook 'post_setup' (<function add_handler_override_options at 0x105a9d510>) from cement.core.foundation
2015-09-09 16:39:08,781 (DEBUG) pubkey : running setup()
2015-09-09 16:39:08,782 (DEBUG) cement.core.hook : registering hook '_post_run' from __main__ into hooks['post_run']
2015-09-09 16:39:08,782 (DEBUG) cement.core.hook : registering hook '_pre_close' from __main__ into hooks['pre_close']
2015-09-09 16:39:08,782 (DEBUG) cement.core.controller : collecting arguments/commands for <__main__.PubKeyBaseController object at 0x105b3f748>
2015-09-09 16:39:08,783 (DEBUG) cement.core.hook : running hook 'post_argument_parsing' (<function handler_override at 0x105ae0730>) from cement.core.foundation
2015-09-09 16:39:08,783 (DEBUG) pubkey : run_forever()
^C2015-09-09 16:39:13,594 (DEBUG) cement.core.foundation : Caught signal 2
2015-09-09 16:39:13,594 (INFO) pubkey : Shutting down pubkey REST server
2015-09-09 16:39:13,595 (DEBUG) cement.core.hook : running hook 'pre_close' (<bound method PubKeyApp._pre_close of <__main__.PubKeyApp object at 0x105afc4e0>>) from __main__
2015-09-09 16:39:13,595 (DEBUG) pubkey : running _pre_close()
2015-09-09 16:39:13,595 (DEBUG) pubkey : running _finish()
2015-09-09 16:39:13,701 (DEBUG) cement.core.foundation : closing the application

Interestingly other hooks like _pre_close() execute on both versions as expected.

Without the custom base controller the post_run hook gets executed on both 2.6.x as well as 2.7.x. If I comment out the base controller on 2.7.x the post_run hook executes as expected.

Hope this helps!

@derks

This comment has been minimized.

Show comment
Hide comment
@derks

derks Sep 10, 2015

Member

Thanks @fxstein for the detailed report. I'll look at this as soon as possible.

Member

derks commented Sep 10, 2015

Thanks @fxstein for the detailed report. I'll look at this as soon as possible.

@fxstein

This comment has been minimized.

Show comment
Hide comment
@fxstein

fxstein Sep 10, 2015

Contributor

You are very welcome! Getting to know cement in small increments but am already a fan!

Contributor

fxstein commented Sep 10, 2015

You are very welcome! Getting to know cement in small increments but am already a fan!

@derks derks added this to the 2.8.0 Stable milestone Oct 5, 2015

@derks derks self-assigned this Oct 5, 2015

@derks derks changed the title from Potential Bug in 2.7.x: post_run hook not being executed with custom base controller to Post Run Hook Not Executing in Cement 2.7.x Oct 5, 2015

@derks

This comment has been minimized.

Show comment
Hide comment
@derks

derks Oct 5, 2015

Member

Verified.

Member

derks commented Oct 5, 2015

Verified.

derks added a commit that referenced this issue Oct 5, 2015

@derks

This comment has been minimized.

Show comment
Hide comment
@derks

derks Oct 5, 2015

Member

Should be fixed, but please test against current git/master and verify for me.

Member

derks commented Oct 5, 2015

Should be fixed, but please test against current git/master and verify for me.

@derks

This comment has been minimized.

Show comment
Hide comment
@derks

derks Oct 14, 2015

Member

@fxstein any word on this? Just want to make sure it fixed your issues. If I don't hear anything I'll close out... but keeping open for you.

Member

derks commented Oct 14, 2015

@fxstein any word on this? Just want to make sure it fixed your issues. If I don't hear anything I'll close out... but keeping open for you.

@fxstein

This comment has been minimized.

Show comment
Hide comment
@fxstein

fxstein Oct 15, 2015

Contributor

@derks Confirmed this is now working for me. Thank you!

Contributor

fxstein commented Oct 15, 2015

@derks Confirmed this is now working for me. Thank you!

@derks derks closed this Oct 15, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment