forked from GoogleCloudPlatform/golang-samples
/
taskqueue.go
52 lines (43 loc) · 1.08 KB
/
taskqueue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package sample
// [START tasks_within_transactions]
import (
"net/url"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/taskqueue"
)
func f(ctx context.Context) {
err := datastore.RunInTransaction(ctx, func(ctx context.Context) error {
t := taskqueue.NewPOSTTask("/worker", url.Values{
// ...
})
// Use the transaction's context when invoking taskqueue.Add.
_, err := taskqueue.Add(ctx, t, "")
if err != nil {
// Handle error
}
// ...
return nil
}, nil)
if err != nil {
// Handle error
}
// ...
}
// [END tasks_within_transactions]
func example() {
var ctx context.Context
// [START purging_tasks]
// Purge entire queue...
err := taskqueue.Purge(ctx, "queue1")
// [END purging_tasks]
// [START deleting_tasks]
// Delete an individual task...
t := &taskqueue.Task{Name: "foo"}
err = taskqueue.Delete(ctx, t, "queue1")
// [END deleting_tasks]
_ = err
}