def start(self): """Starts the daemon. Does initialization then calls run().""" if self.status != Status.CREATED: print "Can't start a %s that's already been run." \ % type(self).__name__ return if not hasattr(self, 'id'): self.save() if not hasattr(self, 'log'): self.log = make_log(self.log_path) if settings.DEBUG: self.log.info("WARNING, DEBUG is True, which means Django " + "will gobble memory as it stores all database queries.") # This try block is needed because the unit tests run daemons # in threads, which breaks signals. try: for signum in (signal.SIGINT, signal.SIGTERM): signal.signal(signum, self.signal_handler) except ValueError: pass self.log.start_redirect() self.log.info("%s initialized; starting..." % self) self.status = Status.RUNNING self.heartbeat = self.started = datetime.utcnow() self.save() self.heart.start() try: self.run() except Exception: self.set_status(Status.ERROR) self.log.error("An internal error occured!", trace=True) else: if not Status.is_final(self.status): self.set_status(Status.ENDED) finally: self.log.info("Shutting down...") try: self.clean_up() except: self.log.error("Clean up function failed.", trace=True) if not Status.is_final(self.status): self.set_status(Status.ERROR) self.heart.flag.set() self.heart.join() self.ended = datetime.utcnow() self.save() if settings.BACKUP_SYSTEM: self.log.info('Backing up log file...') try: if backup_log(self.log_path): self.log.info('Completed log backup.') else: self.log.error('Failed to backup log.') except: self.log.error('Failed to backup log.', trace=True) self.log.info('%s has been shut down successfully.' % self) self.log.stop_redirect() self.log.close()
def start(self): """Starts the daemon. Does initialization then calls run().""" if self.status != Status.CREATED: print "Can't start a %s that's already been run." \ % type(self).__name__ return if not hasattr(self, 'id'): self.save() if not hasattr(self, 'log'): self.log = make_log(self.log_path) if settings.DEBUG: self.log.info( "WARNING, DEBUG is True, which means Django " + "will gobble memory as it stores all database queries.") # This try block is needed because the unit tests run daemons # in threads, which breaks signals. try: for signum in (signal.SIGINT, signal.SIGTERM): signal.signal(signum, self.signal_handler) except ValueError: pass self.log.start_redirect() self.log.info("%s initialized; starting..." % self) self.status = Status.RUNNING self.heartbeat = self.started = datetime.utcnow() self.save() self.heart.start() try: self.run() except Exception: self.set_status(Status.ERROR) self.log.error("An internal error occured!", trace=True) else: if not Status.is_final(self.status): self.set_status(Status.ENDED) finally: self.log.info("Shutting down...") try: self.clean_up() except: self.log.error("Clean up function failed.", trace=True) if not Status.is_final(self.status): self.set_status(Status.ERROR) self.heart.flag.set() self.heart.join() self.ended = datetime.utcnow() self.save() if settings.BACKUP_SYSTEM: self.log.info('Backing up log file...') try: if backup_log(self.log_path): self.log.info('Completed log backup.') else: self.log.error('Failed to backup log.') except: self.log.error('Failed to backup log.', trace=True) self.log.info('%s has been shut down successfully.' % self) self.log.stop_redirect() self.log.close()
def backup_instance_log(self, instance): self.log.info("Attempting upload of log for %s..." % instance) if backup_log(instance.log_path): self.log.info("Completed upload of log for %s." % instance) else: self.log.info("Failed to upload log for %s." % instance)