Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
65 lines (46 sloc) 1.87 KB
import datetime
import time
import threading
import traceback
from lib.tvdb_api import tvdb_exceptions
from sickbeard import logger
class Scheduler:
def __init__(self, action, cycleTime=datetime.timedelta(minutes=10), runImmediately=True, threadName="ScheduledThread", silent=False):
if runImmediately:
self.lastRun = datetime.datetime.fromordinal(1)
self.lastRun =
self.action = action
self.cycleTime = cycleTime
self.thread = None
self.threadName = threadName
self.silent = silent
self.abort = False
def initThread(self):
if self.thread == None or not self.thread.isAlive():
self.thread = threading.Thread(None, self.runAction, self.threadName)
def timeLeft(self):
return self.cycleTime - ( - self.lastRun)
def forceRun(self):
if not self.action.amActive:
self.lastRun = datetime.datetime.fromordinal(1)
return True
return False
def runAction(self):
while True:
currentTime =
if currentTime - self.lastRun > self.cycleTime:
self.lastRun = currentTime
if not self.silent:
logger.log(u"Starting new thread: "+self.threadName, logger.DEBUG)
except Exception, e:
logger.log(u"Exception generated in thread "+self.threadName+": " + str(e), logger.ERROR)
logger.log(traceback.format_exc(), logger.DEBUG)
if self.abort:
self.abort = False
self.thread = None
Something went wrong with that request. Please try again.