OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package wrapper | |
6 | |
7 import ( | |
8 "golang.org/x/net/context" | |
9 | |
10 "appengine/taskqueue" | |
11 ) | |
12 | |
13 // TQSingleReadWriter allows you to add or delete a single Task from a queue. | |
14 // See appengine.taskqueue. | |
15 type TQSingleReadWriter interface { | |
16 Add(task *taskqueue.Task, queueName string) (*taskqueue.Task, error) | |
17 Delete(task *taskqueue.Task, queueName string) error | |
18 } | |
19 | |
20 // TQMultiReadWriter allows you to add or delete a batch of Tasks from a queue. | |
21 // See appengine.taskqueue. | |
22 type TQMultiReadWriter interface { | |
23 TQSingleReadWriter | |
24 | |
25 AddMulti(tasks []*taskqueue.Task, queueName string) ([]*taskqueue.Task,
error) | |
26 DeleteMulti(tasks []*taskqueue.Task, queueName string) error | |
27 } | |
28 | |
29 // TQLeaser allows you to lease tasks from a Pull queue. | |
30 // See appengine.taskqueue. | |
31 type TQLeaser interface { | |
32 Lease(maxTasks int, queueName string, leaseTime int) ([]*taskqueue.Task,
error) | |
33 LeaseByTag(maxTasks int, queueName string, leaseTime int, tag string) ([
]*taskqueue.Task, error) | |
34 ModifyLease(task *taskqueue.Task, queueName string, leaseTime int) error | |
35 } | |
36 | |
37 // TQPurger allows you to drain a queue without processing it. See | |
38 // appengine.taskqueue. | |
39 type TQPurger interface { | |
40 Purge(queueName string) error | |
41 } | |
42 | |
43 // TQStatter allows you to obtain semi-realtime stats on the current state of | |
44 // a queue. See appengine.taskqueue. | |
45 type TQStatter interface { | |
46 QueueStats(queueNames []string, maxTasks int) ([]taskqueue.QueueStatisti
cs, error) | |
47 } | |
48 | |
49 // TaskQueue is the full interface to the Task Queue service. | |
50 type TaskQueue interface { | |
51 TQMultiReadWriter | |
52 TQLeaser | |
53 TQPurger | |
54 } | |
55 | |
56 // TQFactory is the function signature for factory methods compatible with | |
57 // SetTQFactory. | |
58 type TQFactory func(context.Context) TaskQueue | |
59 | |
60 // GetTQ gets the TaskQueue implementation from context. | |
61 func GetTQ(c context.Context) TaskQueue { | |
62 if f, ok := c.Value(taskQueueKey).(TQFactory); ok && f != nil { | |
63 return f(c) | |
64 } | |
65 return nil | |
66 } | |
67 | |
68 // SetTQFactory sets the function to produce TaskQueue instances, as returned by | |
69 // the GetTQ method. | |
70 func SetTQFactory(c context.Context, tqf TQFactory) context.Context { | |
71 return context.WithValue(c, taskQueueKey, tqf) | |
72 } | |
73 | |
74 // SetTQ sets the current TaskQueue object in the context. Useful for testing | |
75 // with a quick mock. This is just a shorthand SetTQFactory invocation to set | |
76 // a factory which always returns the same object. | |
77 func SetTQ(c context.Context, tq TaskQueue) context.Context { | |
78 return SetTQFactory(c, func(context.Context) TaskQueue { return tq }) | |
79 } | |
OLD | NEW |