Skip to content

Commit

Permalink
Change the way we calculate and check the daily search interval for p…
Browse files Browse the repository at this point in the history
…roviders (#5855)

* Changelog.

* Standardized the namings a little.
Changed param order.

* Import order.

* Import order.

* Small changes
  • Loading branch information
p0psicles authored and medariox committed Dec 8, 2018
1 parent 3ee452a commit 1f4fb2a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
5 changes: 3 additions & 2 deletions medusa/search/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,11 @@ def wanted_episodes(series_obj, from_date):
return wanted


def search_for_needed_episodes(force=False):
def search_for_needed_episodes(scheduler_start_time, force=False):
"""Search providers for needed episodes.
:param force: run the search even if no episodes are needed
:param scheduler_start_time: timestamp of the start of the search scheduler
:return: list of found episodes
"""
show_list = app.showList
Expand Down Expand Up @@ -520,7 +521,7 @@ def search_for_needed_episodes(force=False):
threading.currentThread().name = u'{thread} :: [{provider}]'.format(
thread=original_thread_name, provider=cur_provider.name
)
cur_provider.cache.update_cache()
cur_provider.cache.update_cache(scheduler_start_time)

single_results = {}
multi_results = []
Expand Down
9 changes: 7 additions & 2 deletions medusa/search/daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import threading
from builtins import object
from datetime import date, datetime, timedelta
from time import time

from medusa import app, common
from medusa.db import DBConnection
Expand All @@ -23,6 +24,7 @@
from medusa.search.queue import DailySearchQueueItem
from medusa.show.show import Show


log = BraceAdapter(logging.getLogger(__name__))
log.logger.addHandler(logging.NullHandler())

Expand All @@ -49,6 +51,9 @@ def run(self, force=False): # pylint:disable=too-many-branches
return

self.amActive = True
# Let's keep track of the exact time the scheduler kicked in,
# as we need to compare to this time for each provider.
scheduler_start_time = int(time())

if not network_dict:
update_network_dict()
Expand Down Expand Up @@ -110,9 +115,9 @@ def run(self, force=False): # pylint:disable=too-many-branches
main_db_con = DBConnection()
main_db_con.mass_action(new_releases)

# queue episode for daily search
# queue a daily search
app.search_queue_scheduler.action.add_item(
DailySearchQueueItem(force=force)
DailySearchQueueItem(scheduler_start_time, force=force)
)

self.amActive = False
7 changes: 4 additions & 3 deletions medusa/search/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,15 @@ def add_item(self, item):


class DailySearchQueueItem(generic_queue.QueueItem):
"""Daily searche queue item class."""
"""Daily search queue item class."""

def __init__(self, force):
def __init__(self, scheduler_start_time, force):
"""Initialize the class."""
generic_queue.QueueItem.__init__(self, u'Daily Search', DAILY_SEARCH)

self.success = None
self.started = None
self.scheduler_start_time = scheduler_start_time
self.force = force

def run(self):
Expand All @@ -259,7 +260,7 @@ def run(self):

try:
log.info('Beginning daily search for new episodes')
found_results = search_for_needed_episodes(force=self.force)
found_results = search_for_needed_episodes(self.scheduler_start_time, force=self.force)

if not found_results:
log.info('No needed episodes found')
Expand Down
17 changes: 9 additions & 8 deletions medusa/tv/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ def _check_item_auth(self, title, url):
"""Check item auth."""
return True

def update_cache(self):
def update_cache(self, search_start_time):
"""Update provider cache."""
# check if we should update
if not self.should_update():
if not self.should_update(search_start_time):
return

try:
Expand All @@ -211,7 +211,7 @@ def update_cache(self):
self._clear_cache()

# set updated
self.updated = time()
self.updated = search_start_time

# get last 5 rss cache results
recent_results = self.provider.recent_results
Expand Down Expand Up @@ -363,13 +363,14 @@ def _set_time(self, table, value):
{'provider': self.provider_id}
)

def should_update(self):
def should_update(self, scheduler_start_time):
"""Check if we should update provider cache."""
# if we've updated recently then skip the update
if time() - self.updated < self.minTime * 60:
log.debug('Last update was too soon, using old cache: {0}.'
' Updated less than {1} minutes ago',
self.updated, self.minTime)
if scheduler_start_time - self.updated < self.minTime * 60:
log.debug('Last update was too soon, using old cache.'
' Last update ran {0} seconds ago.'
' Updated less than {1} minutes ago.',
scheduler_start_time - self.updated, self.minTime)
return False
log.debug('Updating providers cache')

Expand Down

0 comments on commit 1f4fb2a

Please sign in to comment.