简体   繁体   English

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

[英]GAE - cron job failing, with no error message in logs

I have been trying to run a cron job with GAE (code developed in Python), but when I trigger the job, it fails without any error message -- I can't find anything at all in the logs. 我一直在尝试使用GAE(用Python开发的代码)运行Cron作业,但是当我触发该作业时,它会失败,并且没有任何错误消息-我根本无法在日志中找到任何内容。

This is happening for a service for which I'm using the flexible environment. 对于使用灵活环境的服务,这种情况正在发生。

This is the structure of my files: 这是我文件的结构:

my_service.yaml looks like this: my_service.yaml看起来像这样:

service: my_service
runtime: custom
env: flex

env_variables:
  a:
  b:

the my_service.app looks like this: 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)

Then I have a dispatch.yaml with this structure: 然后我有一个具有以下结构的dispatch.yaml

dispatch:

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

And a cron.yaml : 还有一个cron.yaml

cron:

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

Not sure what I'm doing wrong here. 不知道我在做什么错。

EDIT 编辑

A bit of context. 有点背景。 This is something I'm editing starting from this repo . 这是我正在从此repo开始编辑的东西。

The service called backend that is defined in there works perfectly (it has also the same schedule in the cron job as my_service but when I trigger it in a day different from the one in which it's scheduled, it works just fine). 在那里定义的名为backend的服务可以完美地工作(它在cron作业中的时间表也与my_service相同,但是当我在不同于预定日期的一天触发它时,它就可以正常工作)。 What I did was to create an additional service with its own yaml file, which looks exactly the same as the beckend.yaml , its own my_service.py and adding it to the dispacth.yaml and the cron.yaml . 我所做的就是建立有自己的YAML文件,看起来完全一样的附加服务beckend.yaml ,自有my_service.py并将其添加到dispacth.yamlcron.yaml In theory this should work, since the structure is exactly the same, but it doesn't. 从理论上讲,这应该起作用,因为结构完全相同,但事实并非如此。

This service was originally developed in the standard environment and there it was working, the problem originated when I moved it to the flex environment. 该服务最初是在标准环境中开发的,并且可以正常工作,当我将其移至Flex环境时便产生了问题。

EDIT 2: 编辑2:

The problem was actually in the Dockerfile, that was calling a service that I was not using. 问题实际上出在Dockerfile中,它正在调用我未使用的服务。

EDIT: 编辑:

def get(self): may have some issues. def get(self):可能有一些问题。

First, get may be reserved. 首先, get可能会保留。 Second, you aren't able to send self to that function. 其次,您无法将self发送给该函数。 Change that to: 更改为:

def get_service():

EDIT2: 编辑2:

You also need to import logging at the top of any page that uses it. 您还需要在使用import logging的任何页面顶部import logging And, you have not imported Flask and its components: 并且,您尚未导入Flask及其组件:

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

Your 1 of month 10:00 cron schedule specification can most likely be the culprit: it specifies to run the job at 10:00 only on the first day of each month! 1 of month 10:00 Cron计划规范很可能是罪魁祸首:它指定仅在每月的第一天在10:00运行作业! From Defining the cron job schedule : 定义cron工作计划

Example: For the 1,2,3 of month 07:00 schedule, the job runs one time at 07:00 on the first three days of each month. 示例:对于1,2,3 of month 07:001,2,3 of month 07:00计划,作业在每月前三天的07:00运行一次。

So the last execution happened 3 days ago (if this cron config was deployed at the time) and no other attempt will happen until Nov 1st :) 因此,最后一次执行发生在3天前(如果当时已部署此cron配置),直到11月1日才进行其他尝试:)

Change the schedule to something easier to test with, like every 5 minutes or every 1 hours and revert the change once you're happy it works as expected. 将时间表更改为更易于测试的内容,例如every 5 minutesevery 1 hours一次更改,并在您满意后按预期恢复所做的更改。

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

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