Class: AppEngine::Labs::TaskQueue::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/appengine-apis/labs/taskqueue.rb

Overview

Represents a single Task on a queue.

Instance Method Summary collapse

Constructor Details

#initialize(payload = nil, options = {}) ⇒ Task

Initializer.

All parameters are optional.

Options:

:payload

The payload data for this Task that will be delivered to

the webhook as the HTTP request body. This is only allowed for
POST and PUT methods. Assumed to be UTF-8 unless it is a Blob.
:bytes

Binary payload data for this Task.

[:countdown]: Time in seconds into the future that this Task should

execute. Defaults to zero.
:eta

Absolute time when the Task should execute. Must be a Time

object. May not be specified if 'countdown' is also supplied.
:headers

Hash of headers to pass to the webhook. Values in the

hash may be enumerable to indicate repeated header fields.

[:method]: HTTP method to use when accessing the webhook. Defaults

to 'POST'.
:name

Name to give the Task; if not specified, a name will be

auto-generated when added to a queue and assigned to this object.

[:params]: Hash of parameters to use for this Task.

For POST requests these params will be encoded as
'application/x-www-form-urlencoded' and set to the payload.
For all other methods, the parameters will be converted to a
query string. May not be specified if the URL already
contains a query string.
:url

Relative URL where the webhook that should handle this task is

located for this application. May have a query string unless
this is a POST method.

Raises:

InvalidTaskError if any of the parameters are invalid;
InvalidTaskNameError if the task name is invalid; InvalidUrlError if
the task URL is invalid or too long; TaskTooLargeError if the task with
its payload is too large.


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/appengine-apis/labs/taskqueue.rb', line 86

def initialize(payload=nil, options={})
  if payload.kind_of? Hash
    options, payload = payload, nil
  elsif payload.kind_of? TaskOptions
    @task_options = payload
    return
  end
  options = options.dup
  if payload.kind_of? Blob
    options[:bytes] = payload
  elsif payload
    options[:payload] = payload
  end
  @task_options = convert_options(options)
end

Instance Method Details

#add(queue = nil) ⇒ Object

Adds this Task to a queue

Args:

  • queue: Name of the queue where this Task should be added. (optional)



114
115
116
117
118
# File 'lib/appengine-apis/labs/taskqueue.rb', line 114

def add(queue=nil)
  queue = Queue.new(queue) unless queue.kind_of? Queue
  @handle = queue.java_queue.add(_task)
  self
end

#enqueued?Boolean

Note: This will not check if this Task already exists in the queue.

Returns:

  • (Boolean)


105
106
107
# File 'lib/appengine-apis/labs/taskqueue.rb', line 105

def enqueued?
  !!@handle
end

#etaObject

Returns the Time when this Task will execute.



121
122
123
# File 'lib/appengine-apis/labs/taskqueue.rb', line 121

def eta
  Time.at(@handle.eta_millis / 1000.0) if @handle
end

#nameObject

Returns the name of this Task.

Will be nil if using an auto-assigned Task name and this Task has not yet been added to a Queue.



129
130
131
# File 'lib/appengine-apis/labs/taskqueue.rb', line 129

def name
  @handle.name if @handle
end

#queueObject

Returns the name of the Queue where this Task was enqueued.



134
135
136
# File 'lib/appengine-apis/labs/taskqueue.rb', line 134

def queue
  @handle.queue_name if @handle
end