Skip to content

Commit

Permalink
Merge pull request #114 from Drazzilb08/scheduler-fix
Browse files Browse the repository at this point in the history
Scheduler fix
  • Loading branch information
Drazzilb08 authored Mar 7, 2024
2 parents 810b72e + 974765e commit 9d3fc99
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ prettytable = "*"
croniter = "*"
"ruamel.yaml" = "*"
docker = "*"
python-dateutil = "*"

[dev-packages]

Expand Down
15 changes: 8 additions & 7 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
-i https://pypi.org/simple
certifi==2024.2.2; python_version >= '3.6'
charset-normalizer==3.3.2; python_full_version >= '3.7.0'
croniter==2.0.1; python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'
croniter==2.0.2; python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'
docker==7.0.0; python_version >= '3.8'
idna==3.6; python_version >= '3.5'
packaging==23.2; python_version >= '3.7'
pillow==10.2.0; python_version >= '3.8'
plexapi==4.15.10; python_version >= '3.8'
prettytable==3.10.0; python_version >= '3.8'
python-dateutil==2.8.2; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
python-dateutil==2.9.0.post0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
pytz==2024.1
pyyaml==6.0.1; python_version >= '3.6'
qbittorrent-api==2024.2.59; python_version >= '3.8'
Expand Down
36 changes: 28 additions & 8 deletions util/scheduler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from datetime import datetime
from croniter import croniter
from dateutil import tz

last_run = {}
next_run_times = {}

def check_schedule(script_name, schedule, logger):
"""
Expand Down Expand Up @@ -60,14 +61,33 @@ def check_schedule(script_name, schedule, logger):
if start_month <= current_month <= end_month and start_day <= current_day <= end_day:
return True
elif frequency == "cron":
cron = croniter(data)
next_run = cron.get_next(datetime)
if last_run.get(script_name) is None:
last_run[script_name] = next_run
return False
if next_run > last_run[script_name]:
last_run[script_name] = next_run
local_tz = tz.tzlocal()
local_date = datetime.now(local_tz)

current_time = datetime.now(local_tz).replace(second=0, microsecond=0)

logger.debug(f"Local time: {current_time}")

next_run = next_run_times.get(script_name)
if next_run is None:
next_run = croniter(data, local_date).get_next(datetime)
next_run_times[script_name] = next_run

logger.debug(f"Next run for {script_name}: {next_run}")

if next_run <= current_time:
next_run = croniter(data, local_date).get_next(datetime)
next_run_times[script_name] = next_run

logger.debug(f"Next run for {script_name}: {next_run}\n")

return True
else:
logger.debug(f"Next run time for script {script_name}: {next_run} is in the future\n")
return False



except ValueError as e:
logger.error(f"Invalid schedule: {schedule} for script: {script_name}")
logger.error (f"Error: {e}")
Expand Down

0 comments on commit 9d3fc99

Please sign in to comment.