/
service.py
173 lines (148 loc) · 6.16 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import threading
import Zope2
from zope.component import getUtility
from zope.interface import implements
from zope.event import notify
from zope.app.component.hooks import setSite
from zExceptions import BadRequest
from AccessControl.SecurityManagement import noSecurityManager,\
newSecurityManager, getSecurityManager
from AccessControl.User import SpecialUser
from Products.CMFCore.interfaces import ISiteRoot
from zc.async.interfaces import KEY
from zc.async.job import serial, parallel, Job
from plone.app.async.interfaces import IAsyncDatabase, IAsyncService
from plone.app.async.interfaces import JobSuccess, JobFailure
def makeJob(func, context, *args, **kwargs):
"""Return a job_info tuple."""
return (func, context, args, kwargs)
def _getAuthenticatedUser():
"""Get user info."""
user = getSecurityManager().getUser()
if isinstance(user, SpecialUser):
return (), None
acl_users = user.aq_parent
return acl_users.getPhysicalPath(), user.getId()
def _executeAsUser(context_path, portal_path, uf_path, user_id, func, *args,
**kwargs):
"""Reconstruct environment and execute func."""
transaction = Zope2.zpublisher_transactions_manager # Supports isDoomed
transaction.begin()
app = Zope2.app()
result = None
try:
try:
portal = app.unrestrictedTraverse(portal_path, None)
if portal is None:
raise BadRequest(
'Portal path %s not found' % '/'.join(portal_path))
setSite(portal)
if uf_path:
acl_users = app.unrestrictedTraverse(uf_path, None)
if acl_users is None:
raise BadRequest(
'Userfolder path %s not found' % '/'.join(uf_path))
user = acl_users.getUserById(user_id)
if user is None:
raise BadRequest('User %s not found' % user_id)
newSecurityManager(None, user)
context = portal.unrestrictedTraverse(context_path, None)
if context is None:
raise BadRequest(
'Context path %s not found' % '/'.join(context_path))
result = func(context, *args, **kwargs)
transaction.commit()
except:
transaction.abort()
raise
finally:
noSecurityManager()
setSite(None)
app._p_jar.close()
return result
def job_success_callback(result):
"""Fire event on job success."""
notify(JobSuccess(result))
def job_failure_callback(result):
"""Fire event on job failure."""
notify(JobFailure(result))
class AsyncService(threading.local):
"""Utility providing async execution services to Plone.
"""
implements(IAsyncService)
def __init__(self):
self._db = None
self._conn = None
def getQueues(self):
"""Return the queues container."""
db = getUtility(IAsyncDatabase)
if self._db is not db:
self._db = db
conn = getUtility(ISiteRoot)._p_jar
self._conn = conn.get_connection(db.database_name)
self._conn.onCloseCallback(self.__init__)
return self._conn.root()[KEY]
def queueJobInQueue(self, queue, quota_names, func, context, *args,
**kwargs):
"""Queue a job in the specified queue."""
portal = getUtility(ISiteRoot)
portal_path = portal.getPhysicalPath()
context_path = context.getPhysicalPath()
uf_path, user_id = _getAuthenticatedUser()
job = Job(_executeAsUser, context_path, portal_path, uf_path, user_id,
func, *args, **kwargs)
if quota_names:
job.quota_names = quota_names
job = queue.put(job)
job.addCallbacks(success=job_success_callback,
failure=job_failure_callback)
return job
def queueJob(self, func, context, *args, **kwargs):
"""Queue a job in the default queue."""
queue = self.getQueues()['']
return self.queueJobInQueue(queue, ('default',), func, context, *args,
**kwargs)
def queueJobWithDelay(self, begin_by, begin_after, func, context, *args,
**kwargs):
queue = self.getQueues()['']
jobs = [(func, context, args, kwargs)]
return self._queueJobsInQueue(queue, ('default',), jobs, True,
begin_by, begin_after)
def _queueJobsInQueue(self, queue, quota_names, job_infos, serialize=True,
begin_by=None, begin_after=None):
"""Queue multiple jobs in the specified queue."""
portal = getUtility(ISiteRoot)
portal_path = portal.getPhysicalPath()
uf_path, user_id = _getAuthenticatedUser()
scheduled = []
for (func, context, args, kwargs) in job_infos:
context_path = context.getPhysicalPath()
job = Job(_executeAsUser, context_path, portal_path, uf_path,
user_id, func, *args, **kwargs)
scheduled.append(job)
if serialize:
job = serial(*scheduled)
else:
job = parallel(*scheduled)
if quota_names:
job.quota_names = quota_names
job = queue.put(job, begin_by=begin_by, begin_after=begin_after)
job.addCallbacks(success=job_success_callback,
failure=job_failure_callback)
return job
def queueSerialJobsInQueue(self, queue, quota_names, *job_infos):
"""Queue serial jobs in the specified queue."""
return self._queueJobsInQueue(queue, quota_names, job_infos,
serialize=True)
def queueParallelJobsInQueue(self, queue, quota_names, *job_infos):
"""Queue parallel jobs in the specified queue."""
return self._queueJobsInQueue(queue, quota_names, job_infos,
serialize=False)
def queueSerialJobs(self, *job_infos):
"""Queue serial jobs in the default queue."""
queue = self.getQueues()['']
return self.queueSerialJobsInQueue(queue, ('default',), *job_infos)
def queueParallelJobs(self, *job_infos):
"""Queue parallel jobs in the default queue."""
queue = self.getQueues()['']
return self.queueParallelJobsInQueue(queue, ('default',), *job_infos)