Skip to content

Commit

Permalink
feat: allow configuring background jobs in module
Browse files Browse the repository at this point in the history
  • Loading branch information
boosterl committed Sep 9, 2024
1 parent 56504e2 commit b8f77e6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ dependencies = [

[project.optional-dependencies]
loader = [
"APScheduler>=3.10.4",
"cloudevents>=1.9.0",
"inuits-policy-based-auth>=9.6.0",
"pytz>=2024.1",
"six>=1.16.0",
"tzlocal>=5.2",
]
util = [
"cloudevents>=1.9.0",
Expand Down
27 changes: 25 additions & 2 deletions src/elody/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import os

from apscheduler.triggers.cron import CronTrigger
from importlib import import_module
from inuits_policy_based_auth.exceptions import (
PolicyFactoryException,
Expand All @@ -16,6 +17,24 @@ def load_apps(flask_app, logger):
flask_app.register_blueprint(api_bp)


def load_jobs(scheduler, logger):
apps = util.read_json_as_dict(os.getenv("APPS_MANIFEST"), logger)
for app in apps:
for job, job_properties in apps[app].get("jobs", {}).items():
try:
job_class = __get_class_from_module(
import_module(f"apps.{app}.cron_jobs.{job}")
)
scheduler.add_job(
job_class(),
CronTrigger.from_crontab(
job_properties.get("expression", "0 0 * * *")
),
)
except ModuleNotFoundError:
pass


def load_policies(policy_factory, logger, permissions={}):
if permissions:
from elody.policies.permission_handler import set_permissions
Expand Down Expand Up @@ -69,11 +88,15 @@ def __get_class(app, auth_type, policy_module_name):
pass
else:
raise ModuleNotFoundError(f"Policy {policy_module_name} not found")
policy_class_name = module.__name__.split(".")[-1].title().replace("_", "")
policy = getattr(module, policy_class_name)
policy = __get_class_from_module(module)
return policy


def __get_class_from_module(module):
class_name = module.__name__.split(".")[-1].title().replace("_", "")
return getattr(module, class_name)


def __instantiate_authentication_policy(policy_module_name, policy, logger):
allow_anonymous_users = os.getenv("ALLOW_ANONYMOUS_USERS", False) in [
"True",
Expand Down

0 comments on commit b8f77e6

Please sign in to comment.