django-crontab icon indicating copy to clipboard operation
django-crontab copied to clipboard

task hasn't log anything,which longging was configured in Django setting file

Open MakingL opened this issue 6 years ago • 6 comments

Environment & Versions

  • Operating system: Ubuntu 18.04 LTS
  • Python: Python 3.7.4
  • Django: django=2.2.4
  • django-crontab: django-crontab=0.7.1

Settings

  • My django-crontab settings:
CRONJOBS = (
    ('*/5 * * * *', 'whatsApp_crawl_query_app.task.notify_expired_worker'),
)

Details

my task setting in whatsApp_crawl_query_app.task.py file is shown as following:

import logging
logger = logging.getLogger('background_task')

def notify_expired_worker():
       # my task executing code here
       logger.info("Email notified worker: {} expire".format(worker_id))

The logging is setted in Django setting file. The configured log is usually recorded, but the task running shows it didn't log anything in logging file.

I am sure that the task code was actually executed, because the negative events in my task code did occur.

MakingL avatar Nov 07 '19 09:11 MakingL

Did your problem get solved already?

lynoure avatar Nov 20 '20 16:11 lynoure

Did your problem get solved already?

No yeat, I tried to solve this, but I haven't found a propoerty solution, and I just moved logging point to other py function which weren't called by django-crontab task. Mybe it is helpful setting the logging save path to absolute path, since I guess the django-crontab task is run in a process wich have a different pid and working directory with the Django process.

MakingL avatar Nov 21 '20 13:11 MakingL

I'm having this problem even when I'm not logging to a file, but to shell and azure through opencensus. I'm starting to think that print statements and redirecting the output&error is the most reliable way of logging from django-crontab tasks but that would be a bit heartbreaking.

lynoure avatar Nov 24 '20 11:11 lynoure

Maybe this comment solves your problem: https://github.com/kraiz/django-crontab/issues/83#issuecomment-395753733

there was the problem, that python logger writes its output to stderr and not stdout. so you need to redirect stderr to stdout by appending 2>&1 to the command

Bergiu avatar Apr 03 '21 19:04 Bergiu

Maybe this comment solves your problem: #83 (comment)

there was the problem, that python logger writes its output to stderr and not stdout. so you need to redirect stderr to stdout by appending 2>&1 to the command

Thanks for your reply@Bergiu. I think this comment is still different from the problem I described. I want to write the logging file as the Django setting file configured, but I find that the path to the file I write to is not what I set. I think it seems that crontab forked another process to perform a timed task, and the working directory of that process is not the same as the working directory of my django process. I think one solution is that when crontab writes the log file, the file path should be specified as an absolute path, not a relative path, but this method may involve synchronization between multiple processes.

MakingL avatar Apr 08 '21 01:04 MakingL

You are right about it being a different working directory, as django-crontab creates the crontab entries and cron still runs them. That's why directing output from stdout and stderr and using an absolute path is almost the best practice solution. You can additionally specify an absolute path for the working directory directly in the crontab file, by adding a HOME= line if there is a single working directory that fits all the cron jobs in that file

lynoure avatar Apr 10 '21 19:04 lynoure