Add a cron_setup
task to initialize code-defined cron tasks.
This commit is contained in:
parent
9fa3084b0f
commit
576bf6bb89
5 changed files with 88 additions and 1 deletions
52
files/commands/cron_setup.py
Normal file
52
files/commands/cron_setup.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
|
||||
from typing import Optional
|
||||
import datetime
|
||||
|
||||
import sqlalchemy
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from files.__main__ import app, db_session
|
||||
from files.classes.cron.pycallable import PythonCodeTask
|
||||
from files.classes.cron.tasks import DayOfWeek
|
||||
from files.helpers.config.const import AUTOJANNY_ID
|
||||
|
||||
|
||||
@app.cli.command('cron_setup')
|
||||
def cron_setup():
|
||||
db: Session = db_session()
|
||||
|
||||
tasklist = db.query(PythonCodeTask)
|
||||
|
||||
# I guess in theory we should load this from a file or something, but, ehhhh
|
||||
hardcoded_cron_jobs = {
|
||||
#'testjob': {
|
||||
#'frequency_day': DayOfWeek.ALL,
|
||||
#'time_of_day_utc': datetime.time(0, 0),
|
||||
#'import_path': 'files.commands.debug_printout',
|
||||
#'callable': 'printstuff',
|
||||
#},
|
||||
}
|
||||
|
||||
print(f"{tasklist.count()} tasks")
|
||||
for task in tasklist:
|
||||
if task.label and task.label in hardcoded_cron_jobs:
|
||||
print(f"Cron: Updating {task.label}")
|
||||
ref = hardcoded_cron_jobs[task.label]
|
||||
task.frequency_day = ref["frequency_day"]
|
||||
task.time_of_day_utc = ref["time_of_day_utc"]
|
||||
task.import_path = ref["import_path"]
|
||||
task.callable = ref["callable"]
|
||||
del hardcoded_cron_jobs[task.label]
|
||||
|
||||
for label, ref in hardcoded_cron_jobs.items():
|
||||
print(f"Cron: Creating {label}")
|
||||
task = PythonCodeTask()
|
||||
task.label = label
|
||||
task.author_id = AUTOJANNY_ID
|
||||
task.frequency_day = ref["frequency_day"]
|
||||
task.time_of_day_utc = ref["time_of_day_utc"]
|
||||
task.import_path = ref["import_path"]
|
||||
task.callable = ref["callable"]
|
||||
db.add(task)
|
||||
|
||||
db.commit()
|
Loading…
Add table
Add a link
Reference in a new issue