简体   繁体   中英

Assign a URL to a python script to run a cron job on Google Cloud?

I have a simple Django app that displays some data it pulls from a database. I have a Python script that updates the data and I would like it to run every day. I'm having trouble figuring out how to format my app.yaml handlers so they respond to the cron.yaml.

cron.yaml looks like this:

cron:
- description: "daily update"
  url: /go
  schedule: every 24 hours

the app.yaml looks like this (minus some sensitive parts):

runtime: python
entrypoint: gunicorn -b :$PORT rcg_new.wsgi # specific to a GUnicorn HTTP server deployment
env: flex

handlers:
- url: /static
  static_dir: static
- url: /go
  script: scripto.app

runtime_config:
  python_version: 3

the script looks like this:

import pandas as pd
import numpy as np
import requests
import webapp2
import urllib2
import urllib3
import certifi
import json
import re
from rcg_func_dj import pull_rc, pull_artists, artist_cycle, tally

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "rcg_new.settings")
import django
django.setup()
from rcg_app.models import Gender, Groups

http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
spot_id = os.environ['SPOT_ID']
spot_sec = os.environ['SPOT_SEC']

class update_dbs(webapp2.RequestHandler):
    def get(self):

        self.response.headers["Content-Type"] = "text/html"
        self.response.write('running rcg...')
        rc = pull_rc(spot_id, spot_sec)
        artists_unprocessed = pull_artists(rc)
        artists_processed = artist_cycle(artists_unprocessed)
        tally(artists_processed)
        self.response.write('rcg done!')


routes = [('/go', update_dbs)]
app = webapp2.WSGIApplication(routes, debug=True) 

the script works, as i used it to populate the db in the first place!

it shows up fine in the task queue, but gives me a "failed" status when i try to run it. nothing shows up in the logs though.

I never got this work through webapp and app.yaml, but I did find a workaround: I created a new view in the Django app that ran the script.

This took a while to figure out!

The tricky part was the cron job was making a call that involved a redirect from something like "url/script/" to "url/script", and it would only make the initial call and not the redirect.

So when you define your path in the 'urls.py' file of the Django app, make sure you don't put a trailing slash!

it should look like this:

urlpatterns = [
    path('script', views.script, name='script')
]

... NOT like this:

urlpatterns = [
    path('script/', views.script, name='script')
]

While the cron.yaml looks like this:

cron:
- description: "daily update"
  url: /app/script/
  schedule: every 24 hours

I still don't entirely understand why this worked. Smarter people than me, follow up if you have notes.

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