Skip to content

Commit

Permalink
Add new option to run just new jobs or jobs without history (#831)
Browse files Browse the repository at this point in the history
New command-line option `--prepare-jobs` to initialize new jobs or jobs without history
  • Loading branch information
nille02 authored Jan 5, 2025
1 parent cbc8eb0 commit b601917
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format mostly follows [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## UNRELEASED

### Added

- New command-line option `--prepare-jobs` to initialize new jobs or jobs without history (#831 by nille02)

## [2.29] -- 2024-10-28

### Added
Expand Down
15 changes: 15 additions & 0 deletions lib/urlwatch/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ def test_filter(self, id):
# (ignore_cached) and we do not want to store the newly-retrieved data yet (filter testing)
return 0

def prepare_jobs(self):
new_jobs = []
for idx, job in enumerate(self.urlwatcher.jobs):
has_history = self.urlwatcher.cache_storage.has_history_data(job.get_guid())
if not has_history:
logger.info('Add Job: %s', job.pretty_name())
new_jobs.append(idx + 1)
if not new_jobs:
return 0
self.urlwatch_config.idx_set = frozenset(new_jobs)
self.urlwatcher.run_jobs()
self.urlwatcher.close()

def _resolve_job_history(self, id, max_entries=10):
job = self._get_job(id)

Expand Down Expand Up @@ -274,6 +287,8 @@ def handle_actions(self):
sys.exit(self.test_filter(self.urlwatch_config.test_filter))
if self.urlwatch_config.test_diff_filter:
sys.exit(self.test_diff_filter(self.urlwatch_config.test_diff_filter))
if self.urlwatch_config.prepare_jobs:
sys.exit(self.prepare_jobs())
if self.urlwatch_config.dump_history:
sys.exit(self.dump_history(self.urlwatch_config.dump_history))
if self.urlwatch_config.list:
Expand Down
1 change: 1 addition & 0 deletions lib/urlwatch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def parse_args(self, cmdline_args):
group.add_argument('--delete', metavar='JOB', help='delete job by location or index')
group.add_argument('--enable', metavar='JOB', help='enable job by location or index')
group.add_argument('--disable', metavar='JOB', help='disable job by location or index')
group.add_argument('--prepare-jobs', action='store_true', help='run jobs without history')
group.add_argument('--change-location', metavar=('JOB', 'NEW_LOCATION'), nargs=2, help='change the location of an existing job by location or index')
group.add_argument('--test-filter', metavar='JOB', help='test filter output of job by location or index')
group.add_argument('--test-diff-filter', metavar='JOB',
Expand Down
14 changes: 14 additions & 0 deletions lib/urlwatch/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ def __init__(self, filename):
self.db = minidb.Store(self.filename, debug=True, vacuum_on_close=False)
self.db.register(CacheEntry)

self._cached_has_history_data_set = None

def close(self):
self.db.close()
self.db = None
Expand Down Expand Up @@ -600,6 +602,15 @@ def get_history_data(self, guid, count=1):
break
return history

def has_history_data(self, guid):
if not self._cached_has_history_data_set:
self._cached_has_history_data_set = frozenset(guid[0] for guid in
list(CacheEntry.query(self.db, CacheEntry.c.guid,
where=((CacheEntry.c.tries == 0)
| (CacheEntry.c.tries == None))) # noqa:E711
))
return guid in self._cached_has_history_data_set

def save(self, job, guid, data, timestamp, tries, etag=None):
self.db.save(CacheEntry(guid=guid, timestamp=timestamp, data=data, tries=tries, etag=etag))
self.db.commit()
Expand Down Expand Up @@ -688,6 +699,9 @@ def get_history_data(self, guid, count=1):
break
return history

def has_history_data(self, guid):
return bool(self.get_history_data(guid))

def save(self, job, guid, data, timestamp, tries, etag=None):
r = {
'data': data,
Expand Down

0 comments on commit b601917

Please sign in to comment.