Пример #1
0
def create_app(home_path=os.getenv("DIRECTOR_HOME"),
               config_path=os.getenv("DIRECTOR_CONFIG")):
    app = DirectorFlask(__name__)
    c = Config(home_path, config_path)
    app.config.from_object(c)

    # Init User's config
    config.init()

    # Init Blueprints
    app.register_blueprint(api_bp)
    app.register_blueprint(view_bp)
    # Init the blueprint for the User's static assets
    app.register_blueprint(
        Blueprint(
            "user",
            __name__,
            static_url_path="/static/user",
            static_folder=str(Path(app.config["STATIC_FOLDER"])),
        ))

    # Error handler
    app.register_error_handler(404, lambda e: handle_not_found(e))

    # Init extensions
    db.init_app(app)
    db.app = app

    migrate.init_app(
        app=app,
        db=db,
        directory=str(Path(__file__).resolve().parent / "migrations"),
    )
    schema.init_app(app)
    cel.init_app(app)
    cel_workflows.init_app(app)

    # Register the periodic tasks for Celery Beat
    for workflow, conf in cel_workflows.workflows.items():
        if "periodic" in conf:
            payload = conf.get("periodic").get("payload", {})
            schedule = conf.get("periodic").get("schedule")

            cel.conf.beat_schedule.update({
                f"periodic-{workflow}-{schedule}s": {
                    "task": "director.tasks.periodic.execute",
                    "schedule": schedule,
                    "args": (
                        workflow,
                        payload,
                    ),
                }
            })

    return app
Пример #2
0
def create_app(home_path=os.getenv("DIRECTOR_HOME"),
               config_path=os.getenv("DIRECTOR_CONFIG")):
    app = DirectorFlask(__name__)
    c = Config(home_path, config_path)
    app.config.from_object(c)

    # Init User's config
    config.init()

    # Init Blueprints
    app.register_blueprint(api_bp)
    app.register_blueprint(view_bp)
    # Init the blueprint for the User's static assets
    app.register_blueprint(
        Blueprint(
            "user",
            __name__,
            static_url_path="/static/user",
            static_folder=str(Path(app.config["STATIC_FOLDER"])),
        ))

    # Error handler
    app.register_error_handler(HTTPException,
                               lambda e: http_exception_handler(e))

    # Init extensions
    db.init_app(app)
    db.app = app

    migrate.init_app(
        app=app,
        db=db,
        directory=str(Path(__file__).resolve().parent / "migrations"),
    )
    schema.init_app(app)
    cel.init_app(app)
    cel_workflows.init_app(app)
    sentry.init_app(app)

    # Dict passed to the cleanup function
    retentions = {}

    # Register the periodic tasks for Celery Beat
    for workflow, conf in cel_workflows.workflows.items():
        retention = conf.get("retention",
                             {}).get("offset",
                                     app.config["DEFAULT_RETENTION_OFFSET"])

        # A dict is built for the periodic cleaning if the retention is valid
        if retention >= 0:
            retentions[workflow] = retention

        if "periodic" in conf:
            periodic_conf = conf.get("periodic")
            periodic_payload = periodic_conf.get("payload", {})
            schedule_str, schedule_value = build_celery_schedule(
                workflow, periodic_conf)

            cel.conf.beat_schedule.update({
                f"periodic-{workflow}-{schedule_str}": {
                    "task": "director.tasks.periodic.execute",
                    "schedule": schedule_value,
                    "args": (
                        workflow,
                        periodic_payload,
                    ),
                }
            })

    if len(retentions):
        cel.conf.beat_schedule.update({
            "periodic-cleanup": {
                "task": "director.tasks.periodic.cleanup",
                "schedule": crontab(minute=0, hour=0),
                "args": (retentions, ),
            }
        })

    @app.teardown_request
    def session_clear(exception=None):
        db.session.remove()

    return app