mozilla-services/circus — pull request #239 by j4mie (2012-07-29 00:17:31+00:00)
Title: Worker hooks

OK, this pull request contains initial work on the new features discussed in #237 for review.

This is far from finished, but I'd appreciate any comments.

The basic idea is: in the config file, you can define Python callables that "hook in" to the lifecycle of a worker. The available hooks are `before_start`, `after_start`, `before_stop`, `after_stop`. Eg:

```cfg
[watcher:queue-worker]
cmd = python
args = -u worker.py
hooks.before_start = mycoolapp.hooks.check_redis
```

A hook callable (currently) looks like this:

```python
def check_redis(watcher, action):
"""Check that Redis is running"""
```

Questions:

* Does this look relatively sane?
* Am I taking the right approach to implementing it?
* Should the hook functions also take the arbiter instance as an argument (as in @tarekziade's example in #237)? If so, how will this work? (watchers don't seem to have a reference back to the arbiter that created them)
* What happens when exceptions are raised (or `False` is returned) inside each type of hook?
* Do the hooks need to explicitly return `True` to allow execution to continue?
* Does this feature, plus priorities, cover the use cases discussed in #237? Or do we need the more complex "dependencies" feature as well?

Obvious todos:

* More tests
* More documentation
* Handle raising exceptions/returning `False` inside hooks