[Grok-dev] where to put the code that can be run automatically when groks are started up?

Jan-Wijbrand Kolman janwijbrand at gmail.com
Wed Apr 29 03:26:23 EDT 2009


Hi,

TonyXu wrote:
> Thank you for sharing your knowledge. 
> It's so complicated that I need more time to understand it and I still don't 
> know how I can implement my purpose.
> I think you might be able to help me.
> The scenario is that I have written a scheduler thread, and I want the
> program
> can read the configuration from ZODB to initialize the scheduler thread, and
> start
> the thread right after the Grok applications are started up.

I *think* it is quite a bit simpler than you might think. As far as I
know an event is send out when starting Zope - you can easily subscribe
to this event.

I investigated a bit and I think this would work:

1)

  from zope.app.appsetup import IDatabaseOpenedEvent
  from zope.app.appsetup.bootstrap import getInformationFromEvent

  @grok.subscribe(IDatabaseOpenedEvent)
  def process_started(event):
      db, connection, root, root_folder = getInformationFromEvent(event)
      print 'DDD', event, db

2)

  from zope.app.appsetup import IProcessStartingEvent

  @grok.subscribe(IProcessStartingEvent)
  def process_started(event):
      print 'EEE', event # Do your stuff


There's a small issue with the second event subscriber; it is only fired
in projects build with the grokproject tool *before* it started using
paster. I think this actually is a bug in Zope. If it is I'll issue a
bug report for that.

I hope this helps.

regards,
jw



More information about the Grok-dev mailing list