119 lines
3.7 KiB
Python
119 lines
3.7 KiB
Python
# w.c.s. - web application for online forms
|
|
# Copyright (C) 2005-2010 Entr'ouvert
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import sys
|
|
import time
|
|
import traceback
|
|
import uuid
|
|
|
|
from quixote import get_publisher, get_response
|
|
from quixote.directory import Directory
|
|
|
|
from . import N_, _, errors, force_text
|
|
from .storage import StorableObject
|
|
|
|
|
|
class AfterJobStatusDirectory(Directory):
|
|
def _q_lookup(self, component):
|
|
try:
|
|
job = AfterJob.get(component)
|
|
except KeyError:
|
|
raise errors.TraversalError()
|
|
|
|
response = get_response()
|
|
response.set_content_type('text/plain')
|
|
if not job.completion_status:
|
|
return job.status + '|' + str(_(job.status))
|
|
return job.status + '|' + str(_(job.status)) + ' ' + job.completion_status
|
|
|
|
|
|
class AfterJob(StorableObject):
|
|
_names = 'afterjobs'
|
|
_reset_class = False
|
|
|
|
label = None
|
|
status = None
|
|
creation_time = None
|
|
completion_time = None
|
|
completion_status = None
|
|
|
|
execute = None
|
|
|
|
def __init__(self, label=None, cmd=None, **kwargs):
|
|
super().__init__(id=str(uuid.uuid4()))
|
|
if label:
|
|
self.label = force_text(label)
|
|
self.done_action_label_arg = kwargs.pop('done_action_label', None)
|
|
self.done_action_url_arg = kwargs.pop('done_action_url', None)
|
|
self.done_button_attributes_arg = kwargs.pop('done_button_attributes', None)
|
|
self.creation_time = time.time()
|
|
self.job_cmd = cmd
|
|
self.status = N_('registered')
|
|
self.kwargs = kwargs
|
|
|
|
def done_action_label(self):
|
|
return self.done_action_label_arg
|
|
|
|
def done_action_url(self):
|
|
return self.done_action_url_arg % {'job_id': self.id}
|
|
|
|
def done_action_attributes(self):
|
|
return self.done_button_attributes_arg
|
|
|
|
def run(self, spool=False):
|
|
if self.completion_time:
|
|
return
|
|
|
|
if spool and self.id and self.execute:
|
|
from django.conf import settings
|
|
|
|
if 'uwsgi' in sys.modules and settings.WCS_MANAGE_COMMAND:
|
|
from .spooler import run_after_job
|
|
|
|
self.store()
|
|
run_after_job.spool(tenant_dir=get_publisher().app_dir, job_id=self.id)
|
|
return
|
|
|
|
self.status = N_('running')
|
|
if self.id:
|
|
self.store()
|
|
try:
|
|
if self.execute:
|
|
self.execute()
|
|
else:
|
|
self.job_cmd(job=self)
|
|
except Exception:
|
|
get_publisher().notify_of_exception(sys.exc_info())
|
|
self.exception = traceback.format_exc()
|
|
self.status = N_('failed')
|
|
else:
|
|
self.status = N_('completed')
|
|
self.completion_time = time.time()
|
|
if self.id:
|
|
self.store()
|
|
|
|
def __getstate__(self):
|
|
if getattr(self, 'done_action_label_arg', None):
|
|
self.done_action_label_arg = force_text(self.done_action_label_arg)
|
|
if not isinstance(self.job_cmd, str):
|
|
obj_dict = self.__dict__.copy()
|
|
obj_dict['job_cmd'] = None
|
|
return obj_dict
|
|
return self.__dict__
|
|
|
|
def get_processing_url(self):
|
|
return '/backoffice/processing?job=%s' % self.id
|