feat(crons): refactored

This commit is contained in:
Taha Yassine Kraiem 2022-09-14 11:25:50 +01:00
parent 31512e7f69
commit 9de9d43a8a
5 changed files with 20 additions and 15 deletions

View file

@ -20,6 +20,8 @@ PG_CONFIG = dict(_PG_CONFIG)
if config("pg_timeout", cast=int, default=0) > 0:
PG_CONFIG["options"] = f"-c statement_timeout={config('pg_timeout', cast=int) * 1000}"
logging.info(f">PG_POOL:{config('PG_POOL', cast=bool, default=None)}")
class ORThreadedConnectionPool(psycopg2.pool.ThreadedConnectionPool):
def __init__(self, minconn, maxconn, *args, **kwargs):

View file

@ -1,28 +1,33 @@
print("============= CRONS =============")
import sys
import asyncio
import sys
from routers.crons import core_dynamic_crons
ACTIONS = {
"TELEMETRY": core_dynamic_crons.telemetry_cron,
"JOB": core_dynamic_crons.run_scheduled_jobs,
"REPORT": core_dynamic_crons.weekly_report
}
def default_action(action):
async def _func():
print(f"{action} not found in crons-definitions")
print("possible actions:")
print(ACTIONS.keys())
return _func
async def process(action):
await {
"TELEMETRY": core_dynamic_crons.telemetry_cron,
"JOB": core_dynamic_crons.run_scheduled_jobs,
"REPORT": core_dynamic_crons.weekly_report2
}.get(action.upper(), default_action(action))()
await ACTIONS.get(action.upper(), default_action(action))()
if __name__ == '__main__':
if len(sys.argv) < 2 or len(sys.argv[1]) < 1:
print("please provide actions as argument")
print("please provide actions as argument\npossible actions:")
print(ACTIONS.keys())
else:
print(f"action: {sys.argv[1]}")
asyncio.run(process(sys.argv[1]))

View file

@ -6,9 +6,8 @@ from starlette.responses import RedirectResponse
import schemas
import schemas_ee
from chalicelib.core import integrations_manager
from chalicelib.core import sessions
from chalicelib.core import tenants, users, metadata, projects, license
from chalicelib.core import tenants, users, projects, license
from chalicelib.core import webhook
from chalicelib.core.collaboration_slack import Slack
from chalicelib.utils import SAML2_helper

View file

@ -1,5 +1,6 @@
from chalicelib.core import telemetry, unlock
from chalicelib.core import weekly_report, jobs
from chalicelib.core import jobs
from chalicelib.core import weekly_report as weekly_report_script
from decouple import config
@ -7,15 +8,14 @@ async def run_scheduled_jobs() -> None:
jobs.execute_jobs()
async def weekly_report2() -> None:
weekly_report.cron()
async def weekly_report() -> None:
weekly_report_script.cron()
async def telemetry_cron() -> None:
telemetry.compute()
# @app.schedule(Cron('0/60', '*', '*', '*', '?', '*'))
def unlock_cron() -> None:
print("validating license")
unlock.check()
@ -28,7 +28,7 @@ cron_jobs = [
SINGLE_CRONS = [{"func": telemetry_cron, "trigger": "cron", "day_of_week": "*"},
{"func": run_scheduled_jobs, "trigger": "interval", "seconds": 60, "misfire_grace_time": 20},
{"func": weekly_report2, "trigger": "cron", "day_of_week": "mon", "hour": 5,
{"func": weekly_report, "trigger": "cron", "day_of_week": "mon", "hour": 5,
"misfire_grace_time": 60 * 60}]
if config("LOCAL_CRONS", default=False, cast=bool):

View file

@ -1,7 +1,6 @@
from chalicelib.core import roles, traces
from chalicelib.core import unlock
from chalicelib.utils import assist_helper
from chalicelib.utils.TimeUTC import TimeUTC
unlock.check()