blob: bdbe837e61716aa2d110f0d5fa9760921b27b5d3 [file] [log] [blame]
// Copyright 2015 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package taskqueue
import (
"net/http"
"net/url"
"time"
)
// RetryOptions let you control whether to retry a task and the backoff intervals between tries.
type RetryOptions struct {
// Number of tries/leases after which the task fails permanently and is deleted.
// If AgeLimit is also set, both limits must be exceeded for the task to fail permanently.
RetryLimit int32
// Maximum time allowed since the task's first try before the task fails permanently and is deleted (only for push tasks).
// If RetryLimit is also set, both limits must be exceeded for the task to fail permanently.
AgeLimit time.Duration
// Minimum time between successive tries (only for push tasks).
MinBackoff time.Duration
// Maximum time between successive tries (only for push tasks).
MaxBackoff time.Duration
// Maximum number of times to double the interval between successive tries before the intervals increase linearly (only for push tasks).
MaxDoublings int32
// If MaxDoublings is zero, set ApplyZeroMaxDoublings to true to override the default non-zero value.
// Otherwise a zero MaxDoublings is ignored and the default is used.
ApplyZeroMaxDoublings bool
}
// Task represents a taskqueue task to be executed.
type Task struct {
// Path is the worker URL for the task.
// If unset, it will default to /_ah/queue/<queue_name>.
Path string
// Payload is the data for the task.
// This will be delivered as the HTTP request body.
// It is only used when Method is POST, PUT or PULL.
// url.Values' Encode method may be used to generate this for POST requests.
Payload []byte
// Additional HTTP headers to pass at the task's execution time.
// To schedule the task to be run with an alternate app version
// or backend, set the "Host" header.
Header http.Header
// Method is the HTTP method for the task ("GET", "POST", etc.),
// or "PULL" if this is task is destined for a pull-based queue.
// If empty, this defaults to "POST".
Method string
// A name for the task.
// If empty, a name will be chosen.
Name string
// Delay specifies the duration the task queue service must wait
// before executing the task.
// Either Delay or ETA may be set, but not both.
Delay time.Duration
// ETA specifies the earliest time a task may be executed (push queues)
// or leased (pull queues).
// Either Delay or ETA may be set, but not both.
ETA time.Time
// The number of times the task has been dispatched or leased.
RetryCount int32
// Tag for the task. Only used when Method is PULL.
Tag string
// Retry options for this task. May be nil.
RetryOptions *RetryOptions
}
// NewPOSTTask creates a Task that will POST to a path with URL-encoded values.
func NewPOSTTask(path string, params url.Values) *Task {
h := make(http.Header)
h.Set("Content-Type", "application/x-www-form-urlencoded")
return &Task{
Path: path,
Payload: []byte(params.Encode()),
Header: h,
Method: "POST",
}
}
// Duplicate returns a deep copy of this Task.
func (t *Task) Duplicate() *Task {
ret := *t
if len(t.Header) > 0 {
ret.Header = make(http.Header, len(t.Header))
for k, vs := range t.Header {
newVs := make([]string, len(vs))
copy(newVs, vs)
ret.Header[k] = newVs
}
}
if len(t.Payload) > 0 {
ret.Payload = make([]byte, len(t.Payload))
copy(ret.Payload, t.Payload)
}
if t.RetryOptions != nil {
ret.RetryOptions = &RetryOptions{}
*ret.RetryOptions = *t.RetryOptions
}
return &ret
}