Commits

Marc-Alexandre Chan committed b86f8ff

Fixed signal handler attaching; fixed _populate_queue; fixed missing import

Comments (0)

Files changed (2)

 
 from minibot import DailyPromptMinibot
 from minibot.errors import InvalidRedditError, ConfigValueError,\
-    InvalidUserPass, HTTPError
+    InvalidUserPass, HTTPError, SQLAlchemyError, DBAPIError
 import sys
 
 def main():

minibot/__init__.py

 
 import time
 
-from signal import signal, SIGINT, SIGTERM
-try:
-    from signal import SIGHUP, SIGQUIT, SIGABRT
-except ImportError:
-    pass # may not exist on some systems
+import signal
 
 import logging
 from logging.handlers import RotatingFileHandler
         for sig in ['SIGINT', 'SIGTERM', 'SIGHUP', 'SIGQUIT', 'SIGABRT']:
             try: # Windows doesn't like all of those signals
                 signal.signal(getattr(signal, sig), self.sigterm_handler)
-            except (ValueError, AttributeError):
-                pass
+            except (ValueError, AttributeError) as e:
+                self.logger.warn("Cannot attach handler to signal %s: %s",
+                    sig, repr(e))
 
     def init_resources(self):
         """ Initialises resource components (e.g. database and network classes).
         if self._initialised:
             return
 
+        self.logger.info("Initialising Daily Prompt Minibot resources...")
+
         # database
         try:
             self._init_db()
 
         self._init_scheduler()
 
-        self.initialised = True
+        self.logger.info("Daily Prompt Minibot resources initialised.")
+        self._initialised = True
 
     def _init_logging(self):
         """ Initialises logging. The application-level logger is stored in
             config.sqlite.file, table_prefix=config.sqlite.tableprefix,
             logfile=config.log.db_file, loglevel=config.log.db_level)
         self.db.create_tables()
+        self.logger.debug("Initialised Database resource.")
 
     def _init_reddit(self):
         """ Initialise the Reddit API object. The configuration must be loaded.
                     config.reddit.target))
             else:
                 raise
+        self.logger.debug("Initialised Reddit resource.")
 
     def _init_scheduler(self):
         """ Initialise the event scheduler. """
         self.scheduler = EventScheduler(
             self.config, self.reddit, self.db, self.logger)
+        self.logger.debug("Initialised Scheduler resource.")
 
-    # TODO: test me
     def run(self):
         """ Run the minibot. """
         self.init_resources() # init components if not done yet
             raise
         self._running = False
 
-    # TODO: test me
     def _populate_queue(self):
         """ Populates event scheduler with default/always-running events. """
         now = time.time()
 
         ev_msg = CheckMessageEvent()
         ev_msg.start_time = now + 5
+        ev_msg.interval = self.config.minibot.msg_rate
+        ev_msg.duration = -1
         self.scheduler.queue_event(ev_msg)
 
         ev_post = CheckPostQueueEvent()
         ev_post.start_time = now + 8
+        ev_post.interval = self.config.minibot.msg_rate
+        ev_post.duration = -1
         self.scheduler.queue_event(ev_post)
 
         ev_sugg = CheckSuggestionQueueEvent()
-        ev_post.start_time = now + 13
+        ev_sugg.start_time = now + 13
+        ev_sugg.interval = self.config.minibot.msg_rate
+        ev_sugg.duration = -1
         self.scheduler.queue_event(ev_sugg)
 
         ev_sugg_maint = SuggestionThreadQueueMaintainer()
-        ev_sugg_maint.interval =\
-            self.THREAD_MAINTAINER_INTERVAL_FACTOR * self.config.minibot.queue_rate
-        ev_post.start_time = now + 21
+        ev_sugg_maint.start_time = now + 21
+        ev_sugg_maint.interval = self.THREAD_MAINTAINER_INTERVAL_FACTOR *\
+                                 self.config.minibot.queue_rate
+        ev_sugg.duration = -1
         self.scheduler.queue_event(ev_sugg_maint)
 
-    # TODO: test me
     def sigterm_handler(self, signum, frame):
         """ Handles signals. """
         self.logger.warn("Received signal %d", signum)