简体   繁体   English

Google App Engine(Python)中的简单Cron作业

[英]Simple cron job in google app engine (python)

I need help in implementing a simple cron job in GAE (python). 我需要在GAE(python)中实现简单的cron作业的帮助。

According to what I understood from appengine documentation, I made a file cron.yaml in the application root directory with the following content: 根据我从appengine文档中了解的信息,我在应用程序根目录中制作了一个文件cron.yaml,内容如下:

cron:
- description: blah blah
  url: /crontask
  schedule: every 1 minute

And my app.yaml file has the following content: 我的app.yaml文件具有以下内容:

application: template-123
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: template-123.app

I made all my application code (cron and other parts) into a single file "template-123.py". 我将所有应用程序代码(cron和其他部分)制作到一个文件“ template-123.py”中。 In the code I implement cron in the following way: 在代码中,我通过以下方式实现cron:

class CronTask(Handler):

    def  post(self):
        i=25
        number = Birthday(day = i)
        number.put()

And I tell the code to use this class for cron by stating: ('/crontask', CronTask). 我通过声明:('/ crontask',CronTask)告诉代码将此类用于cron。

However no new entries are uploaded to the datastore (as I believe they should be). 但是,没有新条目上载到数据存储区(我相信应该如此)。 And I know that it isn't a problem with the way I'm accessing the data store because when I try to do the same thing manually (upload entries to the data store in my non-cron part of application) it returns with appropriate results. 而且我知道访问数据存储的方式没有问题,因为当我尝试手动执行相同的操作(将条目上载到应用程序非克隆部分的数据存储中)时,它将以适当的方式返回结果。

So I need some guidance as to what might I be doing wrong or missing? 因此,对于我可能做错了什么或想念什么,我需要一些指导。 Do have to make some more changes to the yaml files or add some other libraries? 是否需要对yaml文件进行更多更改或添加其他库?

Cron uses GET, not POST. Cron使用GET,而不是POST。 Change your def post(self) to def get(self) (or whatever else is appropriate). def post(self)更改为def get(self) (或其他合适的方法)。

Usually python files end in py. 通常python文件以py结尾。 the handler should say 经理应该说

script: template-123.py

Test your cron job locally first. 首先在本地测试您的cron作业。 You should be able to access localhost:8000/_ah/admin, and click on the Cron Jobs link on the left. 您应该能够访问localhost:8000 / _ah / admin,然后单击左侧的Cron Jobs链接。 It should list all your cron jobs, and you should be able to test them with a link on the page. 它应该列出您所有的cron作业,并且您应该能够通过页面上的链接对其进行测试。 Debug on dev_appserver first. 首先在dev_appserver上进行调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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