initial commit

This commit is contained in:
2021-07-28 02:15:48 +03:00
commit 735633853a
6607 changed files with 1084121 additions and 0 deletions

View File

@@ -0,0 +1 @@
!.env

View 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/

View 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`

View File

@@ -0,0 +1,3 @@
# from app_celery import app as my_celery_app
#
# __all__ = ('my_celery_app', )

View 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://',
)

View File

@@ -0,0 +1,6 @@
# RabbitMQ settings:
RABBITMQ_DEFAULT_USER=rabbit_admin
RABBITMQ_DEFAULT_PASS=mypass
RABBITMQ_PORT=5672
RABBITMQ_HOST=rabbitmq_host

View 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

View 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

View File

@@ -0,0 +1,2 @@
celery==5.0.2
python-decouple==3.3