-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2169685
commit bf11967
Showing
12 changed files
with
164 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
version: '3.0' | ||
|
||
services: | ||
redis: | ||
image: redis:7.4-alpine | ||
ports: | ||
- '6379:6379' | ||
command: redis-server --requirepass admin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
import logging.config | ||
|
||
from celery import Celery | ||
|
||
from ..plugin.parser import EntryPointPlugin | ||
|
||
|
||
def init_celery_manager(*, plugins: list[EntryPointPlugin]): | ||
""" | ||
Create a new Celery app and initialize it with the given plugins. | ||
""" | ||
from .. import manager | ||
|
||
_log = logging.getLogger(__name__) | ||
|
||
if not manager.settings.visyn_core.celery: | ||
_log.warning("No Celery settings found in configuration, skipping Celery initialization") | ||
return None | ||
|
||
manager.celery = Celery("visyn", **manager.settings.visyn_core.celery) | ||
|
||
_log.info("Initializing celery app") | ||
|
||
# Discover tasks from all plugins, i.e. visyn_core.tasks, visyn_plugin.tasks | ||
manager.celery.autodiscover_tasks([p.id for p in plugins]) | ||
|
||
return manager.celery | ||
|
||
|
||
def create_celery_worker_app(*, workspace_config: dict | None = None): | ||
""" | ||
Create a new Celery app in standalone mode, i.e. without a FastAPI instance. | ||
""" | ||
|
||
from ..settings.constants import default_logging_dict | ||
|
||
# Initialize the logging very early as otherwise the already created loggers receive a default loglevel WARN, leading to logs not being shown. | ||
logging.config.dictConfig(default_logging_dict) | ||
|
||
from ..server.utils import init_settings_manager | ||
|
||
# As we are in standalone mode, we need to initialize the settings manager ourselves (which otherwise happens in the create_visyn_server) | ||
# We don't initialize anything else (i.e. no registry, no endpoints, ...) as we don't need them in the worker. | ||
plugins = init_settings_manager(workspace_config=workspace_config) | ||
|
||
_log = logging.getLogger(__name__) | ||
_log.info("Starting celery worker") | ||
|
||
return init_celery_manager(plugins=plugins) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import pathlib | ||
|
||
from visyn_core.celery.app import create_celery_worker_app | ||
|
||
celery_app = create_celery_worker_app(workspace_config={"_env_file": pathlib.Path(__file__).parent.parent / ".env"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import os | ||
import pathlib | ||
import sys | ||
|
||
from .server.visyn_server import create_visyn_server | ||
|
||
# This app is either started via the uvicorn runner in __main__.py, | ||
# or as module to execute commands via `python -m <app>.dev_app db-migration exec ...` | ||
app = create_visyn_server( | ||
start_cmd=" ".join(sys.argv[1:]), workspace_config={"_env_file": os.path.join(os.path.dirname(os.path.realpath(__file__)), ".env")} | ||
) | ||
app = create_visyn_server(start_cmd=" ".join(sys.argv[1:]), workspace_config={"_env_file": pathlib.Path(__file__).parent / ".env"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from fastapi.testclient import TestClient | ||
|
||
from visyn_core import manager | ||
|
||
|
||
def test_celery_worker(client: TestClient): | ||
|
||
@manager.celery.task | ||
def add(x, y): | ||
return x + y | ||
|
||
result = add.delay(2, 4) | ||
assert result.get(timeout=1) == 6 |