简体   繁体   中英

django_cron config when .ebextensions is not in root directory

There are a few solutions to configuring .ebextension container commands for cronjobs but none of them are working for me.

I am concerned that the reason they aren't working is because .ebextensions is not in the root directory. This messy code was handed over to me and I've tried to move .ebextensions to where it needs to be but that seems to break everything else.

This app is a streaming video application currently in production and I can't afford to break it so I ended up just leaving it where it is.

Can someone tell if I am doing this right and I just need to find a way to move .ebextensions or is the problem in my cronjob configuration?

app1/.ebextensions/02_python.config

container_commands:
  ...
  cronjob:
    command: "echo .ebextensions/cronjobs.txt > /etc/cron.d/cronjobs && 644 /etc/cron.d/cronjobs"
    leader_only: true
  ...

app1/.ebextensions/cronjobs.txt

***** root source /opt/python/run/venv/bin/activate && python3 manage.py runcrons > /var/log/cronjobs.log

app1/settings.py

INSTALLED_APPS = [
    ...
    'django_cron',
    ...
]

CRON_CLASSES = [
    'app2.crons.MyCronJob',
]

app2/crons

from django_cron import CronJobBase, Schedule

class MyCronJob(CronJobBase):

    RUN_EVERY_MINS = 1
    schedule = Schedule(run_every_mins=RUN_EVERY_MINS)

    def do(self):
        # calculate stuff
        # update variables

This deploys to AWS elastic beanstalk without error and logs show it's run but the work doesn't get done and it only runs the command once on deploy. Logs show this.

Command cronjob] : Starting activity...
  [2018-02-15T12:58:41.648Z] INFO  [24604] - [Application update     ingest16@207/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_api_backend/Test for Command 05_cronjob] : 

Completed activity. Result:

Completed successfully

This does the job but only once on deploy.

container_commands:
  ...
  cronjob:
    command: "source /opt/python/run/venv/bin/activate && python3 manage.py runcrons"
    leader_only: true
  ...

This doesn't work at all.

container_commands:
  ...
  cronjob:
    command: "echo /app1/.ebextensions/cronjobs.txt > /etc/cron.d/cronjobs && 644 /etc/cron.d/cronjobs"
    leader_only: true
  ...

Hi why using django_cron, when you only need cron ?

Here is my config .ebextensions:

container_commands:
  ...
  0.0.1.cron.mailing:
    command: "cat .ebextensions/mailing.txt > /etc/cron.d/mailing && chmod 644 /etc/cron.d/mailing"
    leader_only: true

Here my mailing.txt:

Every Morning at 05:00am

#* * * * * * command
#| | | | | | |
#| | | | | | + Comande Line
#| | | | | +-- Year              (range: 1900-3000)
#| | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)
#| | | +------ Month of the Year (range: 1-12)
#| | +-------- Day of the Month  (range: 1-31)
#| +---------- Hour              (range: 0-23)
#+------------ Minute            (range: 0-59)

# m h dom mon dow command
0 5 * * * root source /opt/python/run/venv/bin/activate && source /opt/python/current/env && cd /opt/python/current/app/ && python manage.py  my_command >> /home/ec2-user/cron-mailing.log 2>&1

And here how to create custom command : https://docs.djangoproject.com/en/2.0/howto/custom-management-commands/#module-django.core.management

Hope this help,

You need space in your cron file between * :

Your cronfile :

***** root source /opt/python/run/venv/bin/activate && python3 manage.py runcrons > /var/log/cronjobs.log

Fix it like that :

* * * * * root source /opt/python/run/venv/bin/activate && python3 manage.py runcrons > /var/log/cronjobs.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM