feat: start celery worker programmatically (#2988)

### Description

Allow us to be more flexible with the option passed the celery worker,
for example, to change the logging level.
This commit is contained in:
Jonas L 2024-04-13 21:03:57 +02:00 committed by GitHub
parent 7040d0e4bd
commit 9c548b365e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 35 additions and 16 deletions

View File

@ -201,14 +201,7 @@ RUN --mount=type=cache,target=/root/.cache/pip \
USER ${UID}:${GID} USER ${UID}:${GID}
WORKDIR /app WORKDIR /app
CMD ["/usr/local/bin/celery", "worker", \ CMD ["/usr/local/bin/libretime-worker"]
"--app=libretime_worker.tasks:worker", \
"--config=libretime_worker.config", \
"--beat", \
"--time-limit=1800", \
"--concurrency=1", \
"--loglevel=info"]
ARG LIBRETIME_VERSION ARG LIBRETIME_VERSION
ENV LIBRETIME_VERSION=$LIBRETIME_VERSION ENV LIBRETIME_VERSION=$LIBRETIME_VERSION

View File

@ -22,14 +22,7 @@ Environment=LIBRETIME_CONFIG_FILEPATH=@@CONFIG_FILEPATH@@
Environment=LIBRETIME_LOG_FILEPATH=@@LOG_DIR@@/worker.log Environment=LIBRETIME_LOG_FILEPATH=@@LOG_DIR@@/worker.log
WorkingDirectory=@@WORKING_DIR@@/worker WorkingDirectory=@@WORKING_DIR@@/worker
ExecStart=/usr/bin/sh -c '@@VENV_DIR@@/bin/celery worker \ ExecStart=/usr/local/bin/libretime-worker
--app=libretime_worker.tasks:worker \
--config=libretime_worker.config \
--beat \
--time-limit=1800 \
--concurrency=1 \
--loglevel=INFO \
--logfile=$LIBRETIME_LOG_FILEPATH'
Restart=always Restart=always
User=libretime User=libretime

View File

@ -0,0 +1,28 @@
from pathlib import Path
from typing import Optional
import click
from libretime_shared.cli import cli_logging_options
from libretime_shared.config import DEFAULT_ENV_PREFIX
from .config import __name__ as config_module
from .tasks import worker
@click.command(context_settings={"auto_envvar_prefix": DEFAULT_ENV_PREFIX})
@cli_logging_options()
def cli(log_level: str, log_filepath: Optional[Path]):
"""
Run celery.
"""
args = [
f"--config={config_module}",
"--beat",
"--time-limit=1800",
"--concurrency=1",
f"--loglevel={log_level}",
]
if log_filepath is not None:
args.append(f"--logfile={log_filepath}")
worker.worker_main(args)

View File

@ -15,6 +15,11 @@ setup(
}, },
license="MIT", license="MIT",
packages=find_packages(exclude=["*tests*", "*fixtures*"]), packages=find_packages(exclude=["*tests*", "*fixtures*"]),
entry_points={
"console_scripts": [
"libretime-worker=libretime_worker.main:cli",
]
},
python_requires=">=3.8", python_requires=">=3.8",
install_requires=[ install_requires=[
"celery==4.4.7", "celery==4.4.7",