On Wed, Oct 06, 2010 at 07:36:18PM +0200, Michael Hanselmann wrote:
> Am 6. Oktober 2010 11:58 schrieb Iustin Pop <[email protected]>:
> > --- a/lib/daemon.py
> > +++ b/lib/daemon.py
> > -def GenericMain(daemon_name, optionparser, check_fn, exec_fn,
> > +def GenericMain(daemon_name, optionparser,
> > +                check_fn, prepare_fn, exec_fn,
> 
> … if only it was a class, adding a new method would be so much simpler
> and data could be stored in instance variables.
> 
> > […]
> > -    exec_fn(options, args)
> > +    if callable(prepare_fn):
> > +      prep_results = prepare_fn(options, args)
> > +    else:
> > +      prep_results = None
> > +
> > +    exec_fn(options, args, prep_results)
> 
> I don't like this very much. Passing a callback to both check_fn and
> exec_fn that can be called when the daemon is ready (e.g. after
> listen(2)) would be better in my opinion, but you mentioned that you
> don't like it.

That won't work. I though about it, but is worse than the current patch.

The problem is that you want to call the callback as soon as the prepare
function is done. However, if you don't split the exec_fn in two, you
can't ensure that the callback is called and that error reporting works:
either you add try..except blocks to all Exec() functions (urgh!), or
you add a try..except block to GenericMain, but then you don't know
whether the exception was raised during the preparation or execution
phase, and whether Exec() has already called the callback.

So in summary: we need to split the functions anyway (and we want to, to
make a clear separation), at which point adding a callback doesn't make
sense, since we can abstract the work of the callback in GenericMain.

iustin

Reply via email to