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

Lucio Correia luciojhc at linux.vnet.ibm.com
Mon May 9 19:55:49 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/model/notifications.py | 52 ++++++++++++++++++++++++++++++------------
 src/wok/objectstore.py         | 36 -----------------------------
 src/wok/server.py              |  4 ----
 3 files changed, 38 insertions(+), 54 deletions(-)

Changes in V2:
 * Do not log notification delete requests

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