2

I've been working on this all week, and I don't seem to understand what I'm missing. The problem is simple, I've a container running a platform on Django, and I need to create a Cronjob for an smaller task, I created a test ask that executes every minute and just print a log just to test, but it is not working, while it installs cron, add the cronjobs, start the cron service, and I can see them in the crontab, they are just never triggered.

When I first started, I had the Cron running in the same instance, but after reading this Question I found that I had to separate it in 2 instances, since apparently having Django running was afecting the cron service, so following that, this is how I have my files:

docker-compose.yml

version: '3'

services:

    auth:
        build:
            context: ./
            dockerfile: ./devops/Dockerfile
            args:
                [Bunch of parameters]
        container_name: auth
        volumes:
            - ./project:/app
        ports:
            - 8000:8000
        environment:
            [Bunch of parameters]
        command: python manage.py runserver 0.0.0.0:8000

    cron:
        build:
            context: ./
            dockerfile: ./devops/Dockerfile
            args:
                [Bunch of parameters]
        container_name: cron
        volumes:
            - ./project:/app
        environment:
            [Bunch of parameters]
        command: cron -f

Dockerfile

FROM python:3.8

ENV PYTHONUNBUFFERED 1

WORKDIR /app

COPY ./devops/requirements.txt .

COPY ./project .

# COPY ./.env .

RUN apt-get update

RUN apt-get -y install cron

RUN cp ./.env . || echo "file not found"

RUN pip install -r requirements.txt

#Set permission to entrypoint.sh
RUN chmod +x entrypoint.sh

# start web server
ENTRYPOINT ["./entrypoint.sh"]
CMD ["gunicorn", "-b", "0.0.0.0:8000", "project.wsgi:application", "--workers=5"]

entrypoint.sh

#!/bin/sh

# Set up scheduled jobs, if this is the cron container.
if [ "$1" = cron ]; then
    service cron start
    python ./manage.py crontab add
    service cron stop
fi

# Run whatever command we got passed.
exec "$@"

settings.py

CRONJOBS = [
    ('*/1 * * * *', 'apps.coupons.cron.test'),
]

cron.py

import logging

logger = logging.getLogger(__name__)

def test():
    logger.warning('Hello World')
    logger.debug('Hello World')
    logger.info('Hello World')
    logger.error('Hello World')
    logger.critical('Hello World')
    print("Hello World")
    return "Finished"

Here you can see that the cron were added, and that cron is running, and that executing the cronjob manually works.

Job added

Cron is running

Still, doesn't matter how long I wait, it doesn't seem like the Cronjob runs automatically every minute (I check this by using the file.log that I setted in the settings.py logger's options). What am I doing wrong or what may be missing to make the cron work?

Efraín
  • 453
  • 1
  • 7
  • 13
  • I may have misunderstood something, but the container log is the output from the main process in the container. The process started by cron is not the main process, so the output from that will not be visible in the container log. Try writing to a file instead and see if you get some output there. – Hans Kilian Jun 17 '22 at 18:46
  • @HansKilian As I mentioned in the post, the way I check this is by checking file.log, something I configured with LOGGING var in settings.py for the logger to output there, and when I run it manually, I can see the Hello Worlds appear in it, so I know it should write there. I'm not sure if this is correct, or if may be failing to understand something. – Efraín Jun 17 '22 at 19:50

0 Answers0