2014-03-10 01:18:05 -04:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
|
|
|
# This file is part of Sick Beard.
|
|
|
|
#
|
|
|
|
# Sick Beard is free software: you can redistribute it and/or modify
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# Sick Beard is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import threading
|
2014-05-08 10:03:50 -04:00
|
|
|
import Queue
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
from sickbeard import logger
|
|
|
|
|
|
|
|
class QueuePriorities:
|
|
|
|
LOW = 10
|
|
|
|
NORMAL = 20
|
|
|
|
HIGH = 30
|
|
|
|
|
2014-05-08 10:03:50 -04:00
|
|
|
class GenericQueue:
|
2014-03-10 01:18:05 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.currentItem = None
|
|
|
|
self.thread = None
|
|
|
|
self.queue_name = "QUEUE"
|
|
|
|
self.min_priority = 0
|
2014-05-11 08:49:07 -04:00
|
|
|
self.queue = Queue.Queue()
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def pause(self):
|
|
|
|
logger.log(u"Pausing queue")
|
|
|
|
self.min_priority = 999999999999
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def unpause(self):
|
|
|
|
logger.log(u"Unpausing queue")
|
|
|
|
self.min_priority = 0
|
|
|
|
|
|
|
|
def add_item(self, item):
|
|
|
|
item.added = datetime.datetime.now()
|
2014-05-11 08:49:07 -04:00
|
|
|
self.queue.put(item)
|
2014-03-10 01:18:05 -04:00
|
|
|
return item
|
|
|
|
|
2014-05-12 07:10:45 -04:00
|
|
|
def run(self):
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
# only start a new task if one isn't already going
|
2014-03-20 14:03:22 -04:00
|
|
|
if self.thread == None or self.thread.isAlive() == False:
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
# if the thread is dead then the current item should be finished
|
2014-03-20 14:03:22 -04:00
|
|
|
if self.currentItem != None:
|
2014-03-10 01:18:05 -04:00
|
|
|
self.currentItem.finish()
|
|
|
|
self.currentItem = None
|
|
|
|
|
2014-05-08 10:03:50 -04:00
|
|
|
if not self.queue.empty():
|
|
|
|
queueItem = self.queue.get()
|
2014-03-10 01:18:05 -04:00
|
|
|
if queueItem.priority < self.min_priority:
|
|
|
|
return
|
|
|
|
|
|
|
|
threadName = self.queue_name + '-' + queueItem.get_thread_name()
|
2014-05-12 04:52:14 -04:00
|
|
|
self.thread = threading.Thread(None, queueItem.execute, threadName)
|
2014-05-12 07:10:45 -04:00
|
|
|
self.thread.start()
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
self.currentItem = queueItem
|
|
|
|
|
|
|
|
class QueueItem:
|
2014-03-25 01:57:24 -04:00
|
|
|
def __init__(self, name, action_id=0):
|
2014-03-10 01:18:05 -04:00
|
|
|
self.name = name
|
|
|
|
self.inProgress = False
|
|
|
|
self.priority = QueuePriorities.NORMAL
|
|
|
|
self.thread_name = None
|
|
|
|
self.action_id = action_id
|
|
|
|
self.added = None
|
|
|
|
|
|
|
|
def get_thread_name(self):
|
|
|
|
if self.thread_name:
|
|
|
|
return self.thread_name
|
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
return self.name.replace(" ", "-").upper()
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
"""Implementing classes should call this"""
|
|
|
|
|
|
|
|
self.inProgress = True
|
|
|
|
|
|
|
|
def finish(self):
|
|
|
|
"""Implementing Classes should call this"""
|
|
|
|
|
2014-05-08 10:03:50 -04:00
|
|
|
self.inProgress = False
|