mirror of
https://github.com/Balshgit/public.git
synced 2026-02-04 10:00:39 +03:00
github stars project celery-rabbit
This commit is contained in:
1
celery-rabbit-example/.gitignore
vendored
Normal file
1
celery-rabbit-example/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
!.env
|
||||
34
celery-rabbit-example/Dockerfile
Normal file
34
celery-rabbit-example/Dockerfile
Normal file
@@ -0,0 +1,34 @@
|
||||
FROM python:3.8.6-buster
|
||||
|
||||
ENV PYTHONFAULTHANDLER=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
PYTHONHASHSEED=random \
|
||||
PYTHONDONTWRITEBYTECODE=1 \
|
||||
# pip:
|
||||
PIP_NO_CACHE_DIR=off \
|
||||
PIP_DISABLE_PIP_VERSION_CHECK=on \
|
||||
PIP_DEFAULT_TIMEOUT=100
|
||||
|
||||
RUN apt-get update \
|
||||
&& apt-get install --no-install-recommends -y \
|
||||
bash \
|
||||
build-essential \
|
||||
curl \
|
||||
gettext \
|
||||
git \
|
||||
libpq-dev \
|
||||
nano
|
||||
|
||||
WORKDIR /code
|
||||
|
||||
# Copy and install dependencies:
|
||||
COPY requirements.txt /code/
|
||||
RUN python -m pip install --upgrade pip
|
||||
RUN pip install --no-cache-dir -r /code/requirements.txt
|
||||
|
||||
# Copy source files:
|
||||
COPY . /code/
|
||||
# COPY app.py /code/
|
||||
|
||||
|
||||
|
||||
6
celery-rabbit-example/README.md
Normal file
6
celery-rabbit-example/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# celery first example
|
||||
|
||||
Steps:
|
||||
1. Run `docker-compose up`
|
||||
2. Show logs
|
||||
3. In a new terminal run `docker-compose exec worker python`
|
||||
3
celery-rabbit-example/celery_config/__init__.py
Normal file
3
celery-rabbit-example/celery_config/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# from app_celery import app as my_celery_app
|
||||
#
|
||||
# __all__ = ('my_celery_app', )
|
||||
25
celery-rabbit-example/celery_config/app_celery.py
Normal file
25
celery-rabbit-example/celery_config/app_celery.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from celery import Celery
|
||||
from pathlib import Path
|
||||
from decouple import AutoConfig
|
||||
|
||||
BASE_DIR = Path.cwd().parent
|
||||
config = AutoConfig(search_path=BASE_DIR.joinpath('config'))
|
||||
|
||||
|
||||
RABBITMQ_DEFAULT_USER = config('RABBITMQ_DEFAULT_USER')
|
||||
RABBITMQ_DEFAULT_PASS = config('RABBITMQ_DEFAULT_PASS')
|
||||
RABBITMQ_PORT = config('RABBITMQ_PORT', cast=int, default=5672)
|
||||
RABBITMQ_HOST = config('RABBITMQ_HOST')
|
||||
|
||||
|
||||
app_celery_instance = Celery(
|
||||
'tasks',
|
||||
broker='amqp://{login}:{password}@{host}:{port}'.format(
|
||||
login=RABBITMQ_DEFAULT_USER,
|
||||
password=RABBITMQ_DEFAULT_PASS,
|
||||
host=RABBITMQ_HOST,
|
||||
port=RABBITMQ_PORT,
|
||||
),
|
||||
# TODO: try to get async results with and without backend configured
|
||||
backend='rpc://',
|
||||
)
|
||||
6
celery-rabbit-example/config/.env
Normal file
6
celery-rabbit-example/config/.env
Normal file
@@ -0,0 +1,6 @@
|
||||
# RabbitMQ settings:
|
||||
|
||||
RABBITMQ_DEFAULT_USER=rabbit_admin
|
||||
RABBITMQ_DEFAULT_PASS=mypass
|
||||
RABBITMQ_PORT=5672
|
||||
RABBITMQ_HOST=rabbitmq_host
|
||||
28
celery-rabbit-example/docker-compose.yml
Normal file
28
celery-rabbit-example/docker-compose.yml
Normal file
@@ -0,0 +1,28 @@
|
||||
version: '3.7'
|
||||
|
||||
services:
|
||||
|
||||
rabbitmq:
|
||||
hostname: rabbitmq_host
|
||||
image: 'rabbitmq:3.8.18-management-alpine'
|
||||
container_name: first_rabbit
|
||||
env_file: config/.env
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- 8080:15672
|
||||
- 5672:5672
|
||||
|
||||
worker:
|
||||
container_name: first_celery
|
||||
build: .
|
||||
command: celery --app=my_app:app_celery_instance worker --loglevel=INFO
|
||||
env_file: config/.env
|
||||
depends_on:
|
||||
- rabbitmq
|
||||
restart: unless-stopped
|
||||
|
||||
networks:
|
||||
default:
|
||||
name: celery_network
|
||||
driver: bridge
|
||||
|
||||
14
celery-rabbit-example/my_app.py
Normal file
14
celery-rabbit-example/my_app.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from celery_config.app_celery import app_celery_instance
|
||||
|
||||
|
||||
@app_celery_instance.task
|
||||
def add(first: int, second: int) -> int:
|
||||
print(first + second)
|
||||
return first + second
|
||||
|
||||
|
||||
# TODO: try with `@app.task(throws=(ZeroDivisionError,))`
|
||||
@app_celery_instance.task
|
||||
def div(first: int, second: int) -> float:
|
||||
# TODO: show how errors work
|
||||
return first / second
|
||||
2
celery-rabbit-example/requirements.txt
Normal file
2
celery-rabbit-example/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
celery==5.0.2
|
||||
python-decouple==3.3
|
||||
Reference in New Issue
Block a user