summaryrefslogtreecommitdiff
path: root/plugins/msgqueue.py
diff options
context:
space:
mode:
authorLivio Recchia <recchialivio@libero.it>2020-02-10 23:06:34 +0100
committerLivio Recchia <recchialivio@libero.it>2020-02-10 23:06:34 +0100
commit9a13903a2f7d3a65fdf15a65fb59cccd622e2066 (patch)
tree9403b7dff39eb5e5d7fa0f79efb69b496add4c4b /plugins/msgqueue.py
parent11cc316b74d5f3f283413a33e7693b314741aa4a (diff)
downloadmanachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.gz
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.bz2
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.xz
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.zip
Initial commit
Diffstat (limited to 'plugins/msgqueue.py')
-rw-r--r--plugins/msgqueue.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/plugins/msgqueue.py b/plugins/msgqueue.py
new file mode 100644
index 0000000..25fe2b0
--- /dev/null
+++ b/plugins/msgqueue.py
@@ -0,0 +1,76 @@
+from collections import deque
+from loggers import debuglog
+from logicmanager import logic_manager
+
+
+__all__ = [ 'PLUGIN', 'init', 'delayed_functions', 'reload_function' ]
+
+
+PLUGIN = {
+ 'name': 'msgqueue',
+ 'requires': (),
+ 'blocks': (),
+}
+
+
+reloaded_functions = {}
+event_queue = deque()
+_times = { 'next_event' : 0 }
+
+delayed_functions = {
+ # 'net.mapserv.cmsg_chat_whisper': 7.5,
+ # 'net.mapserv.cmsg_chat_message': 3.5,
+}
+
+
+def delayed_function(func_name, delay):
+
+ def func(*args, **kwargs):
+ call = (delay, reloaded_functions[func_name], args, kwargs)
+ event_queue.append(call)
+
+ return func
+
+
+def reload_function(name, delay):
+
+ def recurs_import(name):
+ m = __import__(name)
+ for n in name.split('.')[1:]:
+ m = getattr(m, n)
+ return m
+
+ ss = name.rsplit('.', 1)
+
+ if len(ss) == 1:
+ ss.insert(0, 'net.mapserv')
+ name = 'net.mapserv.' + name
+
+ try:
+ module = recurs_import(ss[0])
+ func_name = ss[1]
+ reloaded_functions[name] = getattr(module, func_name)
+ setattr(module, func_name, delayed_function(name, delay))
+ debuglog.debug('function %s wrapped with delay %d', name, delay)
+
+ except Exception as e:
+ debuglog.error('error wrapping function %s: %s', name, e)
+
+
+def msgq_logic(ts):
+ if len(event_queue):
+ if ts > _times['next_event']:
+ delay, func, args, kwargs = event_queue.popleft()
+ _times['next_event'] = ts + delay
+ func(*args, **kwargs)
+
+
+def init(config):
+ section = PLUGIN['name']
+ for option in config.options(section):
+ delayed_functions[option] = config.getfloat(section, option)
+
+ for func_name, delay in delayed_functions.iteritems():
+ reload_function(func_name, delay)
+
+ logic_manager.add_logic(msgq_logic)