Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: impl/memory/taskqueue.go

Issue 2995493002: [impl/memory] Enforce named tasks during txn. (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « PRESUBMIT.py ('k') | impl/memory/taskqueue_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The LUCI Authors. 1 // Copyright 2015 The LUCI Authors.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 func (t *taskqueueImpl) AddMulti(tasks []*tq.Task, queueName string, cb tq.RawTa skCB) error { 57 func (t *taskqueueImpl) AddMulti(tasks []*tq.Task, queueName string, cb tq.RawTa skCB) error {
58 t.Lock() 58 t.Lock()
59 defer t.Unlock() 59 defer t.Unlock()
60 60
61 q, err := t.getQueueLocked(queueName) 61 q, err := t.getQueueLocked(queueName)
62 if err != nil { 62 if err != nil {
63 return err 63 return err
64 } 64 }
65 65
66 for _, task := range tasks { 66 for _, task := range tasks {
67 » » task, err := prepTask(t.ctx, task, q, t.ns) 67 » » task, err := prepTask(t.ctx, task, q, t.ns, false)
68 if err == nil { 68 if err == nil {
69 err = q.addTask(task) 69 err = q.addTask(task)
70 } 70 }
71 if err != nil { 71 if err != nil {
72 cb(nil, err) 72 cb(nil, err)
73 } else { 73 } else {
74 cb(task.Duplicate(), nil) 74 cb(task.Duplicate(), nil)
75 } 75 }
76 } 76 }
77 return nil 77 return nil
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 type taskqueueTxnImpl struct { 162 type taskqueueTxnImpl struct {
163 *txnTaskQueueData 163 *txnTaskQueueData
164 164
165 ctx context.Context 165 ctx context.Context
166 ns string 166 ns string
167 } 167 }
168 168
169 var _ tq.RawInterface = (*taskqueueTxnImpl)(nil) 169 var _ tq.RawInterface = (*taskqueueTxnImpl)(nil)
170 170
171 func (t *taskqueueTxnImpl) addLocked(task *tq.Task, q *sortedQueue) (*tq.Task, e rror) { 171 func (t *taskqueueTxnImpl) addLocked(task *tq.Task, q *sortedQueue) (*tq.Task, e rror) {
172 » toSched, err := prepTask(t.ctx, task, q, t.ns) 172 » toSched, err := prepTask(t.ctx, task, q, t.ns, true)
173 if err != nil { 173 if err != nil {
174 return nil, err 174 return nil, err
175 } 175 }
176 176
177 numTasks := 0 177 numTasks := 0
178 for _, vs := range t.anony { 178 for _, vs := range t.anony {
179 numTasks += len(vs) 179 numTasks += len(vs)
180 } 180 }
181 if numTasks+1 > 5 { 181 if numTasks+1 > 5 {
182 // transactional tasks are actually implemented 'for real' as Ac tions which 182 // transactional tasks are actually implemented 'for real' as Ac tions which
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 t.Lock() 256 t.Lock()
257 defer t.Unlock() 257 defer t.Unlock()
258 t.setConstraintsLocked(*c) 258 t.setConstraintsLocked(*c)
259 return nil 259 return nil
260 } 260 }
261 261
262 func (t *taskqueueTxnImpl) GetTestable() tq.Testable { return &taskQueueTestable {t.ns, t} } 262 func (t *taskqueueTxnImpl) GetTestable() tq.Testable { return &taskQueueTestable {t.ns, t} }
263 263
264 ////////////////////////// private functions /////////////////////////////////// 264 ////////////////////////// private functions ///////////////////////////////////
265 265
266 func prepTask(c context.Context, task *tq.Task, q *sortedQueue, ns string) (*tq. Task, error) { 266 func prepTask(c context.Context, task *tq.Task, q *sortedQueue, ns string, inTxn bool) (*tq.Task, error) {
267 toSched := task.Duplicate() 267 toSched := task.Duplicate()
268 268
269 if inTxn && task.Name != "" {
270 return nil, fmt.Errorf("taskqueue: INVALID_TASK_NAME: cannot add named task %q in transaction", task.Name)
271 }
272
269 if toSched.ETA.IsZero() { 273 if toSched.ETA.IsZero() {
270 toSched.ETA = clock.Now(c).Add(toSched.Delay) 274 toSched.ETA = clock.Now(c).Add(toSched.Delay)
271 } else if toSched.Delay != 0 { 275 } else if toSched.Delay != 0 {
272 panic("taskqueue: both Delay and ETA are set") 276 panic("taskqueue: both Delay and ETA are set")
273 } 277 }
274 toSched.Delay = 0 278 toSched.Delay = 0
275 279
276 switch toSched.Method { 280 switch toSched.Method {
277 // Methods that can have payloads. 281 // Methods that can have payloads.
278 case "": 282 case "":
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 } else { 317 } else {
314 if !validTaskName.MatchString(toSched.Name) { 318 if !validTaskName.MatchString(toSched.Name) {
315 return nil, errInvalidTaskName 319 return nil, errInvalidTaskName
316 } 320 }
317 } 321 }
318 322
319 return toSched, nil 323 return toSched, nil
320 } 324 }
321 325
322 func taskNamespace(task *tq.Task) string { return task.Header.Get(currentNamespa ce) } 326 func taskNamespace(task *tq.Task) string { return task.Header.Get(currentNamespa ce) }
OLDNEW
« no previous file with comments | « PRESUBMIT.py ('k') | impl/memory/taskqueue_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698