Pregunta

I'm trying to get a backend in Google Appengine to run a task from the taskqueue. It runs without errors, but the task is not performed. I've found the documentation extremely confusing.

My cron.yaml:

- description: backend test
  url: /send_to_backend
  schedule: every 2 minutes

My app.yaml:

- url: /send_to_backend
  script: test.app
  login: admin

My backends.yaml:

- name: backendtest
  class: B1

My queue.yaml:

total_storage_limit: 500M
queue:
    - name: test
      rate: 1/s
      max_concurrent_requests: 1

My handlers in main.py:

class BackendHandler(webapp2.RequestHandler):
    def get(self):
    taskqueue.add(url='/test', target='backendtest')

class TestHandler(webapp2.RequestHandler):
    def get(self):
        test.test()

The function that does the actual work, in test.py:

def test():
    company = Company()
    company.name = "Advanced Micro Devices, Inc"
    company.exchange = "NASDAQ"
    company.put()

AMD is never entered into the db, and I'm at a loss. Am I even doing this the right way? Do backends and taskqueues go together like this?

¿Fue útil?

Solución

Yes, backends and task queues go together like this.

Unfortunately you didn't post completely runnable example so it's hard to say how many fixes you need. One fix that you definitely need is change get -> post in TestHandler (queue tasks processing is done by POST)

Below there is fully runnable and working version of your example. Do not forget that "The development server doesn't automatically run your cron jobs", so try it with curl in dev environment:

app.yaml

application: stackoverflow-21225722
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:

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

backends.yaml

backends:
- name: backendtest
  class: B1

cron.yaml

- description: backend test
  url: /send_to_backend
  schedule: every 2 minutes

main.py

from google.appengine.api import taskqueue
import test
import webapp2

class BackendHandler(webapp2.RequestHandler):
    def get(self):
      taskqueue.add(url='/test', target='backendtest')

class TestHandler(webapp2.RequestHandler):
    def post(self):
       test.test()

app = webapp2.WSGIApplication([
    ('/send_to_backend', BackendHandler),
    ('/test', TestHandler)
], debug=True)

queue.yaml

total_storage_limit: 500M
queue:
    - name: test
      rate: 1/s
      max_concurrent_requests: 1

test.py

from google.appengine.ext import ndb

class Company(ndb.Model):
    name = ndb.StringProperty()
    exchange = ndb.StringProperty()

def test():
    company = Company()
    company.name = "Advanced Micro Devices, Inc"
    company.exchange = "NASDAQ"
    company.put()
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top