App Engine中的Cron Job未运行

0 投票
1 回答
872 浏览
提问于 2025-04-18 11:59

在appengine上,我创建了一个定时任务,每隔2分钟就执行一次,向我的Facebook墙发布一些数据。

但是每次过了两分钟,我查看appengine的日志时,看到以下几行:

This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application.

而且我的Facebook墙上什么也没有发布。

但当我手动执行定时任务的链接(直接在浏览器中输入这个链接)时,应用就会在我的墙上发布一些内容。

这是我的 app.yaml 文件:

application: thisisreallyappname
version: 1
runtime: python27
api_version: 1
threadsafe: yes

builtins:
- remote_api: on

inbound_services:
- warmup

libraries:
- name: django
  version: latest

handlers:
- url: /_ah/queue/deferred
  script: djangoappengine.deferred.handler.application
  login: admin

- url: /_ah/stats/.*
  script: djangoappengine.appstats.application

- url: /media/admin
  static_dir: django/contrib/admin/media
  expiration: '0'

- url: /static/admin
  static_dir: django/contrib/admin/static/admin
  expiration: '0'

- url: /static
  static_dir: static
  expiration: '0'

- url: /.*
  script: djangoappengine.main.application

这是我的 urls.py 文件:

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^crona/$', 'testapp.views.crona', name='crona'),
    url(r'^admin/', include(admin.site.urls)),
)+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

这是视图部分:

def crona(request):
   logging.info("hello")
   croned=CronEntries.objects.all().order_by('posted_datetime')[0]

   if croned.email_id=='n*******@gmail.com':
      try:
         access_token="##############################################################"
         graph = facebook.GraphAPI(access_token)
         graph.put_object("me", "feed", message=croned.status_cron)
         croned.delete()
      except Exception as e:
         logging.info(e)
      else:
         return HttpResponse("ok")

这是 cron.yaml 文件:

cron:
- description: post on fb
  url: /crona
  schedule: every 2 minutes

1 个回答

2

你的网址处理器 url(r'^crona/$', ... 需要在网址的最后加一个斜杠,但你的定时任务网址没有加这个斜杠。即使你把应用设置成自动加上这个斜杠,你也不想在定时任务或任务队列中这样做,因为你希望它们返回 200 状态,而不是 301

撰写回答