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