简体   繁体   中英

Python django django-crontab only runs once. How can I test it running the expected every minute?

I have installed django-crontab to the python virtual environment. I am attempting to add a row to a Google sheet daily, I know the function works so I just need a cron job to trigger it.

python 3.6.7
django 2.0.9
django-crontab 7.0.1

Below is my folder structure:

├── api
│   ├── utils
│   |    └── cron.py
|---manage.py

Here is my cron.py

# For Google API to sheets
import pygsheets

# The ID of spreadsheet.
SPREADSHEET_ID = 'abc123'

def addToGraphs():
    # Use service credentials 
    print('Attempt CRON')
    gs = pygsheets.authorize(service_account_file='credentials.json')
    spreadsheet = gs.open_by_key(SPREADSHEET_ID)
    graphsWorksheet = spreadsheet.worksheet_by_title('Graphs')
    graphsWorksheet.append_table(["29/12/2020", 1, 2, 3])
    return True

In my settings.py I have added CRONJOBS like so:

CRONJOBS = [
    ('*/1 * * * *', 'utils.cron.addToGraphs')
]

Using the commands that the documentation says, I am seemingly correctly adding the cron using: python manage.py crontab add.

And then able to list that correctly using: python manage.py crontab show

When I run it locally using: python manage.py crontab run {taskId}

The function does print out "Attempt CRON" and adds the row to the Google sheet, yay. But only once... It then just ends.

When I kick off the python django server with: python manage.py runserver

I get no print statement and the Google doc doesn't update.

What am I missing? I feel I am close as when I run it manually it works. Any advice is appreciated, thank you!

Relative paths didn't work I had to update the path to my credentials.json file to an absolute path. I changed: credentails.json to /Users/Matt/Documents/Project/Backend/api/credentials.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM