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