Bläddra i källkod

docstrings in Bot; daemonize wiki_scheduler

tags/v0.1^2
Ben Kurtovic 12 år sedan
förälder
incheckning
6373eea1f7
2 ändrade filer med 28 tillägg och 11 borttagningar
  1. +26
    -9
      earwigbot/bot.py
  2. +2
    -2
      earwigbot/irc/connection.py

+ 26
- 9
earwigbot/bot.py Visa fil

@@ -69,6 +69,7 @@ class Bot(object):
self.tasks.load() self.tasks.load()


def _start_irc_components(self): def _start_irc_components(self):
"""Start the IRC frontend/watcher in separate threads if enabled."""
if self.config.components.get("irc_frontend"): if self.config.components.get("irc_frontend"):
self.logger.info("Starting IRC frontend") self.logger.info("Starting IRC frontend")
self.frontend = Frontend(self) self.frontend = Frontend(self)
@@ -80,6 +81,7 @@ class Bot(object):
Thread(name="irc_watcher", target=self.watcher.loop).start() Thread(name="irc_watcher", target=self.watcher.loop).start()


def _start_wiki_scheduler(self): def _start_wiki_scheduler(self):
"""Start the wiki scheduler in a separate thread if enabled."""
def wiki_scheduler(): def wiki_scheduler():
while self._keep_looping: while self._keep_looping:
time_start = time() time_start = time()
@@ -91,15 +93,27 @@ class Bot(object):


if self.config.components.get("wiki_scheduler"): if self.config.components.get("wiki_scheduler"):
self.logger.info("Starting wiki scheduler") self.logger.info("Starting wiki scheduler")
Thread(name="wiki_scheduler", target=wiki_scheduler).start()
thread = Thread(name="wiki_scheduler", target=wiki_scheduler)
thread.daemon = True # Stop if other threads stop
thread.start()


def _stop_irc_components(self): def _stop_irc_components(self):
"""Request the IRC frontend and watcher to stop if enabled."""
if self.frontend: if self.frontend:
self.frontend.stop() self.frontend.stop()
if self.watcher: if self.watcher:
self.watcher.stop() self.watcher.stop()


def _loop(self):
def run(self):
"""Main entry point into running the bot.

Starts all config-enabled components and then enters an idle loop,
ensuring that all components remain online and restarting components
that get disconnected from their servers.
"""
self.logger.info("Starting bot")
self._start_irc_components()
self._start_wiki_scheduler()
while self._keep_looping: while self._keep_looping:
with self.component_lock: with self.component_lock:
if self.frontend and self.frontend.is_stopped(): if self.frontend and self.frontend.is_stopped():
@@ -110,15 +124,17 @@ class Bot(object):
self.logger.warn("IRC watcher has stopped; restarting") self.logger.warn("IRC watcher has stopped; restarting")
self.watcher = Watcher(self) self.watcher = Watcher(self)
Thread(name=name, target=self.watcher.loop).start() Thread(name=name, target=self.watcher.loop).start()
sleep(3)

def run(self):
self.logger.info("Starting bot")
self._start_irc_components()
self._start_wiki_scheduler()
self._loop()
sleep(2)


def restart(self): def restart(self):
"""Reload config, commands, tasks, and safely restart IRC components.

This is thread-safe, and it will gracefully stop IRC components before
reloading anything. Note that you can safely reload commands or tasks
without restarting the bot with bot.commands.load() or
bot.tasks.load(). These should not interfere with running components
or tasks.
"""
self.logger.info("Restarting bot per request from owner") self.logger.info("Restarting bot per request from owner")
with self.component_lock: with self.component_lock:
self._stop_irc_components() self._stop_irc_components()
@@ -128,6 +144,7 @@ class Bot(object):
self._start_irc_components() self._start_irc_components()


def stop(self): def stop(self):
"""Gracefully stop all bot components."""
self.logger.info("Shutting down bot") self.logger.info("Shutting down bot")
with self.component_lock: with self.component_lock:
self._stop_irc_components() self._stop_irc_components()


+ 2
- 2
earwigbot/irc/connection.py Visa fil

@@ -147,10 +147,10 @@ class IRCConnection(object):
self._close() self._close()
break break


def stop(self):
def stop(self, msg=None):
"""Request the IRC connection to close at earliest convenience.""" """Request the IRC connection to close at earliest convenience."""
if self._is_running: if self._is_running:
self.quit()
self.quit(msg)
self._is_running = False self._is_running = False


def is_stopped(self): def is_stopped(self):


Laddar…
Avbryt
Spara