Skip to content

Commit

Permalink
Add Preparation phase (Flexget#1866)
Browse files Browse the repository at this point in the history
* Moved all config changing plugin into new prepearation phase

* Change remember rejected into what it was before config hash refactor

* Changed preparation to prepare

* Skipping prepare phase on rerun
  • Loading branch information
liiight authored Jun 5, 2017
1 parent 81b81b9 commit e9799f1
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions flexget/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def decorator(target):

# task phases, in order of their execution; note that this can be extended by
# registering new phases at runtime
task_phases = ['start', 'input', 'metainfo', 'filter', 'download', 'modify', 'output', 'learn', 'exit']
task_phases = ['prepare', 'start', 'input', 'metainfo', 'filter', 'download', 'modify', 'output', 'learn', 'exit']

# map phase names to method names
phase_methods = {
Expand Down Expand Up @@ -414,7 +414,7 @@ def _load_plugins_from_dirs(dirs):
else:
log.trace('Loaded module %s from %s', module_name, plugin_path)
_check_phase_queue()


def _load_plugins_from_packages():
"""Load plugins installed via PIP"""
Expand Down
22 changes: 8 additions & 14 deletions flexget/plugins/filter/remember_rejected.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,27 @@ class FilterRememberRejected(object):

@plugin.priority(0)
def on_task_start(self, task, config):
with Session() as session:
remember_task = session.query(RememberTask).filter(RememberTask.name == task.name).first()
if not remember_task:
# Create this task in the db if not present
session.add(RememberTask(name=task.name))

@plugin.priority(-255)
def on_task_input(self, task, config):
"""Purge remembered entries if the config has changed."""
with Session() as session:
# See if the task has changed since last run
remember_task = session.query(RememberTask).filter(RememberTask.name == task.name).first()
if not task.is_rerun and remember_task and task.config_modified:
old_task = session.query(RememberTask).filter(RememberTask.name == task.name).first()
if not task.is_rerun and old_task and task.config_modified:
log.debug('Task config has changed since last run, purging remembered entries.')
session.delete(remember_task)
remember_task = None
if not remember_task:
session.delete(old_task)
old_task = None
if not old_task:
# Create this task in the db if not present
session.add(RememberTask(name=task.name))
elif not task.is_rerun:
# Delete expired items if this is not a rerun
deleted = session.query(RememberEntry).filter(RememberEntry.task_id == remember_task.id). \
deleted = session.query(RememberEntry).filter(RememberEntry.task_id == old_task.id). \
filter(RememberEntry.expires < datetime.now()).delete()
if deleted:
log.debug('%s entries have expired from remember_rejected table.' % deleted)
task.config_changed()

@plugin.priority(-255)
def on_task_input(self, task, config):
for entry in task.all_entries:
entry.on_reject(self.on_entry_reject)

Expand Down
4 changes: 4 additions & 0 deletions flexget/plugins/filter/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,10 @@ class SeriesDBManager(FilterSeriesBase):

@plugin.priority(0)
def on_task_start(self, task, config):
# Only operate if task changed
if not task.config_modified:
return

# Clear all series from this task
with Session() as session:
session.query(SeriesTask).filter(SeriesTask.name == task.name).delete()
Expand Down
2 changes: 1 addition & 1 deletion flexget/plugins/operate/configure_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def schema(self):
'additionalProperties': False
}

def on_task_start(self, task, config):
def on_task_prepare(self, task, config):

series = {}
for input_name, input_config in config.get('from', {}).items():
Expand Down
2 changes: 1 addition & 1 deletion flexget/plugins/operate/include.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PluginInclude(object):
schema = one_or_more({'type': 'string'})

@plugin.priority(256)
def on_task_start(self, task, config):
def on_task_prepare(self, task, config):
if not config:
return

Expand Down
2 changes: 1 addition & 1 deletion flexget/plugins/operate/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def prepare_config(self, config):
return config

@plugin.priority(257)
def on_task_start(self, task, config):
def on_task_prepare(self, task, config):
if config is False: # handles 'template: no' form to turn off template on this task
return
# implements --template NAME
Expand Down
8 changes: 4 additions & 4 deletions flexget/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,8 @@ def __run_task_phase(self, phase):
finally:
fire_event('task.execute.after_plugin', self, plugin.name)
self.session = None
# check config hash for changes at the end of 'start' phase
if phase == 'start':
# check config hash for changes at the end of 'prepare' phase
if phase == 'prepare':
self.check_config_hash()

def __run_plugin(self, plugin, phase, args=None, kwargs=None):
Expand Down Expand Up @@ -601,8 +601,8 @@ def _execute(self):
log.info('Plugin %s is not executed because %s phase is disabled (e.g. --test)' %
(plugin.name, phase))
continue
if phase == 'start' and self.is_rerun:
log.debug('skipping task_start during rerun')
if phase in ('start', 'prepare') and self.is_rerun:
log.debug('skipping phase %s during rerun', phase)
elif phase == 'exit' and self._rerun and self._rerun_count < self.max_reruns:
log.debug('not running task_exit yet because task will rerun')
else:
Expand Down

0 comments on commit e9799f1

Please sign in to comment.