Skip to content

Commit

Permalink
Update for #2653 (AP Scheduler triggers are function calls and not st…
Browse files Browse the repository at this point in the history
…rings anymore)
  • Loading branch information
Ozzie Isaacs committed Jan 15, 2023
1 parent ce0b3d8 commit e178efb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
8 changes: 4 additions & 4 deletions cps/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import datetime

from . import config, constants
from .services.background_scheduler import BackgroundScheduler, use_APScheduler
from .services.background_scheduler import BackgroundScheduler, CronTrigger, use_APScheduler
from .tasks.database import TaskReconnectDatabase
from .tasks.thumbnail import TaskGenerateCoverThumbnails, TaskGenerateSeriesThumbnails, TaskClearCoverThumbnailCache
from .services.worker import WorkerThread
Expand Down Expand Up @@ -66,10 +66,10 @@ def register_scheduled_tasks(reconnect=True):
duration = config.schedule_duration

# Register scheduled tasks
scheduler.schedule_tasks(tasks=get_scheduled_tasks(reconnect), trigger='cron', hour=start)
scheduler.schedule_tasks(tasks=get_scheduled_tasks(reconnect), trigger=CronTrigger(hour=start))
end_time = calclulate_end_time(start, duration)
scheduler.schedule(func=end_scheduled_tasks, trigger='cron', name="end scheduled task", hour=end_time.hour,
minute=end_time.minute)
scheduler.schedule(func=end_scheduled_tasks, trigger=CronTrigger(hour=end_time.hour, minute=end_time.minute),
name="end scheduled task")

# Kick-off tasks, if they should currently be running
if should_task_be_running(start, duration):
Expand Down
16 changes: 9 additions & 7 deletions cps/services/background_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

try:
from apscheduler.schedulers.background import BackgroundScheduler as BScheduler
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.date import DateTrigger
use_APScheduler = True
except (ImportError, RuntimeError) as e:
use_APScheduler = False
Expand All @@ -47,31 +49,31 @@ def __new__(cls):

return cls._instance

def schedule(self, func, trigger, name=None, **trigger_args):
def schedule(self, func, trigger, name=None):
if use_APScheduler:
return self.scheduler.add_job(func=func, trigger=trigger, name=name, **trigger_args)
return self.scheduler.add_job(func=func, trigger=trigger, name=name)

# Expects a lambda expression for the task
def schedule_task(self, task, user=None, name=None, hidden=False, trigger='cron', **trigger_args):
def schedule_task(self, task, user=None, name=None, hidden=False, trigger=None):
if use_APScheduler:
def scheduled_task():
worker_task = task()
worker_task.scheduled = True
WorkerThread.add(user, worker_task, hidden=hidden)
return self.schedule(func=scheduled_task, trigger=trigger, name=name, **trigger_args)
return self.schedule(func=scheduled_task, trigger=trigger, name=name)

# Expects a list of lambda expressions for the tasks
def schedule_tasks(self, tasks, user=None, trigger='cron', **trigger_args):
def schedule_tasks(self, tasks, user=None, trigger=None):
if use_APScheduler:
for task in tasks:
self.schedule_task(task[0], user=user, trigger=trigger, name=task[1], hidden=task[2], **trigger_args)
self.schedule_task(task[0], user=user, trigger=trigger, name=task[1], hidden=task[2])

# Expects a lambda expression for the task
def schedule_task_immediately(self, task, user=None, name=None, hidden=False):
if use_APScheduler:
def immediate_task():
WorkerThread.add(user, task(), hidden)
return self.schedule(func=immediate_task, trigger='date', name=name)
return self.schedule(func=immediate_task, trigger=DateTrigger(), name=name)

# Expects a list of lambda expressions for the tasks
def schedule_tasks_immediately(self, tasks, user=None):
Expand Down

0 comments on commit e178efb

Please sign in to comment.