Permalink
Browse files

Implemented the taskqueue api. Usable!

  • Loading branch information...
1 parent f2e387d commit c8712fd24e663d0db0d0bd973b7334b287ceab28 @gmosx gmosx committed Nov 17, 2009
Showing with 92 additions and 46 deletions.
  1. +12 −1 README.md
  2. +80 −27 lib/google/appengine/api/labs/taskqueue.js
  3. +0 −18 lib/google/appengine/ext/db/errors.js
View
@@ -21,9 +21,9 @@ This library is under construction but usable. Substantial parts of the Python A
* google/appengine/api/mail: 60% (usable)
* google/appengine/api/images: 40% (usable)
* google/appengine/api/users: 80% (usable)
+* google/appengine/api/lab/taskqueue: 80% (usable)
* google/appengine/ext/db: 80% (usable, expect minor API changes)
* google/appengine/ext/db/forms: 30% (expect API changes)
-* google/appengine/api/lab/taskqueue: 10%
Datastore
@@ -90,7 +90,18 @@ Users
}
var url = user.createLoginURL();
+
+
+Task Queue
+----------
+
+ var taskqueue = require("google/appengine/api/labs/taskqueue");
+ taskqueue.add({url: "/worker", method: "GET", params: {par1: "hello", par2: "world"}});
+
+ var task = new Task({url: "/worker", method: "GET", params: {par1: "hello", par2: "world"}});
+ task.add("customqueue");
+
Example
-------
@@ -1,47 +1,82 @@
+/**
+ * Task Queue API.
+ *
+ * Enables an application to queue background work for itself. Work is done through
+ * webhooks that process tasks pushed from a queue. Tasks will execute in
+ * best-effort order of ETA. Webhooks that fail will cause tasks to be retried at a
+ * later time. Multiple queues may exist with independent throttling controls.
+ *
+ * Webhook URLs may be specified directly for Tasks, or the default URL scheme
+ * may be used, which will translate Task names into URLs relative to a Queue's
+ * base path. A default queue is also provided for simple usage.
+ */
+
var JQueue = Packages.com.google.appengine.api.labs.taskqueue.Queue,
JQueueFactory = Packages.com.google.appengine.api.labs.taskqueue.QueueFactory,
- JTaskOptionsBuilder = Packages.com.google.appengine.api.labs.taskqueue.TaskOptions.Builder;
+ JTaskOptions = Packages.com.google.appengine.api.labs.taskqueue.TaskOptions,
+ JTaskOptionsMethod = JTaskOptions.Method;
+ JTaskOptionsBuilder = JTaskOptions.Builder;
+
+var HTTP_METHODS = {
+ "DELETE": JTaskOptionsMethod.DELETE,
+ "GET": JTaskOptionsMethod.GET,
+ "HEAD": JTaskOptionsMethod.HEAD,
+ "POST": JTaskOptionsMethod.POST,
+ "PUT": JTaskOptionsMethod.PUT
+}
var jdefaultQueue = JQueueFactory.getDefaultQueue();
-var defineError = function(name) {
- var ctor = function(message) {
- var stack = (new Error).stack.split("\n").slice(1);
- var [undef, undef, this.fileName, this.lineNumber] = /^(.*?)@(.*?):(.*?)$/.exec(stack[1]);
- this.stack = stack.join("\n");
- this.message = message;
- }
-
- ctor.prototype = Object.create(Error.prototype);
- ctor.prototype.constructor = ctor;
- ctor.prorotype.name = name;
-
- return ctor;
-}
+var defineError = require("google/appengine/utils").defineError;
-/**
- * Convenience method will create a Task and add it to the default queue.
- *
- * Args:
- * *args, **kwargs: Passed to the Task constructor.
- *
- * Returns:
- * The Task that was added to the queue.
+/** The queue specified is unknown. */
+exports.UnknownQueueError = defineError("UnknownQueueError");
+
+/**
+ * There was a transient error while accessing the queue.
+ * Please Try again later.
*/
-exports.add = function(args) {
-}
+exports.TransientError = defineError("TransientError");
/**
* Instantiates a Task object which will describe a unit of offline work.
* The instance may be inserted into a queue, so that it will be executed
* asynchronously by App Engine.
+ *
+ * All arguments are optional.
+ *
+ * Arguments:
+ * - payload
+ * - countdown
+ * - eta
+ * - headers
+ * - method
+ * - name
+ * - params
+ * - url
*/
var Task = exports.Task = function(args) {
+ var o = this.jtaskOptions = JTaskOptionsBuilder.withDefaults();
+
+ if (args.url) o.url(args.url);
+ if (args.name) o.taskName(args.name);
+ if (args.eta) o.etaMillis(args.eta);
+ if (args.method) o.method(HTTP_METHODS[args.method]);
+
+ if (args.params) {
+ for (var paramName in args.params) {
+ o.param(paramName, args.params[paramName]);
+ }
+ }
}
+/**
+ * Adds this Task to a queue. The queue is specified by name. A single Task
+ * instance may only be added to one queue.
+ */
Task.prototype.add = function(queueName) {
- var jqueue = queueName ?JQueueFactory.getQueue(queueName) : jdefaultQueue;
-
+ var jqueue = queueName ? JQueueFactory.getQueue(queueName) : jdefaultQueue;
+ jqueue.add(this.jtaskOptions);
}
/**
@@ -57,9 +92,27 @@ var Queue = exports.Queue = function(name) {
this._jqueue = JQueueFactory.getQueue(name);
} else {
this._jqueue = jdefaultQueue;
+ this.name = "default";
}
}
+/**
+ * Add a Task to this Queue.
+ */
Queue.prototype.add = function(task) {
task.add(this)
}
+
+/**
+ * Convenience method will create a Task and add it to the default queue.
+ *
+ * Args:
+ * *args, **kwargs: Passed to the Task constructor.
+ *
+ * Returns:
+ * The Task that was added to the queue.
+ */
+exports.add = function(args) {
+ var task = new Task(args);
+ task.add();
+}
@@ -1,23 +1,5 @@
var defineError = require("google/appengine/utils").defineError;
-/*
-var KindError = exports.KindError = function(message) {
- this.message = message;
-}
-
-KindError.prototype.toString = function() {
- return this.message;
-}
-
-var BadValueError = function(message) {
- this.message = message;
-}
-
-BadValueError.prototype = Object.create(Error.prototype);
-
-exports.NotSavedError = Error;
-*/
-
exports.KindError = defineError("KindError");
exports.BadValueError = defineError("BadValueError");
exports.NotSavedError = defineError("NotSavedError");

0 comments on commit c8712fd

Please sign in to comment.