【问题标题】:cron job fails in gae pythoncron 作业在 gae python 中失败
【发布时间】:2013-07-23 13:42:36
【问题描述】:

我在 Google Appengine 中有一个脚本,由 cron.yaml 每 20 分钟启动一次。这在我自己的机器上本地工作。当我(手动)转到在线启动脚本的 url 时,它也可以工作。但是,当 cron.yaml 负责启动脚本时,该脚本总是无法在线完成,在 Google 的实例上。

日志显示没有错误,只有 2 条调试消息:

D 2013-07-23 06:00:08.449
type(soup): <class 'bs4.BeautifulSoup'> END type(soup)

D 2013-07-23 06:00:11.246
type(soup): <class 'bs4.BeautifulSoup'> END type(soup)

这是我的脚本:

# coding: utf-8
import jinja2, webapp2, urllib2, re

from bs4 import BeautifulSoup as bs
from google.appengine.api import memcache
from google.appengine.ext import db

class Article(db.Model):
    content = db.TextProperty()
    datetime = db.DateTimeProperty(auto_now_add=True)
    companies = db.ListProperty(db.Key)
    url = db.StringProperty()

class Company(db.Model):
    name = db.StringProperty() 
    ticker = db.StringProperty()

    @property
    def articles(self):
        return Article.gql("WHERE companies = :1", self.key()) 

def companies_key(companies_name=None):
  return db.Key.from_path('Companies', companies_name or 'default_companies')

def articles_key(articles_name=None):
  return db.Key.from_path('Articles', articles_name or 'default_articles')

def scrape():
   companies = memcache.get("companies")
   if not companies:
      companies = Company.all()
      memcache.add("companies",companies,30)
   for company in companies:
      links = links(company.ticker)
      links = set(links)
      for link in links:
          if link is not "None": 
              article_object = Article() 
              text = fetch(link)            
              article_object.content = text
              article_object.url = link
              article_object.companies.append(company.key()) #doesn't work.
              article_object.put()

def fetch(link):
    try:
        html = urllib2.urlopen(url).read()
        soup = bs(html)
    except:
        return "None"
    text = soup.get_text()
    text = text.encode('utf-8')
    text = text.decode('utf-8')
    text = unicode(text)
    if text is not "None": 
        return text
    else: 
        return "None"


def links(ticker):
    url = "https://www.google.com/finance/company_news?q=NASDAQ:" + ticker + "&start=10&num=10"
    html = urllib2.urlopen(url).read()
    soup = bs(html)
    div_class = re.compile("^g-section.*")
    divs = soup.find_all("div", {"class" : div_class})
    links = []
    for div in divs:
        a = unicode(div.find('a', attrs={'href': re.compile("^http://")})) 
        link_regex = re.search("(http://.*?)\"",a)
        try:
            link = link_regex.group(1)
            soup = bs(link)
            link = soup.get_text() 
        except:
            link = "None"
        links.append(link)

    return links

...和 ​​main 中的脚本处理程序:

class ScrapeHandler(webapp2.RequestHandler):
    def get(self):
        scrape.scrape()
        self.redirect("/")

我的猜测是问题可能是抓取脚本中的双重 for 循环,但我不明白为什么。

更新: 文章确实被刮掉了(应该有多少),现在没有日志错误,甚至根本没有调试消息。查看日志,cron 作业似乎执行得很完美。即便如此,Appengine 的 cron 作业面板仍显示 cron 作业失败。

【问题讨论】:

  • 发布您的代码,可能有问题。这些调试消息是否打印在 cron 作业处理程序中?
  • 调试消息打印在 gae 日志中,在线。这只会在谷歌自己的云中在线失败。在我的机器上,它可以在没有任何警告或调试的情况下运行。
  • 你怎么知道它无法启动,日志表明有东西正在启动。但可能没有完成。我建议您在那里进行更多登录,以查看该过程的进展情况。
  • 是的,他们确实开始了——他们只是没有完成。我将编辑帖子以反映这一点。另外:GAE 默认记录所有内容;没有什么了。
  • if link is not "None":

标签: python google-app-engine cron


【解决方案1】:

我很确定这个错误是由于 DeadlineExceededError 造成的,我没有在本地遇到这个错误。我的 scrape() 脚本现在可以处理更少的公司和文章,并且不会超过截止日期。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2020-08-30
    • 2021-07-31
    • 2011-04-11
    • 2018-07-28
    • 1970-01-01
    相关资源
    最近更新 更多