[Kimchi-devel] [PATCH] [Wok] Save notifications in memory instead of object store

Lucio Correia luciojhc at linux.vnet.ibm.com
Fri May 6 21:09:08 UTC 2016


Notifications are temporary data structure, so save it in
memory instead of object store, allowing it to work even
when there is no disk space.

Signed-off-by: Lucio Correia <luciojhc at linux.vnet.ibm.com>
---
 src/wok/control/notifications.py |  6 +++++
 src/wok/model/notifications.py   | 52 +++++++++++++++++++++++++++++-----------
 src/wok/objectstore.py           | 36 ----------------------------
 src/wok/server.py                |  4 ----
 4 files changed, 44 insertions(+), 54 deletions(-)

diff --git a/src/wok/control/notifications.py b/src/wok/control/notifications.py
index 37d45f2..b57595e 100644
--- a/src/wok/control/notifications.py
+++ b/src/wok/control/notifications.py
@@ -21,6 +21,11 @@ from wok.control.base import Collection, Resource
 from wok.control.utils import UrlSubNode
 
 
+NOTIFICATION_REQUESTS = {
+    'DELETE': {'default': "UI notification deleted: %(ident)s"},
+}
+
+
 @UrlSubNode('notifications', True)
 class Notifications(Collection):
     def __init__(self, model):
@@ -31,6 +36,7 @@ class Notifications(Collection):
 class Notification(Resource):
     def __init__(self, model, id):
         super(Notification, self).__init__(model, id)
+        self.log_map = NOTIFICATION_REQUESTS
 
     @property
     def data(self):
diff --git a/src/wok/model/notifications.py b/src/wok/model/notifications.py
index 77184db..79bed03 100644
--- a/src/wok/model/notifications.py
+++ b/src/wok/model/notifications.py
@@ -17,40 +17,64 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
+from datetime import datetime
+
 from wok.exception import NotFoundError, OperationFailed
 from wok.message import WokMessage
+from wok.utils import wok_log
+
+
+notificationsStore = {}
+
+
+def add_notification(code, args={}, plugin_name=None):
+    if not code:
+        wok_log.error("Unable to add notification: invalid code '%(code)s'" %
+                      {'code': str(code)})
+        return
+
+    global notificationsStore
+    notification = notificationsStore.get(code)
+
+    # do not update timestamp if notification already exists
+    timestamp = datetime.now().isoformat() if notification is None else \
+        notification['timestamp']
+
+    args.update({"_plugin_name": plugin_name, "timestamp": timestamp})
+    notificationsStore[code] = args
 
 
 class NotificationsModel(object):
     def __init__(self, **kargs):
-        self.objstore = kargs['objstore']
+        pass
 
     def get_list(self):
-        with self.objstore as session:
-            return session.get_list('notification')
+        global notificationsStore
+        return notificationsStore.keys()
 
 
 class NotificationModel(object):
     def __init__(self, **kargs):
-        self.objstore = kargs['objstore']
+        pass
 
     def lookup(self, id):
-        with self.objstore as session:
-            notification = session.get('notification', str(id))
+        global notificationsStore
+        notification = notificationsStore.get(str(id))
 
-            # use WokMessage to translate the notification
-            if notification:
-                timestamp = notification['timestamp']
-                plugin = notification.pop('_plugin_name', None)
-                message = WokMessage(id, notification, plugin).get_text()
-                return {"code": id, "message": message, "timestamp": timestamp}
+        # use WokMessage to translate the notification
+        if notification:
+            timestamp = notification['timestamp']
+            plugin = notification.pop('_plugin_name', None)
+            message = WokMessage(str(id), notification, plugin).get_text()
+            return {"code": id, "message": message, "timestamp": timestamp}
 
         raise NotFoundError("WOKNOT0001E", {'id': str(id)})
 
     def delete(self, id):
+        global notificationsStore
+
         try:
-            with self.objstore as session:
-                session.delete('notification', str(id))
+            del notificationsStore[str(id)]
         except Exception as e:
             raise OperationFailed("WOKNOT0002E", {'id': str(id),
                                                   'msg': e.msg()})
diff --git a/src/wok/objectstore.py b/src/wok/objectstore.py
index ff3796c..59354f3 100644
--- a/src/wok/objectstore.py
+++ b/src/wok/objectstore.py
@@ -23,8 +23,6 @@ import sqlite3
 import threading
 import traceback
 
-from datetime import datetime
-
 try:
     from collections import OrderedDict
 except ImportError:
@@ -146,37 +144,3 @@ class ObjectStore(object):
                 # exception again
                 wok_log.error(traceback.format_exc())
                 return False
-
-
-def add_notification(code, args={}, plugin_name=None):
-    if not code:
-        wok_log.error("Unable to add notification: invalid code '%(code)s'" %
-                      {'code': str(code)})
-        return
-
-    try:
-        with ObjectStore() as session:
-            notification = session.get('notification', code)
-    except NotFoundError:
-        notification = None
-
-    try:
-        # do not update timestamp if notification already exists
-        timestamp = datetime.now().isoformat() if notification is None else \
-            notification['timestamp']
-        args.update({"_plugin_name": plugin_name, "timestamp": timestamp})
-
-        with ObjectStore() as session:
-            session.store('notification', code, args)
-    except Exception as e:
-        wok_log.error("Unable to store notification: %s" % e.message)
-
-
-def clean_notifications():
-    try:
-        with ObjectStore() as session:
-            notifications = session.get_list('notification')
-            for item in notifications:
-                session.delete('notification', item)
-    except Exception as e:
-        wok_log.error("Unable to clean notifications: %s" % e.message)
diff --git a/src/wok/server.py b/src/wok/server.py
index a329ed4..902d4bf 100644
--- a/src/wok/server.py
+++ b/src/wok/server.py
@@ -33,7 +33,6 @@ from wok.config import config as configParser
 from wok.config import paths, PluginConfig, WokConfig
 from wok.control import sub_nodes
 from wok.model import model
-from wok.objectstore import clean_notifications
 from wok.proxy import start_proxy, terminate_proxy
 from wok.reqlogger import RequestLogger
 from wok.root import WokRoot
@@ -107,9 +106,6 @@ class Server(object):
         if dev_env:
             cherrypy.log.screen = True
 
-        # clean object store notifications
-        clean_notifications()
-
         # close standard file handlers because we are going to use a
         # watchedfiled handler, otherwise we will have two file handlers
         # pointing to the same file, duplicating log enries
-- 
1.9.1




More information about the Kimchi-devel mailing list