(self, func, prefer_looping = False)
Run users work function, safely.
Run users work function, safely.
Run users work function safely .
def run_func_safely(self, func, prefer_looping = False):
        "Run users work function, safely."
        try:
            r = func()
            if self.last_func_fail and time.time() > self.last_func_fail + self.exception_reset:
                self.last_func_fail = None
            # set exception count to 0 after success
            self.exception_count = 0
            return r
        except UsageError, d:
            self.log.error(str(d))
            sys.exit(1)
        except MemoryError, d:
            try: # complex logging may not succeed
                self.log.exception("Job %s out of memory, exiting" % self.job_name)
            except MemoryError:
                self.log.fatal("Out of memory")
            sys.exit(1)
        except SystemExit, d:
            self.send_stats()
            if prefer_looping and self.looping and self.loop_delay > 0:
                self.log.info("got SystemExit(%s), exiting" % str(d))
            self.reset()
            raise d
        except KeyboardInterrupt, d:
            self.send_stats()
            if prefer_looping and self.looping and self.loop_delay > 0:
                self.log.info("got KeyboardInterrupt, exiting")
            self.reset()
            sys.exit(1)
        except Exception, d:
            try: # this may fail too
                self.send_stats()
            except:
                pass
            if self.last_func_fail is None:
                self.last_func_fail = time.time()
            emsg = str(d).rstrip()
            self.reset()
            self.exception_hook(d, emsg)
        # reset and sleep
        self.reset()
        if prefer_looping and self.looping and self.loop_delay > 0:
            # increase exception count & sleep
            self.exception_count += 1
            self.sleep_on_exception()
            return -1
        sys.exit(1)
def run_func_safely ( self , func , prefer_looping = False ) : try : r = func ( ) if self . last_func_fail and time . time ( ) > self . last_func_fail + self . exception_reset : self . last_func_fail = None # set exception count to 0 after success self . exception_count = 0 return r except UsageError , d : self . log . error ( str ( d ) ) sys . exit ( 1 ) except MemoryError , d : try : # complex logging may not succeed self . log . exception ( "Job %s out of memory, exiting" % self . job_name ) except MemoryError : self . log . fatal ( "Out of memory" ) sys . exit ( 1 ) except SystemExit , d : self . send_stats ( ) if prefer_looping and self . looping and self . loop_delay > 0 : self . log . info ( "got SystemExit(%s), exiting" % str ( d ) ) self . reset ( ) raise d except KeyboardInterrupt , d : self . send_stats ( ) if prefer_looping and self . looping and self . loop_delay > 0 : self . log . info ( "got KeyboardInterrupt, exiting" ) self . reset ( ) sys . exit ( 1 ) except Exception , d : try : # this may fail too self . send_stats ( ) except : pass if self . last_func_fail is None : self . last_func_fail = time . time ( ) emsg = str ( d ) . rstrip ( ) self . reset ( ) self . exception_hook ( d , emsg ) # reset and sleep self . reset ( ) if prefer_looping and self . looping and self . loop_delay > 0 : # increase exception count & sleep self . exception_count += 1 self . sleep_on_exception ( ) return - 1 sys . exit ( 1 )