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 // This file contains types which are mirrors/duplicates of the upstream SDK | |
6 // types. This exists so that users can depend solely on this wrapper library | |
7 // without necessarially needing an SDK implementation present. | |
8 // | |
9 // This was done (instead of type-aliasing from the github version of the SDK) | |
10 // because some of the types need to be tweaked (like Task.RetryOptions) to | |
11 // interact well with the wrapper, and the inconsistency of having some types | |
12 // defined by the gae package and others defined by the SDK was pretty awkward. | |
13 | |
14 package taskqueue | 5 package taskqueue |
15 | 6 |
16 import ( | 7 import ( |
17 "net/http" | 8 "net/http" |
| 9 "net/url" |
18 "time" | 10 "time" |
19 ) | 11 ) |
20 | 12 |
21 // Statistics represents statistics about a single task queue. | |
22 type Statistics struct { | |
23 Tasks int // may be an approximation | |
24 OldestETA time.Time // zero if there are no pending tasks | |
25 | |
26 Executed1Minute int // tasks executed in the last minute | |
27 InFlight int // tasks executing now | |
28 EnforcedRate float64 // requests per second | |
29 } | |
30 | |
31 // RetryOptions let you control whether to retry a task and the backoff interval
s between tries. | 13 // RetryOptions let you control whether to retry a task and the backoff interval
s between tries. |
32 type RetryOptions struct { | 14 type RetryOptions struct { |
33 // Number of tries/leases after which the task fails permanently and is
deleted. | 15 // Number of tries/leases after which the task fails permanently and is
deleted. |
34 // If AgeLimit is also set, both limits must be exceeded for the task to
fail permanently. | 16 // If AgeLimit is also set, both limits must be exceeded for the task to
fail permanently. |
35 RetryLimit int32 | 17 RetryLimit int32 |
36 | 18 |
37 // Maximum time allowed since the task's first try before the task fails
permanently and is deleted (only for push tasks). | 19 // Maximum time allowed since the task's first try before the task fails
permanently and is deleted (only for push tasks). |
38 // If RetryLimit is also set, both limits must be exceeded for the task
to fail permanently. | 20 // If RetryLimit is also set, both limits must be exceeded for the task
to fail permanently. |
39 AgeLimit time.Duration | 21 AgeLimit time.Duration |
40 | 22 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 // The number of times the task has been dispatched or leased. | 73 // The number of times the task has been dispatched or leased. |
92 RetryCount int32 | 74 RetryCount int32 |
93 | 75 |
94 // Tag for the task. Only used when Method is PULL. | 76 // Tag for the task. Only used when Method is PULL. |
95 Tag string | 77 Tag string |
96 | 78 |
97 // Retry options for this task. May be nil. | 79 // Retry options for this task. May be nil. |
98 RetryOptions *RetryOptions | 80 RetryOptions *RetryOptions |
99 } | 81 } |
100 | 82 |
| 83 // NewPOSTTask creates a Task that will POST to a path with URL-encoded values. |
| 84 func NewPOSTTask(path string, params url.Values) *Task { |
| 85 h := make(http.Header) |
| 86 h.Set("Content-Type", "application/x-www-form-urlencoded") |
| 87 return &Task{ |
| 88 Path: path, |
| 89 Payload: []byte(params.Encode()), |
| 90 Header: h, |
| 91 Method: "POST", |
| 92 } |
| 93 } |
| 94 |
101 // Duplicate returns a deep copy of this Task. | 95 // Duplicate returns a deep copy of this Task. |
102 func (t *Task) Duplicate() *Task { | 96 func (t *Task) Duplicate() *Task { |
103 ret := *t | 97 ret := *t |
104 | 98 |
105 if len(t.Header) > 0 { | 99 if len(t.Header) > 0 { |
106 ret.Header = make(http.Header, len(t.Header)) | 100 ret.Header = make(http.Header, len(t.Header)) |
107 for k, vs := range t.Header { | 101 for k, vs := range t.Header { |
108 newVs := make([]string, len(vs)) | 102 newVs := make([]string, len(vs)) |
109 copy(newVs, vs) | 103 copy(newVs, vs) |
110 ret.Header[k] = newVs | 104 ret.Header[k] = newVs |
111 } | 105 } |
112 } | 106 } |
113 | 107 |
114 if len(t.Payload) > 0 { | 108 if len(t.Payload) > 0 { |
115 ret.Payload = make([]byte, len(t.Payload)) | 109 ret.Payload = make([]byte, len(t.Payload)) |
116 copy(ret.Payload, t.Payload) | 110 copy(ret.Payload, t.Payload) |
117 } | 111 } |
118 | 112 |
119 if t.RetryOptions != nil { | 113 if t.RetryOptions != nil { |
120 ret.RetryOptions = &RetryOptions{} | 114 ret.RetryOptions = &RetryOptions{} |
121 *ret.RetryOptions = *t.RetryOptions | 115 *ret.RetryOptions = *t.RetryOptions |
122 } | 116 } |
123 | 117 |
124 return &ret | 118 return &ret |
125 } | 119 } |
OLD | NEW |