Skip to content

Commit

Permalink
Add option to disable individual jobs
Browse files Browse the repository at this point in the history
Signed-off-by: James Hewitt <[email protected]>
  • Loading branch information
Jamstah committed Jan 23, 2024
1 parent e342af9 commit 487b767
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format mostly follows [Keep a Changelog](http://keepachangelog.com/en/1.0.0/

## UNRELEASED

### Added

- New `enabled` option for all jobs. Set to false to disable a job without needing to remove it or comment it out (Requested in #625 by snowman, contributed in #785 by jamstah)

### Changed

- Remove EOL'd Python 3.7 (new minimum requirement is Python 3.8), add Python 3.12 testing
Expand Down
1 change: 1 addition & 0 deletions docs/source/jobs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Optional keys for all job types
- ``compared_versions``: Number of versions to compare for similarity
- ``kind`` (redundant): Either ``url``, ``shell`` or ``browser``. Automatically derived from the unique key (``url``, ``command`` or ``navigate``) of the job type
- ``user_visible_url``: Different URL to show in reports (e.g. when watched URL is a REST API URL, and you want to show a webpage)
- ``enabled``: Can be set to false to disable an individual job (default is ``true``)


Setting keys for all jobs at once
Expand Down
5 changes: 4 additions & 1 deletion lib/urlwatch/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def ignore_error(self, exception):

class Job(JobBase):
__required__ = ()
__optional__ = ('name', 'filter', 'max_tries', 'diff_tool', 'compared_versions', 'diff_filter', 'treat_new_as_changed', 'user_visible_url')
__optional__ = ('name', 'filter', 'max_tries', 'diff_tool', 'compared_versions', 'diff_filter', 'enabled', 'treat_new_as_changed', 'user_visible_url')

# determine if hyperlink "a" tag is used in HtmlReporter
def location_is_url(self):
Expand All @@ -205,6 +205,9 @@ def location_is_url(self):
def pretty_name(self):
return self.name if self.name else self.get_location()

def is_enabled(self):
return self.enabled is None or self.enabled


class ShellJob(Job):
"""Run a shell command and get its standard output"""
Expand Down
6 changes: 6 additions & 0 deletions lib/urlwatch/tests/data/disabled-job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: "1"
url: "|echo job 1"
enabled: false
---
name: "2"
url: "|echo job 2"
19 changes: 19 additions & 0 deletions lib/urlwatch/tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ def test_run_watcher():
finally:
cache_storage.close()

def test_disabled_job():
with teardown_func():
urls = os.path.join(here, 'data', 'disabled-job.yaml')
config = os.path.join(here, 'data', 'urlwatch.yaml')
cache = os.path.join(here, 'data', 'cache.db')
hooks = ''

config_storage = YamlConfigStorage(config)
urls_storage = UrlsYaml(urls)
cache_storage = CacheMiniDBStorage(cache)
try:
urlwatch_config = ConfigForTest(config, urls, cache, hooks, True)

urlwatcher = Urlwatch(urlwatch_config, config_storage, cache_storage, urls_storage)
urlwatcher.run_jobs()

assert len(urlwatcher.report.job_states) == 1
finally:
cache_storage.close()

def test_unserialize_shell_job_without_kind():
job = JobBase.unserialize({
Expand Down
2 changes: 1 addition & 1 deletion lib/urlwatch/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def run_jobs(urlwatcher):
raise ValueError(f'All job indices must be between 1 and {len(urlwatcher.jobs)}: {urlwatcher.urlwatch_config.joblist}')
cache_storage = urlwatcher.cache_storage
jobs = [job.with_defaults(urlwatcher.config_storage.config)
for (idx, job) in enumerate(urlwatcher.jobs) if ((idx + 1) in urlwatcher.urlwatch_config.joblist or (not urlwatcher.urlwatch_config.joblist))]
for (idx, job) in enumerate(urlwatcher.jobs) if job.is_enabled() and ((idx + 1) in urlwatcher.urlwatch_config.joblist or (not urlwatcher.urlwatch_config.joblist))]
report = urlwatcher.report

logger.debug('Processing %d jobs (out of %d)', len(jobs), len(urlwatcher.jobs))
Expand Down

0 comments on commit 487b767

Please sign in to comment.