Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MakingL opened this issue Nov 7, 2019 · 6 comments

Comments

@MakingL
Copy link

MakingL commented Nov 7, 2019

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 MakingL changed the title task hasn't log anything,which longging was configured in Django setting task hasn't log anything,which longging was configured in Django setting file Nov 7, 2019
@lynoure
Copy link

lynoure commented Nov 20, 2020

Did your problem get solved already?

@MakingL
Copy link
Author

MakingL commented Nov 21, 2020

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
Copy link

lynoure commented Nov 24, 2020

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
Copy link

Bergiu commented Apr 3, 2021

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

@MakingL
Copy link
Author

MakingL commented Apr 8, 2021

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
Copy link

lynoure commented Apr 10, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants