aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar jesopo2018-11-27 14:25:12 +0000
committerGravatar jesopo2018-11-27 14:25:12 +0000
commitd13a5069e3a0e78a77bf16effe78f2ba223c495b (patch)
treeea7fb7c8dfcb0cea94523572e03b7bab35d7aa49
parentShow list of users in a channel, not just count (diff)
signature
Grab response from functions asked to be executed on the main thread and feed
them back to the callers, allowing rest_api.py to take the main thread while it's waiting for the response to give back to the requesting client
-rw-r--r--modules/github.py11
-rw-r--r--modules/rest_api.py5
-rw-r--r--modules/signals.py6
-rw-r--r--src/IRCBot.py26
4 files changed, 30 insertions, 18 deletions
diff --git a/modules/github.py b/modules/github.py
index ecfe8e7a..dbe20b70 100644
--- a/modules/github.py
+++ b/modules/github.py
@@ -86,16 +86,13 @@ class Module(ModuleManager.BaseModule):
for server, channel in targets:
for output in outputs:
output = "(%s) %s" % (full_name, output)
- trigger = self._make_trigger(channel, server, output)
- self.bot.trigger(trigger)
+ self.events.on("send.stdout").call(target=channel,
+ module_name="Github", server=server, message=line,
+ hide_prefix=channel.get_setting(
+ "github-hide-prefix", False))
return True
- def _make_trigger(self, channel, server, line):
- return lambda: self.events.on("send.stdout").call(target=channel,
- module_name="Github", server=server, message=line,
- hide_prefix=channel.get_setting("github-hide-prefix", False))
-
def _change_count(self, n, symbol, color):
return utils.irc.color("%s%d" % (symbol, n), color)+utils.irc.bold("")
def _added(self, n):
diff --git a/modules/rest_api.py b/modules/rest_api.py
index 28d05aed..601eebd8 100644
--- a/modules/rest_api.py
+++ b/modules/rest_api.py
@@ -30,9 +30,10 @@ class Handler(http.server.BaseHTTPRequestHandler):
if path.startswith("/api/"):
event_response = None
try:
- event_response = _events.on("api").on(method).on(
+ event_response = _bot.trigger(lambda:
+ _events.on("api").on(method).on(
endpoint).call_unsafe_for_result(params=params,
- path=args, data=data, headers=dict(self.headers))
+ path=args, data=data, headers=dict(self.headers)))
except Exception as e:
_log.error("failed to call API endpoint \"%s\"",
[path], exc_info=True)
diff --git a/modules/signals.py b/modules/signals.py
index 9bf2c9a8..9d8230ed 100644
--- a/modules/signals.py
+++ b/modules/signals.py
@@ -11,7 +11,10 @@ class Module(ModuleManager.BaseModule):
def SIGINT(self, signum, frame):
print()
- self.events.on("signal.interrupt").call(signum=signum, frame=frame)
+ self.bot.trigger(lambda: self._kill(signum))
+
+ def _kill(self, signum):
+ self.events.on("signal.interrupt").call(signum=signum)
for server in self.bot.servers.values():
reason = "Leaving"
@@ -19,7 +22,6 @@ class Module(ModuleManager.BaseModule):
reason = self.events.on("get.quit-quote"
).call_for_result(default=reason)
server.send_quit(reason)
- self.bot.trigger()
self.events.on("writebuffer.empty").hook(
lambda event: self.bot.disconnect(event["server"]))
diff --git a/src/IRCBot.py b/src/IRCBot.py
index 62594404..d12cc377 100644
--- a/src/IRCBot.py
+++ b/src/IRCBot.py
@@ -1,4 +1,4 @@
-import os, select, socket, sys, threading, time, traceback, typing, uuid
+import queue, os, select, socket, sys, threading, time, traceback, typing, uuid
from src import EventManager, Exports, IRCServer, Logging, ModuleManager
from src import Socket, utils
@@ -29,12 +29,23 @@ class Bot(object):
self._trigger_functions = []
self._events.on("timer.reconnect").hook(self._timed_reconnect)
- def trigger(self, func: typing.Callable[[], typing.Any]=None):
+ def trigger(self,
+ func: typing.Optional[typing.Callable[[], typing.Any]]=None):
+ func = func or (lambda: None)
+ if threading.current_thread() is threading.main_thread():
+ returned = func()
+ self._trigger_client.send(b"TRIGGER")
+ return returned
+
self.lock.acquire()
- if func:
- self._trigger_functions.append(func)
- self._trigger_client.send(b"TRIGGER")
+
+ func_queue = queue.Queue(1)
+ self._trigger_functions.append([func, func_queue])
+
self.lock.release()
+ self._trigger_client.send(b"TRIGGER")
+
+ return func_queue.get(True)
def add_server(self, server_id: int, connect: bool = True,
connection_params: typing.Optional[
@@ -165,8 +176,9 @@ class Bot(object):
self._timers.call()
self.cache.expire()
- for func in self._trigger_functions:
- func()
+ for func, func_queue in self._trigger_functions:
+ returned = func()
+ func_queue.put(returned)
self._trigger_functions.clear()
for fd, event in events: