Am Montag, 22. Januar 2007 08:32 schrieb Christian Theune:
> Hi,
>
> Am Sonntag, den 21.01.2007, 14:44 +0100 schrieb Florian Lindner:
> > Hello,
> > I am in a function that has no context and want to set a site.
> > The function is called periodically from a scheduler:
> >
> > from scheduler import loop
> >
> > def onStartup(event):
> >     refresher = loop.LoopTask(releaseRefreshEvent, interval=60)
> >
> > In this function I need to call getUtilitiesFor which needs a site set
> > but I don't know how to get a site:
> >
> > (Pdb)  from zope.app.component import hooks
> > (Pdb) from zope.app.zapi import *
> >
> > (Pdb) hooks.setSite(getSiteManager())
> > *** AttributeError: 'BaseGlobalComponents' object has no
> > attribute 'getSiteManager'
> >
> > (Pdb) hooks.setSite(getGlobalSiteManager())
> > *** AttributeError: 'BaseGlobalComponents' object has no
> > attribute 'getSiteManager'
>
> You don't set the site managers as a site, but the objects that are
> sites have site managers (or "component registries") attached to them.
>
> Also, the global registry is always "active", without settinga site.
>
> > Just getting the root object (which AFAIK always is a site) would be ok.
> > Or the object of static path.
>
> You'd have to open a connection to the database, get the root object and
> use that with setSite(). I don't have the complete spelling for that in
> my head though. If this is code that runs within Zope 3, then you might
> not want to talk to the database directly anyway because of the
> automatic transaction management.

Ok, I do it this way now:

def releaseRefreshEvent():
    """ Calls all utlities that have a INewsfeed interface registered. """
    print "releaseRefreshEvent!!"
    db = getUtility(ZODB.interfaces.IDatabase)
    conn = db.open()
    root = conn.root().data['Application']
    zope.app.component.hooks.setSite(root)
    
    utils = getUtilitiesFor(INewsfeed)
    for i in utils:
        print "Utility called:", i[0]
        i[1].sendNotification()
        
    db.close()


called every 60 seconds. This works a few times but then gives an error:

2007-01-28T18:19:01 INFO root Startup time: 16.079 sec real, 4.780 sec CPU
releaseRefreshEvent!!
releaseRefreshEvent!!
Unhandled error in Deferred:
Traceback (most recent call last):
  File "/home/florian/Zope3/src/zope/app/twisted/main.py", line 89, in main
    reactor.run()
  File "/home/florian/Zope3/src/twisted/internet/posixbase.py", line 206, in 
run
    self.mainLoop()
  File "/home/florian/Zope3/src/twisted/internet/posixbase.py", line 214, in 
mainLoop
    self.runUntilCurrent()
  File "/home/florian/Zope3/src/twisted/internet/base.py", line 541, in 
runUntilCurrent
    call.func(*call.args, **call.kw)
--- <exception caught here> ---
  File "/home/florian/Zope3/src/twisted/internet/task.py", line 84, in 
__call__
    self.f(*self.a, **self.kw)
  File "/home/florian/Desktop/zope/lib/python/CS/centershock/centershock.py", 
line 21, in releaseRefreshEvent
    root = conn.root().data['Application']
  File "/home/florian/Zope3/src/ZODB/Connection.py", line 304, in root
    return self.get(z64)
  File "/home/florian/Zope3/src/ZODB/Connection.py", line 204, in get
    p, serial = self._storage.load(oid, self._version)
  File "/home/florian/Zope3/src/ZODB/FileStorage/FileStorage.py", line 549, in 
load
    h = self._read_data_header(pos, oid)
  File "/home/florian/Zope3/src/ZODB/FileStorage/format.py", line 187, in 
_read_data_header
    self._file.seek(pos)
exceptions.ValueError: I/O operation on closed file


any idea what caused it?

Thanks,

Florian

_______________________________________________
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users

Reply via email to