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

Side by Side Diff: taskqueue.go

Issue 1243323002: Refactor a bit. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: fix golint Created 5 years, 5 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 | « service_test.go ('k') | taskqueue_testable.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 gae
6
7 import (
8 "golang.org/x/net/context"
9 )
10
11 // TaskQueue is the full interface to the Task Queue service.
12 type TaskQueue interface {
13 Add(task *TQTask, queueName string) (*TQTask, error)
14 Delete(task *TQTask, queueName string) error
15
16 AddMulti(tasks []*TQTask, queueName string) ([]*TQTask, error)
17 DeleteMulti(tasks []*TQTask, queueName string) error
18
19 Lease(maxTasks int, queueName string, leaseTime int) ([]*TQTask, error)
20 LeaseByTag(maxTasks int, queueName string, leaseTime int, tag string) ([ ]*TQTask, error)
21 ModifyLease(task *TQTask, queueName string, leaseTime int) error
22
23 Purge(queueName string) error
24
25 QueueStats(queueNames []string) ([]TQStatistics, error)
26 }
27
28 // TQFactory is the function signature for factory methods compatible with
29 // SetTQFactory.
30 type TQFactory func(context.Context) TaskQueue
31
32 // TQFilter is the function signature for a filter TQ implementation. It
33 // gets the current TQ implementation, and returns a new TQ implementation
34 // backed by the one passed in.
35 type TQFilter func(context.Context, TaskQueue) TaskQueue
36
37 // GetTQUnfiltered gets gets the TaskQueue implementation from context without
38 // any of the filters applied.
39 func GetTQUnfiltered(c context.Context) TaskQueue {
40 if f, ok := c.Value(taskQueueKey).(TQFactory); ok && f != nil {
41 return f(c)
42 }
43 return nil
44 }
45
46 // GetTQ gets the TaskQueue implementation from context.
47 func GetTQ(c context.Context) TaskQueue {
48 ret := GetTQUnfiltered(c)
49 if ret == nil {
50 return nil
51 }
52 for _, f := range getCurTQFilters(c) {
53 ret = f(c, ret)
54 }
55 return ret
56 }
57
58 // SetTQFactory sets the function to produce TaskQueue instances, as returned by
59 // the GetTQ method.
60 func SetTQFactory(c context.Context, tqf TQFactory) context.Context {
61 return context.WithValue(c, taskQueueKey, tqf)
62 }
63
64 // SetTQ sets the current TaskQueue object in the context. Useful for testing
65 // with a quick mock. This is just a shorthand SetTQFactory invocation to set
66 // a factory which always returns the same object.
67 func SetTQ(c context.Context, tq TaskQueue) context.Context {
68 return SetTQFactory(c, func(context.Context) TaskQueue { return tq })
69 }
70
71 func getCurTQFilters(c context.Context) []TQFilter {
72 curFiltsI := c.Value(taskQueueFilterKey)
73 if curFiltsI != nil {
74 return curFiltsI.([]TQFilter)
75 }
76 return nil
77 }
78
79 // AddTQFilters adds TaskQueue filters to the context.
80 func AddTQFilters(c context.Context, filts ...TQFilter) context.Context {
81 if len(filts) == 0 {
82 return c
83 }
84 cur := getCurTQFilters(c)
85 newFilts := make([]TQFilter, 0, len(cur)+len(filts))
86 newFilts = append(newFilts, getCurTQFilters(c)...)
87 newFilts = append(newFilts, filts...)
88 return context.WithValue(c, taskQueueFilterKey, newFilts)
89 }
OLDNEW
« no previous file with comments | « service_test.go ('k') | taskqueue_testable.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698