0

I would like to use a cron task in order to delete media files if the condition is True.

Users generate export files stored in the Media folder. In order to clean export files in the background, I have a Cron task which loops over each file and looks if the expiry delay is passed or not.

I used django-cron library

Example:

File in Media Folder : Final_Products___2019-04-01_17:50:43.487845.xlsx

My Cron task looks like this :

class MyCronExportJob(CronJobBase):
    """ Cron job which removes expired files at 18:30 """
    RUN_AT_TIMES = ['18:30']

    schedule = Schedule(run_at_times=RUN_AT_TIMES)
    code = 'app.export_cron_job'

    def do(self):
        now = datetime.datetime.now()
        media_folder = os.listdir(os.path.join(settings.MEDIA_ROOT, 'exports'))
        for files in media_folder:
            file = os.path.splitext(files.split(settings.EXPORT_TITLE_SEPARATOR, 1)[1])[0]
            if datetime.datetime.strptime(file, '%Y-%m-%d_%H:%M:%S.%f') + timedelta(minutes=settings.EXPORT_TOKEN_DELAY) < now:
                os.remove(os.path.join(os.path.join(settings.MEDIA_ROOT, 'exports'), files))

# settings.EXPORT_TOKEN_DELAY = 60 * 24

I edited my crontab -e :

30 18 * * * source /home/user/Bureau/Projets/app/venv/bin/activate.csh && python /home/user/Bureau/Projets/app/src/manage.py runcrons --force app.cron.MyCronExportJob

Then I launched service cron restart

But nothing as changed. My file is still there. However, it should be removed because his date is greater than now + settings.EXPORT_TOKEN_DELAY

I'm using Ubuntu to local dev and FreeBSD as a production server environment.

EDIT:

I tried some things but crontab doesn't work for the moment.

1) * * * * * /bin/date >> /home/user/Bureau/Projets/app/cron_output ==> It works, so crontab works

2) I ran : python manage.py runcrons in my console ==> It works

3) I ran this script (cron.sh):

source /home/user/.bashrc
cd /home/user/Bureau/Projets/app
pyenv activate app

python src/manage.py runcrons --force

deactivate

==> It works

4) I ran this crontab line :

35 10 * * * /home/user/Bureau/Projets/app/utility/cron.sh

==> Service restarted at 10h32, I waited until 10h38 : nothing !

Essex
  • 6,042
  • 11
  • 67
  • 139
  • This job will run at 6:30 pm once per day. When did you restart your cron service? – John Gordon Apr 11 '19 at 16:36
  • Note you shouldn't need to activate the virtualenv in the crontab - just use the python in the virtualenv, e.g. `30 18 * * * /path/to/virtualenv/bin/python manage.py runcrons --force app.cron.MyCronExportJob`. Or add `/path/to/virtualenv/bin/` to `PATH` at the top of the crontab, then use `30 18 * * * python manage.py runcrons --force app.cron.MyCronExportJob`. – Alasdair Apr 11 '19 at 16:40
  • @JohnGordon As I'm testing in local, at 18:25 for example I configure cron.py file + crontab -e to 18:30 and I launch immediately `service cron restart`. – Essex Apr 11 '19 at 16:40
  • And at 18:31 nothing has changed? – John Gordon Apr 11 '19 at 16:43
  • @JohnGordon Nothing at 18:31 – Essex Apr 11 '19 at 16:49
  • @Alasdair I tried to write my cron like this : `30 18 * * * /home/my_name/.pyenv/versions/3.6.2/envs/app/bin/python manage.py runcrons --force app.cron.MyCronExportJob` but nothing has changed. – Essex Apr 11 '19 at 16:50
  • Yes, that was just a suggestion to simplify the cron command, it wasn't meant to fix the issue. – Alasdair Apr 11 '19 at 16:51
  • Are you sure your machine is using your local timezone and not UTC? – Alasdair Apr 11 '19 at 16:51
  • How I can check that ? In django settings I have : USE_TZ = False TIME_ZONE = 'Europe/Paris' – Essex Apr 11 '19 at 16:53
  • Those are the Django settings - run `date` to check the sytem's time and timezone. If it's on UTC, the you'll see something like `Thu 11 Apr 17:02:47 UTC 2019`, and your 18:30 cron job should run in about an hour and a half. – Alasdair Apr 11 '19 at 17:02
  • As a first step in debugging this problem, add some print messages to the `do()` function. Put one at the very top of the function to confirm that it runs, and also one at the top of every loop and if/else statement, so you know exactly what path the code is following. – John Gordon Apr 11 '19 at 19:08
  • @Alasdair date gives me the exact datetime. For example date launched at 09:19 gives me `vendredi 12 avril 2019, 09:18:35 (UTC+0200)`. – Essex Apr 12 '19 at 07:19
  • @JohnGordon I will try to do that and see if I can print. But How I can see if the issue is not the cron system himself ? – Essex Apr 12 '19 at 07:20
  • That's why I suggested the print message at the very top of the function, so you can verify that the code is executed via cron. – John Gordon Apr 12 '19 at 15:35

0 Answers0