kraiz / django-crontab

dead simple crontab powered job scheduling for django.
Other
842 stars 112 forks source link

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

Open MakingL opened 4 years ago

MakingL commented 4 years ago

Environment & Versions

Settings

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.

lynoure commented 3 years ago

Did your problem get solved already?

MakingL commented 3 years ago

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.

lynoure commented 3 years ago

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.

Bergiu commented 3 years ago

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

MakingL commented 3 years ago

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.

lynoure commented 3 years ago

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