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

Django cronjob error while using database #118

Open
SrFlipFlop opened this issue Nov 27, 2020 · 4 comments
Open

Django cronjob error while using database #118

SrFlipFlop opened this issue Nov 27, 2020 · 4 comments

Comments

@SrFlipFlop
Copy link

Environment & Versions

  • Operating system: running on Docker container (python:3.8-slim-buster) also tested in Debian 10
  • Python: 3.8
  • Django: 3.0.5
  • django-crontab: 0.7.1

Settings

  • My django-crontab settings:
CRONJOBS = [
    ('0 */4 * * *', 'app.cron.crawler_cronjob'),
]

Details

  • Output of crontab -l after running python manage.py crontab add.
0 */4 * * * /usr/local/bin/python /usr/src/app/manage.py crontab run 569660dc453e6ba975461f0d2f5f25dc # django-cronjobs for news
  • If this output contained a crontab run command and I copy this command to my terminal the job works: yes

When I run the docker-compose exec web python manage.py crontab run 569660dc453e6ba975461f0d2f5f25dc manually it works perfectly and the cronjob downloads data from a web application and stores the data on a Postgres database. But when cron executes the job it returns the following error "no such table: news_app_blog".

Searching for the same error I only found one entry in stackoverflow that doesn't have any reply and its the same error that I have.

Regards,

@tngeene
Copy link

tngeene commented Aug 19, 2021

Has anyone been able to fix this? The package seems unmaintained.

@SrFlipFlop
Copy link
Author

Hi tngeene,

I changed crontab and use a basic django/celery configuration to execute asynchronous tasks in the background.
Hope it works for you 😄

Regards,

@tngeene
Copy link

tngeene commented Aug 20, 2021

Thanks. It's quite unprofessional to make a package that doesn't work, even more, unprofessional not to reply to issues.

@gcmestre
Copy link

This is old, but I had the same issue today and ended up here.

In my case, the difference between running manually and running with the cronjob is the access to the env variables.

By default, crontab doesn't have access to any env variables, so they have to be added manually with CRONTAB_COMMAND_PREFIX.

For example, in my settings, if I didn't set DJANGO_ENV = production it would create a SQLite db and would not access my prod database. Because of that, I had the error, no such table.

To fix the issue I had to pass all necessary env variables to the cronjob to be able to connect to the correct database.

for example:

CRONTAB_COMMAND_PREFIX = f"DJANGO_ENV={os.getenv('DJANGO_ENV')} DBNAME={os.getenv('DBNAME')} DBHOST={os.getenv('DBHOST')} DBUSER={os.getenv('DBUSER')} DBPASS={os.getenv('DBPASS')}

Hope this helps someone :D

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