0

Hi I'm trying to add a service to my docker https://pypi.org/project/django-crontab/ unfortunately despite reading a bunch of threads on the forum I still can't manage to configure the Dockerfile and docker-compose.yaml correctly. I would like to ask for help and clarification on what I am doing wrong. Thank you for your reply

DockerFile

FROM python:3.10
RUN apt update
RUN apt-get install cron -y
RUN alias py=python
ENV PYTHONUNBUFFERED 1
WORKDIR /fieldguard
COPY . /fieldguard/
RUN pip install --upgrade pip
COPY requirments.txt /fieldguard/
RUN pip install -r requirments.txt
RUN chmod +x docker-entrypoint.sh
ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8080"]

docker_compoe.yaml

version: "3.9"

services:
  db:
    image: postgres:14.5
    restart: unless-stopped
    container_name: database
    environment:
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    ports:
      - "5432:5432"
  web:
    build: .
    container_name: webapp
    restart: unless-stopped
    ports:
      - "8000:8000"
    depends_on:
      - db
    environment:
      - SECRET_KEY=${SECRET_KEY}
      - JWT_SECRET_KEY=${JWT_SECRET_KEY}
      - FRONT_URL=${FRONT_URL}
      - EMAIL_PORT=${EMAIL_PORT}
      - EMAIL_HOST=${EMAIL_HOST}
      - EMAIL_HOST_USER=${EMAIL_HOST_USER}
      - EMAIL_HOST_PASSWORD=${EMAIL_HOST_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - WRONG_SECRET_KEY=${WRONG_SECRET_KEY}
      - DEVICE_SECRET_KEY_REGISTER=${DEVICE_SECRET_KEY_REGISTER}
      - DEVICE_SECRET_KEY_LOGIN=${DEVICE_SECRET_KEY_LOGIN}
      - TOKEN_EXPIRATION=${TOKEN_EXPIRATION}
      - DEVICE_TOKEN_EXPIRATION=${DEVICE_TOKEN_EXPIRATION}
  cron:
    build: .     # same as main application
    restart: unless-stopped
    env_file:
     - .env
    depends_on:
     - db
    command: cron -f  # as a long-running foreground process

docker-entrypoint.sh

#!/bin/sh
# docker-entrypoint.sh

# If this is going to be a cron container, set up the crontab.
if [ "$1" = cron ]; then
  python manage.py crontab add
fi

# Launch the main container command passed as arguments.
exec "$@"

settings.py

INSTALLED_APPS = [
    'rest_framework',
    'fg_backend',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_crontab',
]

CRONJOBS = [
    ('*/1 * * * *', 'fg_backend.cron.my_scheduled_job'),
]

And cron.py located in fg_backend/

from datetime import datetime, timedelta
import datetime

def my_scheduled_job():
    try:
        with open("logs/{}_{}.log".format(datetime.datetime.now().strftime('%Y-%m-%d'),
                                                        'CRONJOB'), "a+") as f:
            f.write('{}_SomeTextOnly for test'.format(datetime.datetime.now()))
            # TODO:: CREATE CRON JOB TO DELETE LOGS
            # self.delete_older_logs()
    except Exception as e:
        print(e)

I will be very grateful for your help

I would like to configure django-crontab so that I can add some small shuffles. I know Celerity is also used but I don't need such a big tool. Thank you very much for your help.

STerliakov
  • 4,983
  • 3
  • 15
  • 37
Memo
  • 1

0 Answers0