Example #1
0
func addTaskHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)

	// [START adding_tasks_to_a_pull_queue]
	t := &taskqueue.Task{
		Payload: []byte("hello world"),
		Method:  "PULL",
	}
	_, err := taskqueue.Add(ctx, t, "pull-queue")
	// [END adding_tasks_to_a_pull_queue]
	_ = err

	// [START leasing_tasks_1]
	tasks, err := taskqueue.Lease(ctx, 100, "pull-queue", 3600)
	// [END leasing_tasks_1]

	// [START leasing_tasks_2]
	_, err = taskqueue.Add(ctx, &taskqueue.Task{
		Payload: []byte("parse"), Method: "PULL", Tag: "parse",
	}, "pull-queue")
	_, err = taskqueue.Add(ctx, &taskqueue.Task{
		Payload: []byte("render"), Method: "PULL", Tag: "render",
	}, "pull-queue")

	// leases render tasks, but not parse
	tasks, err = taskqueue.LeaseByTag(ctx, 100, "pull-queue", 3600, "render")

	// Leases up to 100 tasks that have same tag.
	// Tag is that of "oldest" task by ETA.
	tasks, err = taskqueue.LeaseByTag(ctx, 100, "pull-queue", 3600, "")
	// [END leasing_tasks_2]

	// [START deleting_tasks_1]
	tasks, err = taskqueue.Lease(ctx, 100, "pull-queue", 3600)
	// Perform some work with the tasks here

	taskqueue.DeleteMulti(ctx, tasks, "pull-queue")
	// [END deleting_tasks_1]

}
Example #2
0
// Checks the task queue every half-hour
func poller(ctx context.Context) {
	for true {
		tasks, err := taskqueue.Lease(ctx, 1, "reminders", 3600)
		if err != nil {
			// not sure what to do with errors
		} else {
			if len(tasks) > 0 {
				sendOneReminder(ctx, tasks[0])
			}
		}
		time.Sleep(time.Minute * 30)
	}
}