简体   繁体   中英

Google app engine python cron: What is the testcron.py doing in the app.yaml file?

Here's a sample code for gae python cron job.

app.yaml

- url: /testcron
  script: testcron.py
  login: admin

- url: .*
  script: main.app

cron.yaml

cron:
- description: testcron
  url: /testcron
  schedule: every 12 hours

main.py

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.request.write('hello')

class TestCronHandler(webapp2.RequestHandler):
    def get(self):
        logging.info('hello')


app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/testcron',TestCronHandler)
], debug=True)

Okay, so I've noticed that you do not need script: testcron.py in the app.yaml file. But if you put it there, it is required to run another application.

So what is the purpose of that? I mean you can run it in TestCronHandler. What is the difference between running in main.app handler and running in testcron.py? (It seems like you need to run another app with testcron.py)

It's not really clear what you mean by

if you put it there, it is required to run another application

and

It seems like you need to run another app with testcron.py

If you mean you need to define another app = webapp2.WSGIApplication inside taskcron.py then yes, that is true but it looks like you don't have the testcron.py file at all and your main.py is handling requests to /testcron so your app.yaml could look as simple as:

- url: .*
  script: main.app

It's up to you whether you want to separate (for better code organization?) cron requests/handlers into a separate file but note that if you have a file called taskcron. py it would need to appear in app.yaml as "script: taskcron. app ".

Also, an important security note, if you want to make sure that regular users cannot access your cron task URLs then you should definitely move your cron task handlers into a separate file and keep the login: admin line below script: testcron.app inside app.yaml .

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