2

我非常接近完成一个使用 GAE 中的推送任务队列向用户发送后续电子邮件的项目。但是,我不断收到 KeyError 并且不知道为什么。我一直在寻找好的模型来作为我的项目的基础,但还没有找到任何使用多个参数的像样的例子。GAE 文档在上个月有所改进,但仍有很多不足之处。

我已经使用开发服务器中的交互式控制台检查了许多代码,但我仍然不知道我做错了什么。我最好的猜测是参数没有传递到脚本的下一部分(类 pushQueue)。

应用程序.yaml:

application: gae-project
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /cron/sendfu
  script: main.app
  login: admin

- url: /emailworker
  script: main.app
  login: admin

- url: /worker
  script: main.app
  login: admin

- url: /.*
  script: main.app
  login: required

cron.yaml:

cron:
- description: sends follow-up emails
  url: /cron/sendfu
  schedule: every day 20:00

队列.yaml:

total_storage_limit: 120M
queue:
- name: emailworker
  rate: 1/s
  bucket_size: 50
  retry_parameters:
    task_retry_limit: 5
    task_age_limit: 6h
    min_backoff_seconds: 10
    max_backoff_seconds: 60

主要.py:

import webapp2
import datetime
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.api import taskqueue
import jinja2
import os

jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class emailJobs(db.Model):
    """ Models an a list of email jobs for each user """
    triggerid = db.StringProperty()  #Trig id
    recipientid_po = db.StringProperty() # id
    recipientlang = db.StringProperty()  #Language
    fu_email_sent = db.DateTimeProperty() 
    fuperiod = db.IntegerProperty() # (0 - 13)
    fu1 = db.DateTimeProperty() 
    fu2 = db.DateTimeProperty()
    
    @classmethod
    def update_fusent(cls, key_name, senddate):
        """ Class method that updates fu messages sent in the GAE Datastore """
        emailsjobs = cls.get_by_key_name(key_name)
        if emailsjobs is None:
            emailsjobs = cls(key_name=key_name)
        emailsjobs.fu_email_sent = senddate
        emailsjobs.put()

def timeStampFM(now):
    d = now.date()
    year = d.year
    month = d.month
    day = d.day
    t = now.time()
    hour = t.hour
    minute = t.minute + 5
    second = t.second
    today_datetime = datetime.datetime(year, month, day, hour, minute, second)
    return today_datetime


class MainPage(webapp2.RequestHandler):
    """ Main admin login page """
    def get(self):
        if users.get_current_user():
            url = users.create_logout_url(self.request.uri)
            url_linktext = 'Logout'
            urla = '/'
            url_admin = ""
            if users.is_current_user_admin():
                url = users.create_logout_url(self.request.uri)
                urla = "_ah/admin/"
                url_admin = 'Go to admin pages'
                url_linktext = 'Logout'
             
        else:
            url = users.create_login_url(self.request.uri)
            url_linktext = 'Login'

        template_values = {
            'url': url,
            'url_linktext': url_linktext,
            'url_admin': url_admin,
            'urla': urla,
            }

        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))


class sendFollowUp(webapp2.RequestHandler):
    """ Queries Datastore for fu dates that match today's date, then adds them to a task queue """
    def get(self):
    
        now = datetime.datetime.now()
        now_dt = now.date() #today's date to compare with fu dates
   
        q = emailJobs.all()
        q.filter('fuperiod >', 0)
        q.filter('fuperiod <', 99)

        for part in q:
            guid = str(part.recipientid_po)
            lang = str(part.recipientlang)
            trigid = str(part.triggerid)

            if part.fuperiod == 1:
                fu1rawdt = part.fu1
                fu1dt = fu1rawdt.date()
                if fu1dt == now_dt:
                    follow_up = '1'
                
            if part.fuperiod == 2:
                fu2rawdt = part.fu2
                fu2dt = fu2rawdt.date()
                if fu2dt == now_dt:
                    follow_up = '2'
                
            if follow_up != None:
                taskqueue.add(queue_name='emailworker', url='/emailworker', params={'guid': guid,
                                                                                'fu': follow_up,
                                                                                'lang': lang,
                                                                                'trigid': trigid,
                                                                                })
        self.redirect('/emailworker')


class pushQueue(webapp2.RequestHandler):
    """ Sends fu emails, updates the Datastore with datetime sent """

    def store_emails(self, trigid, senddate):
        db.run_in_transaction(emailJobs.update_fusent, trigid, senddate)
        
    def get(self):
        fu_messages = {'1': 'MS_x01', 
                       '2': 'MS_x02',
                       }
        langs = {'EN': 'English subject',
                 'ES': 'Spanish subject',
                 }
    
        fu = str(self.request.get('fu'))
        messageid = fu_messages[fu]

        lang = str(self.request.get('lang'))
        subject = langs[lang]
    
        now = datetime.datetime.now()
        senddate = timeStampFM(now)
     
        guid = str(self.request.get('guid'))
        trigid = str(self.request.get('trigid'))
    
        data = {}
        data['Subject'] = subject
        data['MessageID'] = messageid
        data['SendDate'] = senddate
        data['RecipientID'] = guid
        # Here I do something with data = {}
    
        self.store_emails(trigid, senddate)
    
app = webapp2.WSGIApplication([('/', MainPage),
                           ('/cron/sendfu', sendFollowUp),
                           ('/emailworker', pushQueue)],
                           debug=True)

当我在以下位置测试 cron 作业时:localhost:8086/cron/sendfu

它重定向到:localhost:8086/emailworker

我收到以下错误消息:

内部服务器错误

服务器出错或无法执行请求的操作。

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
  File "/Users/me/Documents/workspace/gae-project/src/main.py", line 478, in get
messageid = fu_messages[fu]
KeyError: ''

从日志:

INFO     2013-03-05 03:03:22,337 dev_appserver.py:3104] "GET /cron/sendfu HTTP/1.1" 302 -
ERROR    2013-03-05 03:03:22,348 webapp2.py:1552] ''
Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
  File "/Users/me/Documents/workspace/gae-project/src/main.py", line 478, in get
messageid = fu_messages[fu]
KeyError: ''
INFO     2013-03-05 03:03:22,355 dev_appserver.py:3104] "GET /emailworker HTTP/1.1" 500 -
INFO     2013-03-05 03:03:22,509 dev_appserver.py:3104] "GET /favicon.ico HTTP/1.1" 404 -

行:

469    def get(self):
470        fu_messages = {'1': 'MS_x01', 
471                       '2': 'MS_x02',
472                       }
473        langs = {'EN': 'English subject',
474                 'ES': 'Spanish subject',
475                 }
476
477        fu = str(self.request.get('fu'))
478        messageid = fu_messages[fu]
4

1 回答 1

2

你打电话时

fu = str(self.request.get('fu'))

如果'fu'请求中没有,self.request.get将返回空字符串 ( '')。所以当你尝试

messageid = fu_messages[fu]

它在

fu_messages = {'1': 'MS_x01', 
               '2': 'MS_x02',
               }

它只有'1''2'作为键。

您的pushQueue处理程序没有看到params您发送的原因

params = {
    'guid': guid,
    'fu': follow_up,
    'lang': lang,
    'trigid': trigid,
}
taskqueue.add(queue_name='emailworker', url='/emailworker', 
              params=params)

是因为您使用的是GET处理程序而不是POSTPUT处理程序。正如文档所述:

参数被编码为application/x-www-form-urlencoded并设置为有效负载。

因此,请求的有效负载中包含您的'fu'参数,但由于它是一个GET请求,因此有效负载被丢弃(这是 HTTP 的工作方式,并非特定于 App Engine)。如果您POST用作处理程序,则有效负载将按预期通过。

我注意到您的代码与文档中的示例非常相似,但只是get在示例使用的地方使用post

于 2013-03-05T03:27:50.573 回答