add IRCBot.stop() - use it for !shutdown and './bitbotctl stop'
This commit is contained in:
parent
cf0ac7d25b
commit
aa2511baaa
3 changed files with 32 additions and 11 deletions
|
@ -20,10 +20,11 @@ def _die(s):
|
||||||
sys.stderr.write("%s\n" % s)
|
sys.stderr.write("%s\n" % s)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
SIMPLE = ["rehash", "reload", "stop"]
|
||||||
if args.command == "log":
|
if args.command == "log":
|
||||||
arg_parser.add_argument("--level", "-l", help="Log level",
|
arg_parser.add_argument("--level", "-l", help="Log level",
|
||||||
default="INFO")
|
default="INFO")
|
||||||
elif args.command in ["rehash", "reload"]:
|
elif args.command in SIMPLE:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
_die("Unknown command '%s'" % args.command)
|
_die("Unknown command '%s'" % args.command)
|
||||||
|
@ -52,10 +53,8 @@ _send("0 version 0")
|
||||||
|
|
||||||
if args.command == "log":
|
if args.command == "log":
|
||||||
_send("1 log %s" % args.level)
|
_send("1 log %s" % args.level)
|
||||||
elif args.command == "rehash":
|
elif args.command in SIMPLE:
|
||||||
_send("1 rehash")
|
_send("1 %s" % args.command)
|
||||||
elif args.command == "reload":
|
|
||||||
_send("1 reload")
|
|
||||||
|
|
||||||
read_buffer = b""
|
read_buffer = b""
|
||||||
|
|
||||||
|
|
|
@ -102,6 +102,9 @@ class Control(PollSource.PollSource):
|
||||||
result = self._bot.try_reload_modules()
|
result = self._bot.try_reload_modules()
|
||||||
response_data = result.message
|
response_data = result.message
|
||||||
keepalive = False
|
keepalive = False
|
||||||
|
elif command == "stop":
|
||||||
|
self._bot.stop()
|
||||||
|
keepalive = False
|
||||||
|
|
||||||
self._send_action(client, response_action, response_data, id)
|
self._send_action(client, response_action, response_data, id)
|
||||||
if not keepalive:
|
if not keepalive:
|
||||||
|
|
|
@ -50,7 +50,8 @@ class Bot(object):
|
||||||
self._timers = timers
|
self._timers = timers
|
||||||
|
|
||||||
self.start_time = time.time()
|
self.start_time = time.time()
|
||||||
self.running = False
|
self._writing = False
|
||||||
|
self._reading = False
|
||||||
self.servers = {}
|
self.servers = {}
|
||||||
self.reconnections = {}
|
self.reconnections = {}
|
||||||
|
|
||||||
|
@ -273,19 +274,37 @@ class Bot(object):
|
||||||
except BitBotPanic:
|
except BitBotPanic:
|
||||||
return
|
return
|
||||||
def _run(self):
|
def _run(self):
|
||||||
self.running = True
|
self._writing = True
|
||||||
|
self._reading = True
|
||||||
|
|
||||||
self._read_thread = self._daemon_thread(
|
self._read_thread = self._daemon_thread(
|
||||||
lambda: self._loop_catch("read", self._read_loop))
|
lambda: self._loop_catch("read", self._read_loop))
|
||||||
self._write_thread = self._daemon_thread(
|
self._write_thread = self._daemon_thread(
|
||||||
lambda: self._loop_catch("write", self._write_loop))
|
lambda: self._loop_catch("write", self._write_loop))
|
||||||
self._event_loop()
|
self._event_loop()
|
||||||
|
|
||||||
|
def stop(self, reason: str="Stopping"):
|
||||||
|
self._reading = False # disable read thread
|
||||||
|
self.trigger_read()
|
||||||
|
for server in self.servers.values():
|
||||||
|
line = server.send_quit(reason)
|
||||||
|
line.events.on("send").hook(self._shutdown_hook(server))
|
||||||
|
def _shutdown_hook(self, server):
|
||||||
|
def shutdown(e):
|
||||||
|
server.disconnect()
|
||||||
|
self.disconnect(server)
|
||||||
|
if not self.servers:
|
||||||
|
self._writing = False
|
||||||
|
return shutdown
|
||||||
|
|
||||||
def _kill(self):
|
def _kill(self):
|
||||||
self.running = False
|
self._writing = False
|
||||||
|
self._reading = False
|
||||||
self._trigger_both()
|
self._trigger_both()
|
||||||
|
|
||||||
def _event_loop(self):
|
def _event_loop(self):
|
||||||
while self.running or not self._event_queue.empty():
|
while ((self._writing or self._reading) or
|
||||||
|
not self._event_queue.empty()):
|
||||||
try:
|
try:
|
||||||
item = self._event_queue.get(block=True,
|
item = self._event_queue.get(block=True,
|
||||||
timeout=self.get_poll_timeout())
|
timeout=self.get_poll_timeout())
|
||||||
|
@ -320,7 +339,7 @@ class Bot(object):
|
||||||
self.panic("Exception on '%s' thread" % name, throw=False)
|
self.panic("Exception on '%s' thread" % name, throw=False)
|
||||||
|
|
||||||
def _write_loop(self):
|
def _write_loop(self):
|
||||||
while self.running:
|
while self._writing:
|
||||||
poll_sources = {}
|
poll_sources = {}
|
||||||
with self._write_condition:
|
with self._write_condition:
|
||||||
fds = []
|
fds = []
|
||||||
|
@ -364,7 +383,7 @@ class Bot(object):
|
||||||
|
|
||||||
def _read_loop(self):
|
def _read_loop(self):
|
||||||
poll_sources = {}
|
poll_sources = {}
|
||||||
while self.running:
|
while self._reading:
|
||||||
new_poll_sources = {}
|
new_poll_sources = {}
|
||||||
for poll_source in self._poll_sources:
|
for poll_source in self._poll_sources:
|
||||||
for fileno in poll_source.get_readables():
|
for fileno in poll_source.get_readables():
|
||||||
|
|
Loading…
Reference in a new issue