GAE-Cron作业失败,日志中没有错误消息

时间:2018-10-04 13:15:15

标签: python google-app-engine cron

我一直在尝试使用GAE(用Python开发的代码)运行cron作业,但是当我触发该作业时,它失败了,没有任何错误消息-我在日志中什么都找不到。

这是我正在使用灵活环境的服务所发生的。

这是我文件的结构:

my_service.yaml看起来像这样:

service: my_service
runtime: custom
env: flex

env_variables:
  a:
  b:

my_service.app看起来像这样:

from __future__ import absolute_import
from flask import Flask
from flask import request
import logging

import datetime
import os
import tweepy
from google.cloud import datastore
import time


logging.basicConfig(level=logging.INFO)
app = Flask(__name__)

@app.route('/Main')
def hello():
  """A no-op."""
  return 'nothing to see.'

@app.route('/my_service')
def get_service():
    is_cron = request.headers.get('X-Appengine-Cron', False)
    logging.info("is_cron is %s", is_cron)
  # Comment out the following test to allow non cron-initiated requests.
    if not is_cron:
        return 'Blocked.'
    ## data scraping and saving in Datastore

    return 'Done.'

@app.errorhandler(500)
def server_error(e):
    logging.exception('An error occurred during a request.')
    return """
    An internal error occurred: <pre>{}</pre>
    See logs for full stacktrace.
    """.format(e), 500


if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8080, debug=True)

然后我有一个dispatch.yaml,其结构如下:

dispatch:

  - url: "*/my_service*"
    service: my_service

还有一个cron.yaml

cron:

- description: run my service
  url: /my_service
  schedule: 1 of month 10:00
  target: my_service

不知道我在做什么错了。

编辑

一些上下文。我正在从this repo开始对此进行编辑。

其中定义的名为backend的服务可以完美运行(它在cron作业中的时间表与my_service相同,但是当我在一天中触发它的时间与按计划,效果很好)。我要做的是用自己的yaml文件创建一个附加服务,该文件看起来与beckend.yaml,它自己的my_service.py完全相同,并将其添加到dispacth.yaml和{{1 }}。从理论上讲,这应该起作用,因为结构完全相同,但事实并非如此。

此服务最初是在标准环境中开发的,并且可以正常工作,问题出在我将其移至Flex环境时。

编辑2:

问题实际上出在Dockerfile中,它正在调用我未使用的服务。

2 个答案:

答案 0 :(得分:2)

编辑:

def get(self):可能有一些问题。

首先,get可以保留。其次,您无法将self发送到该函数。将其更改为:

def get_service():

EDIT2:

您还需要import logging位于使用它的任何页面的顶部。而且,您尚未导入Flask及其组件:

from flask import Flask, request, render_template #  etc...
import logging

答案 1 :(得分:0)

您的1 of month 10:00 cron计划规范很可能是罪魁祸首:它指定仅在每月的第一天在10:00开始运行该作业!来自Defining the cron job schedule

  

示例:对于1,2,3 of month 07:00计划,作业运行一个   时间是每月前三天的07:00。

因此最后一次执行发生在3天前(如果当时已部署此cron配置),那么直到11月1日才可以进行其他尝试:)

将时间表更改为更易于测试的内容,例如every 5 minutesevery 1 hours,并在您满意更改按预期的方式后将其还原。

相关问题