任务队列中的状态405

时间:2012-02-16 06:01:34

标签: python google-app-engine google-cloud-datastore python-2.7

我想解决尝试生成报告时从任务队列中获取的状态405:

2012-02-16 03:56:53.012 /report/ 405 3ms 0kb AppEngine-Google; (+http://code.google.com/appengine)

2012-02-16 03:56:53.007 /createreport/ 302 20ms 0kb Mozilla/5.0 (X11; Linux x86_64; rv:2.0) Gecko/20100101 Firefox/4.0
I 2012-02-16 03:56:52.990 creating report task

创建任务的代码是

class CreateReportHandler(webapp2.RequestHandler):

    def get(self):
        logging.info('creating report task')
        taskqueue.add(url=r'/report/')
        self.redirect('/')

我用webapp2路由它:

Route(r'/createreport/', handler=CreateReportHandler, name='createreport'),

然后我应该能够将它作为一个cron作业,但是当我测试它时,我从这段代码的访问中得到405,如果我尝试直接运行它会超时:

class Report(webapp2.RequestHandler):

    def get(self):
        # Create a conversion request from HTML to PDF.
        users = User.query()
        today = date.today()
        startdate = date(today.year, today.month, 1) # first day of month   
        html = None     
        for user in users: 
            if user.activity() > 0:
                logging.info('found active user %s %s' % (user.firstname, user.lastname))
                html = '<html><body><table border="1">'
                html = html + '<tr><td>ORDER</td><td colspan="2">----DISTRIBUTOR----</td><td>ORDER</td><td>Silver</td><td>%</td><td>Total</td><td>Bonus</td></tr>'
                level = user.level()
                distributor = user
                while distributor.has_downline():
                    downline = User.query(User.sponsor == distributor.key).order(User.lastname).fetch()
                    for person in downline:  # to this for whole downline
                        orders = model.Order.all().filter('distributor_id =' , person.key.id()).filter('created >' , startdate).filter('status =', 'PAID').fetch(999999)
                        silver = 0
                        name = person.firstname +' '+ person.lastname
                        for order in orders:
                            logging.info('found orders')
                            for idx,item in enumerate(order.items):
                                purchase = model.Item.get_by_id(long(item.id()))
                                amount = int(order.amounts[idx])
                                silver = silver + amount*purchase.silver/1000.000 
                            if len(name) > 13:
                                name = name[13]
                            html = html + '<tr><td>' + str(order.created.date().day)+'/'+ str(order.created.date().month )+'</td><td>' + filters.makeid(person.key.id()) +'</td><td>' + name + '</td><td>' + str(order.key().id()) + '</td><td>' + str(silver) 
                            dist_level = order.dist_level
                            bonus = 0   
                            if level == 5 and dist_level == 4:                          
                                bonus = 0.05
                            if level == 5 and dist_level == 3:
                                bonus = 0.1
                            if level == 5 and dist_level == 2:
                                bonus = 0.13
                            if level == 5 and dist_level == 1:
                                bonus = 0.35

                            if level == 4 and dist_level == 3:                          
                                bonus = 0.05
                            if level == 4 and dist_level == 2:
                                bonus = 0.08
                            if level == 4 and dist_level == 1:
                                bonus = 0.3

                            if level == 3 and dist_level == 2:                          
                                bonus = 0.03
                            if level == 3 and dist_level == 1:
                                bonus = 0.25

                            if level == 2 and dist_level == 1:                          
                                bonus = 0.2

                            html = html + '</td><td>' + str(bonus) + '</td><td>' + str(order.total)
                            bonusmoney = bonus * float(order.total)
                            html = html + '</td><td>' + str(bonusmoney) + '</td></tr>'

                        distributor = person

                html = html + '</table>'

            asset = conversion.Asset("text/html", html, "test.html")
            conversion_obj = conversion.Conversion(asset, "application/pdf")        
            rpc = conversion.create_rpc()
            conversion.make_convert_call(rpc, conversion_obj)

            result = rpc.get_result()
            if result.assets:
                for asset in result.assets:
                    logging.info('emailing report')# to %s' % user.email)
                    message = mail.EmailMessage(sender='noreply@bnano.se',
                                    subject='Report %s %s' % (user.firstname, user.lastname))
                    message.body = 'Here is the monthly report'
                    message.to = 'niklasro@gmail.com'
                    message.bcc = 'fridge@koolbusiness.com'
                    message.attachments = ['report.pdf', asset.data]
                    message.send()
                    logging.info('message sent')

如何解决状态405并完成执行?

谢谢

2 个答案:

答案 0 :(得分:15)

我来自GAE / J-land,所以我不熟悉Python,但之前我遇到了来自我的taskqueue worker的405响应。在我的情况下,这是由于在构建TaskOption时将POST方法设置为Task,而我的处理程序仅提供GET个请求。

编辑:检查TaskQueue.add() docs后,如果未指定方法(如代码示例中所示),则使用的默认方法是POST,而处理程序似乎只能提供GET请求。 / p>

我的建议是明确指定您的任务使用GET方法而不是POST,或者将处理程序的处理方法更改为POST而不是GET。

答案 1 :(得分:1)

只想添加一个可能的方案,因为快速搜索&#34; taskqueue 405 &#34;一切都在这个页面结束:

我收到405错误,因为没有指定&#34;目标&#34;参数。 taskqueue.add()最终将任务添加到默认目标,其中我的处理程序位于单独的后端模块上。

  

如果未指定target,则在同一版本上调用任务   他们入队的申请书。所以,如果你排队了   默认应用程序版本中的任务,但未指定目标   在队列中,该任务在默认应用程序版本中调用。