* Add plugin and log_id attributes to AsyncTask
* Create _log() function to log AsyncTask exit status
* Create auxiliary method to save log entry ID for a task
* Handle WokException separately in order to add it in
translatable form to user log
Signed-off-by: Lucio Correia <luciojhc(a)linux.vnet.ibm.com>
---
src/wok/asynctask.py | 46 ++++++++++++++++++++++++++++++++++++++++++----
src/wok/i18n.py | 2 ++
2 files changed, 44 insertions(+), 4 deletions(-)
diff --git a/src/wok/asynctask.py b/src/wok/asynctask.py
index b98ad9a..0d42eda 100644
--- a/src/wok/asynctask.py
+++ b/src/wok/asynctask.py
@@ -25,7 +25,14 @@ import time
import traceback
import uuid
+from wok.exception import WokException
+from wok.reqlogger import RequestRecord, ASYNCTASK_REQUEST_METHOD
+from wok.utils import get_plugin_from_uri
+
+MSG_FAILED = 'WOKASYNC0002L'
+MSG_SUCCESS = 'WOKASYNC0001L'
+task_id = 0
tasks_queue = {}
@@ -40,26 +47,53 @@ def clean_old_tasks():
task.remove()
+def save_request_log_id(log_id, task_id):
+ tasks_queue[task_id].log_id = log_id
+
+
class AsyncTask(object):
def __init__(self, target_uri, fn, opaque=None):
+ # task info
self.id = str(uuid.uuid1())
self.target_uri = target_uri
self.fn = fn
+ self.log_id = None
+ self.plugin = get_plugin_from_uri(target_uri)
+ self.timestamp = time.time()
+
+ # task context
self.status = 'running'
self.message = 'The request is being processing.'
- self.timestamp = time.time()
self._cp_request = cherrypy.serving.request
self.thread = threading.Thread(target=self._run_helper,
args=(opaque, self._status_cb))
self.thread.setDaemon(True)
self.thread.start()
+
# let's prevent memory leak in tasks_queue
clean_old_tasks()
tasks_queue[self.id] = self
- def _status_cb(self, message, success=None):
+ def _log(self, code, status, exception=None):
+ RequestRecord(
+ {'code': code, 'params': {'target_uri':
self.target_uri}},
+ exception,
+ self.log_id,
+ app=self.plugin,
+ req=ASYNCTASK_REQUEST_METHOD,
+ status=status,
+ user='',
+ ip='',
+ ).log()
+
+ def _status_cb(self, message, success=None, exception=None):
if success is not None:
- self.status = 'finished' if success else 'failed'
+ if success:
+ self.status = 'finished'
+ self._log(MSG_SUCCESS, 200)
+ else:
+ self.status = 'failed'
+ self._log(MSG_FAILED, 400, exception)
if message.strip():
self.message = message
@@ -68,10 +102,14 @@ class AsyncTask(object):
cherrypy.serving.request = self._cp_request
try:
self.fn(cb, opaque)
+ except WokException, e:
+ cherrypy.log.error_log.error("Error in async_task %s " % self.id)
+ cherrypy.log.error_log.error(traceback.format_exc())
+ cb(e.message, success=False, exception=e)
except Exception, e:
cherrypy.log.error_log.error("Error in async_task %s " % self.id)
cherrypy.log.error_log.error(traceback.format_exc())
- cb(e.message, False)
+ cb(e.message, success=False)
def remove(self):
try:
diff --git a/src/wok/i18n.py b/src/wok/i18n.py
index ade2ae9..3ef684d 100644
--- a/src/wok/i18n.py
+++ b/src/wok/i18n.py
@@ -57,6 +57,8 @@ messages = {
"WOKPROXY0001E": _("Unable to (re)start system's nginx.service.
Details: '%(error)s'"),
# These messages (ending with L) are for user log purposes
+ "WOKASYNC0001L": _("Successfully completed task
'%(target_uri)s'"),
+ "WOKASYNC0002L": _("Failed to complete task
'%(target_uri)s'"),
"WOKCOL0001L": _("Request made on collection"),
"WOKRES0001L": _("Request made on resource"),
"WOKROOT0001L": _("User '%(username)s' login"),
--
1.9.1