Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ POSTGRES_DB=djangopackages
POSTGRES_PASSWORD=djangopackages
POSTGRES_USER=djangopackages
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_PORT=5432
CELERY_FLOWER_USER=flower_user
CELERY_FLOWER_PASSWORD=flower_pwd
MAILGUN_API_KEY=mailgun_api_key
MAILGUN_SENDER_DOMAIN=mailgun_sender_domain
SECRET_KEY=secret_key
1 change: 1 addition & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
DEBUG: false
REDIS_URL: 'redis://redis:6379/0'
SECRET_KEY: 'this-is-for-testing-only'
CELERY_BROKER_URL: 'redis://redis:6379/0'

run: |
python -m pytest .
Expand Down
12 changes: 12 additions & 0 deletions compose/django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ COPY ./compose/django/entrypoint.sh /entrypoint.sh
RUN sed -i 's/\r//' /entrypoint.sh
RUN chmod +x /entrypoint.sh

COPY ./compose/django/celery/worker/start.sh /start-celeryworker.sh
RUN sed -i 's/\r//' /start-celeryworker.sh
RUN chmod +x /start-celeryworker.sh

COPY ./compose/django/celery/beat/start.sh /start-celerybeat.sh
RUN sed -i 's/\r//' /start-celerybeat.sh
RUN chmod +x /start-celerybeat.sh

COPY ./compose/django/celery/flower/start.sh /start-flower.sh
RUN sed -i 's/\r//' /start-flower.sh
RUN chmod +x /start-flower.sh

COPY . /app
RUN chown -R django /app

Expand Down
12 changes: 12 additions & 0 deletions compose/django/Dockerfile-dev
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ COPY ./compose/django/start-dev.sh /start-dev.sh
RUN sed -i 's/\r//' /start-dev.sh
RUN chmod +x /start-dev.sh

COPY ./compose/django/celery/worker/start.sh /start-celeryworker.sh
RUN sed -i 's/\r//' /start-celeryworker.sh
RUN chmod +x /start-celeryworker.sh

COPY ./compose/django/celery/beat/start.sh /start-celerybeat.sh
RUN sed -i 's/\r//' /start-celerybeat.sh
RUN chmod +x /start-celerybeat.sh

COPY ./compose/django/celery/flower/start.sh /start-flower.sh
RUN sed -i 's/\r//' /start-flower.sh
RUN chmod +x /start-flower.sh


WORKDIR /app

Expand Down
8 changes: 8 additions & 0 deletions compose/django/celery/beat/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

set -o errexit
set -o nounset


rm -f './celerybeat.pid'
celery -A settings.celery_app beat -l INFO
11 changes: 11 additions & 0 deletions compose/django/celery/flower/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

set -o errexit
set -o nounset


celery \
-A settings.celery_app \
-b "${CELERY_BROKER_URL}" \
flower \
--basic_auth="${CELERY_FLOWER_USER}:${CELERY_FLOWER_PASSWORD}"
8 changes: 8 additions & 0 deletions compose/django/celery/worker/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

set -o errexit
set -o nounset


# watchgod celery.__main__.main --args -A celery_app worker -l INFO
celery -A settings.celery_app worker -l INFO
1 change: 1 addition & 0 deletions compose/django/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ done

export REDIS_URL=redis://redis:6379/0
export DATABASE_URL=postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:$POSTGRES_PORT/$POSTGRES_DB
export CELERY_BROKER_URL="${REDIS_URL}"
exec $cmd
30 changes: 29 additions & 1 deletion dev.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.6'

services:
django:
django: &django
build:
context: .
dockerfile: ./compose/django/Dockerfile-dev
Expand Down Expand Up @@ -29,6 +29,34 @@ services:
redis:
build: ./compose/redis

celeryworker:
<<: *django
image: django_packages_dev_celeryworker
container_name: celeryworker
depends_on:
- redis
- postgres
ports: []
command: /start-celeryworker.sh

celerybeat:
<<: *django
image: django_packages_dev_celerybeat
container_name: celerybeat
depends_on:
- redis
- postgres
ports: []
command: /start-celerybeat.sh

flower:
<<: *django
image: django_packages_dev_flower
container_name: flower
ports:
- "5555:5555"
command: /start-flower.sh

volumes:
postgres_backup_dev: {}
postgres_data_dev: {}
Expand Down
9 changes: 9 additions & 0 deletions package/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import logging

class PackageUpdaterException(Exception):
def __init__(self, error, title):
log_message = "For {title}, {error_type}: {error}".format(
title=title, error_type=type(error), error=error
)
logging.critical(log_message)
logging.exception(error)
22 changes: 2 additions & 20 deletions package/management/commands/package_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@
from github3 import login as github_login

from package.models import Package
from package.tasks import update_package_task
from core.utils import healthcheck

logger = logging.getLogger(__name__)


class PackageUpdaterException(Exception):
def __init__(self, error, title):
log_message = "For {title}, {error_type}: {error}".format(
title=title, error_type=type(error), error=error
)
logging.critical(log_message)
logging.exception(error)


class Command(BaseCommand):

help = "Updates all the packages in the system. Commands belongs to django-packages.package"
Expand All @@ -39,17 +31,7 @@ def handle(self, *args, **options):
sleep(120)
break

try:
try:
package.fetch_metadata(fetch_pypi=False)
package.fetch_commits()
except Exception as e:
logger.error(
f"Error while fetching package details for {package.title}."
)
raise PackageUpdaterException(e, package.title)
except PackageUpdaterException:
logger.error(f"Unable to update {package.title}", exc_info=True)
update_package_task.delay(package.id)

print(f"{__file__}::handle::sleep(5)")
sleep(5)
Expand Down
26 changes: 26 additions & 0 deletions package/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from settings import celery_app
from celery.utils.log import get_task_logger

from .exceptions import PackageUpdaterException
from .models import Package


logger = get_task_logger(__name__)


@celery_app.task()
def update_package_task(package_id):
package = Package.objects.get(id=package_id)
logger.info(f'Start updating package {package.id}')

try:
try:
package.fetch_metadata(fetch_pypi=False)
package.fetch_commits()
except Exception as e:
logger.error(
f"Error while fetching package details for {package.title}."
)
raise PackageUpdaterException(e, package.title)
except PackageUpdaterException:
logger.error(f"Unable to update {package.title}", exc_info=True)
5 changes: 5 additions & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ pytest-django
# utils
bumpver
six

# celery
celery==5.1.2
django-celery-beat==2.2.1
flower==1.0.0
Loading