9

I'm trying to get running a task in the Task Queue using deferred.defer(). The task is added to the default task queue, but the task fail with a 404 error.

This is the handler:

import webapp2
import models

import defer_ajust_utils

from google.appengine.ext import ndb
from google.appengine.ext import deferred

class ajust_utils(webapp2.RequestHandler):
  def get(self):
    deferred.defer(defer_ajust_utils.DoTheJob)

application = webapp2.WSGIApplication([('/ajust_utils', ajust_utils)], debug=True)

This is the module defer_ajust_utils :

import logging
import models 
from google.appengine.ext import ndb

def DoTheJob():
  logging.info("Debut de la mise a jour des utilisateurs")
  utilisateurs = models.Utilisateur.query()
  utilisateurs = utilisateurs.fetch()

  for utilisateur in utilisateurs:
    utilisateur.produire_factures_i = False
    utilisateur.put()  

  logging.info("Fin de la mise a jour des utilisateurs")

And my app.yaml file :

application: xxxx
version: dev
runtime: python27
api_version: 1
threadsafe: yes

builtins:
- deferred: on

handlers:
- url: /ajust_utils
  script : tempo_ajuster_utils.application
  login: admin

Here's the log :

0.1.0.2 - - [10/Mar/2014:17:50:45 -0700] "POST /_ah/queue/deferred HTTP/1.1" 404 113
"http://xxxx.appspot.com/ajust_utils" "AppEngine-Google;
(+http://code.google.com/appengine)" "xxxx.appspot.com" ms=6 cpu_ms=0 
cpm_usd=0.000013 queue_name=default task_name=17914595085560382799 
app_engine_release=1.9.0 instance=00c61b117c0b3648693af0563b92051423b3cb

Thank you for help!!

3 Answers3

6

If you are using push-to-deploy with git, when you add in a 'builtin' part to the app.yaml, such as in

builtins: - deferred: on

you need to do a 'normal' gcloud deploy before you run the app. Otherwise it will not update the running app, which causes 404 errors for /_ah/queue/deferred

There is an open bug for this, so vote for it and it may get fixed. https://code.google.com/p/googleappengine/issues/detail?id=10139

nik
  • 726
  • 2
  • 12
  • 28
3

I was receiving the same error.

It appears to be a documentation defect with https://cloud.google.com/appengine/articles/deferred

I looked in the source code and found the following, which wasn't in any of the documentation:

In order for tasks to be processed, you need to set up the handler. Add the
following to your app.yaml handlers section:

handlers:
- url: /_ah/queue/deferred
  script: $PYTHON_LIB/google/appengine/ext/deferred/handler.py
  login: admin

As I have threadsafe: true set in my app.yaml, I had to add the following handler:

- url: /_ah/queue/deferred
  script: google.appengine.ext.deferred.deferred.application
  login: admin

and then the deferred task queue began working and stopped 404'ing.

ashgromnies
  • 3,266
  • 4
  • 27
  • 43
-1

I think you have to add the option deferred: on to your apps "builtin" options in the app.yaml Here is an excerpt from

https://developers.google.com/appengine/docs/python/config/appconfig#Python_app_yaml_Builtin_handlers

builtins:
- deferred: on

The following builtin handlers are available:
admin_redirect - For production App Engine, this results in a redirect from /_ah/admin to the admin console. This builtin has no effect in the development web server.
appstats - Enables Appstats at /_ah/stats/, which you can use to measure your application's performance. In order to use Appstats, you also need to install the event recorder.
deferred - Enables the deferred handler at /_ah/queue/deferred. This builtin allows developers to use deferred.defer() to simplify the creation of Task Queue tasks. Also see Background work with the deferred library.
Markus Breuer
  • 117
  • 1
  • 6