Question

I have a scheduled job (i'm using apscheduler.scheduler lib) that needs access to the plone site object, but I don't have the context in this case. I subscribed IProcessingStart event, but unfortunately getSite() function returns None.

Also, is there a programmatic way to obtain a specific Plone Site from Zope Server root?

Additional info:

I have a job like this:

from zope.site import hooks

sched = Scheduler()

@sched.cron_schedule(day_of_week="*", hour="9", minute="0")
def myjob():
    site = hooks.getSite()
    print site
    print site.absolute_url()
    catalogtool = getToolByName(site, "portal_catalog")
    print catalogtool

The site variable is always None inside a APScheduler job. And we need informations about the site to run correctly the job.

We have avoided to execute using a public URL because an user could execute the job directly.

Was it helpful?

Solution

Build a context first with setSite(), and perhaps a request object:

from zope.app.component.hooks import setSite
from Testing.makerequest import makerequest

app = makerequest(app)
site = app[site_id]
setSite(site)

This does require that you open a ZODB connection yourself and traverse to the site object yourself.

However, it is not clear how you are accessing the Plone site from your scheduler. Instead of running a full new Zope process, consider calling a URL from your scheduling job. If you integrated APScheduler into your Zope process, you'd have to create a new ZODB connection in the job, traverse to the Plone site from the root, and use the above method to set up the site hooks (needed for a lot of local components anyway).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top