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 prod | 5 package prod |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "reflect" | 9 "reflect" |
10 | 10 |
11 tq "github.com/luci/gae/service/taskqueue" | 11 tq "github.com/luci/gae/service/taskqueue" |
12 "golang.org/x/net/context" | 12 "golang.org/x/net/context" |
13 "google.golang.org/appengine" | 13 "google.golang.org/appengine" |
14 "google.golang.org/appengine/taskqueue" | 14 "google.golang.org/appengine/taskqueue" |
15 ) | 15 ) |
16 | 16 |
17 // useTQ adds a gae.TaskQueue implementation to context, accessible | 17 // useTQ adds a gae.TaskQueue implementation to context, accessible |
18 // by gae.GetTQ(c) | 18 // by gae.GetTQ(c) |
19 func useTQ(c context.Context) context.Context { | 19 func useTQ(c context.Context) context.Context { |
20 return tq.SetRawFactory(c, func(ci context.Context) tq.RawInterface { | 20 return tq.SetRawFactory(c, func(ci context.Context) tq.RawInterface { |
21 » » return tqImpl{ci} | 21 » » return tqImpl{AEContext(ci)} |
22 }) | 22 }) |
23 } | 23 } |
24 | 24 |
25 type tqImpl struct { | 25 type tqImpl struct { |
26 » context.Context | 26 » aeCtx context.Context |
27 } | 27 } |
28 | 28 |
29 func init() { | 29 func init() { |
30 const taskExpectedFields = 10 | 30 const taskExpectedFields = 10 |
31 // Runtime-assert that the number of fields in the Task structs match, t
o | 31 // Runtime-assert that the number of fields in the Task structs match, t
o |
32 // avoid missing additional fields if they're added later. | 32 // avoid missing additional fields if they're added later. |
33 // all other type assertions are statically enforced by o2n() and tqF2R(
) | 33 // all other type assertions are statically enforced by o2n() and tqF2R(
) |
34 | 34 |
35 oldType := reflect.TypeOf((*taskqueue.Task)(nil)).Elem() | 35 oldType := reflect.TypeOf((*taskqueue.Task)(nil)).Elem() |
36 newType := reflect.TypeOf((*tq.Task)(nil)).Elem() | 36 newType := reflect.TypeOf((*tq.Task)(nil)).Elem() |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 // tqMF2R (TQ multi-fake-to-real) converts []*tq.Task to []*taskqueue.Task. | 79 // tqMF2R (TQ multi-fake-to-real) converts []*tq.Task to []*taskqueue.Task. |
80 func tqMF2R(ns []*tq.Task) []*taskqueue.Task { | 80 func tqMF2R(ns []*tq.Task) []*taskqueue.Task { |
81 ret := make([]*taskqueue.Task, len(ns)) | 81 ret := make([]*taskqueue.Task, len(ns)) |
82 for i, t := range ns { | 82 for i, t := range ns { |
83 ret[i] = tqF2R(t) | 83 ret[i] = tqF2R(t) |
84 } | 84 } |
85 return ret | 85 return ret |
86 } | 86 } |
87 | 87 |
88 func (t tqImpl) AddMulti(tasks []*tq.Task, queueName string, cb tq.RawTaskCB) er
ror { | 88 func (t tqImpl) AddMulti(tasks []*tq.Task, queueName string, cb tq.RawTaskCB) er
ror { |
89 » realTasks, err := taskqueue.AddMulti(t.Context, tqMF2R(tasks), queueName
) | 89 » realTasks, err := taskqueue.AddMulti(t.aeCtx, tqMF2R(tasks), queueName) |
90 if err != nil { | 90 if err != nil { |
91 if me, ok := err.(appengine.MultiError); ok { | 91 if me, ok := err.(appengine.MultiError); ok { |
92 for i, err := range me { | 92 for i, err := range me { |
93 tsk := (*taskqueue.Task)(nil) | 93 tsk := (*taskqueue.Task)(nil) |
94 if realTasks != nil { | 94 if realTasks != nil { |
95 tsk = realTasks[i] | 95 tsk = realTasks[i] |
96 } | 96 } |
97 cb(tqR2F(tsk), err) | 97 cb(tqR2F(tsk), err) |
98 } | 98 } |
99 err = nil | 99 err = nil |
100 } | 100 } |
101 } else { | 101 } else { |
102 for _, tsk := range realTasks { | 102 for _, tsk := range realTasks { |
103 cb(tqR2F(tsk), nil) | 103 cb(tqR2F(tsk), nil) |
104 } | 104 } |
105 } | 105 } |
106 return err | 106 return err |
107 } | 107 } |
108 | 108 |
109 func (t tqImpl) DeleteMulti(tasks []*tq.Task, queueName string, cb tq.RawCB) err
or { | 109 func (t tqImpl) DeleteMulti(tasks []*tq.Task, queueName string, cb tq.RawCB) err
or { |
110 » err := taskqueue.DeleteMulti(t.Context, tqMF2R(tasks), queueName) | 110 » err := taskqueue.DeleteMulti(t.aeCtx, tqMF2R(tasks), queueName) |
111 if me, ok := err.(appengine.MultiError); ok { | 111 if me, ok := err.(appengine.MultiError); ok { |
112 for _, err := range me { | 112 for _, err := range me { |
113 cb(err) | 113 cb(err) |
114 } | 114 } |
115 err = nil | 115 err = nil |
116 } | 116 } |
117 return err | 117 return err |
118 } | 118 } |
119 | 119 |
120 func (t tqImpl) Purge(queueName string) error { | 120 func (t tqImpl) Purge(queueName string) error { |
121 » return taskqueue.Purge(t.Context, queueName) | 121 » return taskqueue.Purge(t.aeCtx, queueName) |
122 } | 122 } |
123 | 123 |
124 func (t tqImpl) Stats(queueNames []string, cb tq.RawStatsCB) error { | 124 func (t tqImpl) Stats(queueNames []string, cb tq.RawStatsCB) error { |
125 » stats, err := taskqueue.QueueStats(t.Context, queueNames) | 125 » stats, err := taskqueue.QueueStats(t.aeCtx, queueNames) |
126 if err != nil { | 126 if err != nil { |
127 return err | 127 return err |
128 } | 128 } |
129 for _, s := range stats { | 129 for _, s := range stats { |
130 cb((*tq.Statistics)(&s), nil) | 130 cb((*tq.Statistics)(&s), nil) |
131 } | 131 } |
132 return nil | 132 return nil |
133 } | 133 } |
134 | 134 |
135 func (t tqImpl) Testable() tq.Testable { | 135 func (t tqImpl) Testable() tq.Testable { |
136 return nil | 136 return nil |
137 } | 137 } |
OLD | NEW |