2014-03-10 05:18:05 +00:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# This file is part of SickGear.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
2014-03-10 05:18:05 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is distributed in the hope that it will be useful,
|
2014-03-10 05:18:05 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2014-05-26 20:16:07 +00:00
|
|
|
# GNU General Public License for more details.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-11-12 16:43:14 +00:00
|
|
|
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard.exceptions import ex
|
|
|
|
|
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
class Scheduler(threading.Thread):
|
2014-06-19 07:12:30 +00:00
|
|
|
def __init__(self, action, cycleTime=datetime.timedelta(minutes=10), run_delay=datetime.timedelta(minutes=0),
|
2017-12-27 17:54:19 +00:00
|
|
|
start_time=None, threadName="ScheduledThread", silent=True, prevent_cycle_run=None, paused=False):
|
2014-07-15 02:00:53 +00:00
|
|
|
super(Scheduler, self).__init__()
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-06-19 07:12:30 +00:00
|
|
|
self.lastRun = datetime.datetime.now() + run_delay - cycleTime
|
2014-03-10 05:18:05 +00:00
|
|
|
self.action = action
|
|
|
|
self.cycleTime = cycleTime
|
2014-06-19 07:12:30 +00:00
|
|
|
self.start_time = start_time
|
2015-04-05 18:12:15 +00:00
|
|
|
self.prevent_cycle_run = prevent_cycle_run
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
self.name = threadName
|
2014-03-10 05:18:05 +00:00
|
|
|
self.silent = silent
|
2017-12-27 17:54:19 +00:00
|
|
|
self._stop = threading.Event()
|
|
|
|
self._unpause = threading.Event()
|
|
|
|
if not paused:
|
|
|
|
self._unpause.set()
|
2017-11-02 10:29:16 +00:00
|
|
|
self.lock = threading.Lock()
|
2014-05-19 17:40:25 +00:00
|
|
|
self.force = False
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2017-12-27 17:54:19 +00:00
|
|
|
def pause(self):
|
|
|
|
self._unpause.clear()
|
|
|
|
|
|
|
|
def unpause(self):
|
|
|
|
self._unpause.set()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self._stop.set()
|
|
|
|
self.unpause()
|
|
|
|
|
|
|
|
def check_paused(self):
|
|
|
|
if hasattr(self.action, 'check_paused'):
|
|
|
|
if self.action.check_paused():
|
|
|
|
self.pause()
|
|
|
|
self.silent = True
|
|
|
|
else:
|
|
|
|
self.unpause()
|
|
|
|
self.silent = False
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def timeLeft(self):
|
|
|
|
return self.cycleTime - (datetime.datetime.now() - self.lastRun)
|
|
|
|
|
|
|
|
def forceRun(self):
|
|
|
|
if not self.action.amActive:
|
2014-05-19 17:40:25 +00:00
|
|
|
self.force = True
|
2014-03-10 05:18:05 +00:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
def run(self):
|
2017-12-27 17:54:19 +00:00
|
|
|
self.check_paused()
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2017-12-27 17:54:19 +00:00
|
|
|
# if self._unpause Event() is NOT set the loop pauses
|
|
|
|
while self._unpause.wait() and not self._stop.is_set():
|
2014-05-17 09:27:17 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
try:
|
|
|
|
current_time = datetime.datetime.now()
|
|
|
|
should_run = False
|
2014-06-19 07:12:30 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
# check if interval has passed
|
|
|
|
if current_time - self.lastRun >= self.cycleTime:
|
|
|
|
# check if wanting to start around certain time taking interval into account
|
|
|
|
if self.start_time:
|
|
|
|
hour_diff = current_time.time().hour - self.start_time.hour
|
|
|
|
if not hour_diff < 0 and hour_diff < self.cycleTime.seconds / 3600:
|
|
|
|
should_run = True
|
|
|
|
else:
|
|
|
|
# set lastRun to only check start_time after another cycleTime
|
|
|
|
self.lastRun = current_time
|
2014-06-19 07:12:30 +00:00
|
|
|
else:
|
2015-05-04 19:14:29 +00:00
|
|
|
should_run = True
|
|
|
|
|
|
|
|
if self.force:
|
2014-06-19 07:12:30 +00:00
|
|
|
should_run = True
|
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
if should_run and self.prevent_cycle_run is not None and self.prevent_cycle_run():
|
|
|
|
logger.log(u'%s skipping this cycleTime' % self.name, logger.WARNING)
|
|
|
|
# set lastRun to only check start_time after another cycleTime
|
|
|
|
self.lastRun = current_time
|
|
|
|
should_run = False
|
2015-04-05 18:12:15 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
if should_run:
|
|
|
|
self.lastRun = current_time
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
try:
|
|
|
|
if not self.silent:
|
|
|
|
logger.log(u"Starting new thread: " + self.name, logger.DEBUG)
|
2014-05-08 14:03:50 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
self.action.run()
|
2015-06-08 12:47:01 +00:00
|
|
|
except Exception as e:
|
2015-05-04 19:14:29 +00:00
|
|
|
logger.log(u"Exception generated in thread " + self.name + ": " + ex(e), logger.ERROR)
|
2017-06-04 16:19:22 +00:00
|
|
|
logger.log(repr(traceback.format_exc()), logger.ERROR)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
finally:
|
|
|
|
if self.force:
|
|
|
|
self.force = False
|
2014-05-19 17:40:25 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# exiting thread
|
2017-12-27 17:54:19 +00:00
|
|
|
self._stop.clear()
|
|
|
|
self._unpause.clear()
|