Skip to content

Commit

Permalink
handle edge-case of cron-schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
ansibleguy committed Oct 6, 2024
1 parent 110196c commit 1334197
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/ansibleguy-webui/aw/api_endpoints/job_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def get_viewable_jobs_serialized(
job_serialized['next_run'] = None

try:
if job.schedule is not None and job.enabled:
if is_set(job.schedule) and job.enabled:
job_serialized['next_run'] = get_next_cron_execution_str(job.schedule) + f" {config['timezone']}"

except ValueError:
Expand Down
8 changes: 8 additions & 0 deletions src/ansibleguy-webui/aw/execute/threader.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ def run(self, error: bool = False) -> None:
return

while not self.state_stop.is_set():
if not is_set(self.job.schedule):
return

wait_sec = get_next_cron_execution_sec(self.job.schedule)

if wait_sec == -1:
return

self.next_execution_time = get_next_cron_execution_str(schedule=self.job.schedule, wait_sec=wait_sec)
log(
f"Next execution of job {self.log_name_debug} at "
Expand All @@ -96,6 +103,7 @@ def run(self, error: bool = False) -> None:

log(f"Starting job {self.log_name_debug}", level=5)
self.run_playbook()

break

except (AnsibleConfigError, AnsibleRepositoryError, OSError) as err:
Expand Down
20 changes: 15 additions & 5 deletions src/ansibleguy-webui/aw/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,30 @@ def datetime_from_db_str(dt: (datetime, None), fmt: str = SHORT_TIME_FORMAT) ->


def get_next_cron_execution_sec(schedule: str) -> float:
cron = CronTab(schedule)
set_timezone(str(config.timezone))
return cron.next(now=datetime_w_tz())
try:
cron = CronTab(schedule)
set_timezone(str(config.timezone))
return cron.next(now=datetime_w_tz())

except ValueError:
return -1

def get_next_cron_execution(schedule: str, wait_sec: (int, float) = None) -> datetime:

def get_next_cron_execution(schedule: str, wait_sec: (int, float) = None) -> (datetime, None):
if wait_sec is None:
wait_sec = get_next_cron_execution_sec(schedule)
if wait_sec == -1:
return None

return datetime.fromtimestamp(time() + wait_sec)


def get_next_cron_execution_str(schedule: str, wait_sec: (int, float) = None) -> str:
return get_next_cron_execution(schedule, wait_sec).strftime(SHORT_TIME_FORMAT)
next_exec_dt = get_next_cron_execution(schedule, wait_sec)
if next_exec_dt is None:
return ''

return next_exec_dt.strftime(SHORT_TIME_FORMAT)


def _open_file_0600(path: (str, Path), flags):
Expand Down

0 comments on commit 1334197

Please sign in to comment.