Class: TQ::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/tq/queue.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ 'lease_secs' => 60, 'num_tasks' => 1 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, api, options = {}) ⇒ Queue

Returns a new instance of Queue.



9
10
11
12
# File 'lib/tq/queue.rb', line 9

def initialize(client, api, options={})
  @client, @api = client, api
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



8
9
10
# File 'lib/tq/queue.rb', line 8

def api
  @api
end

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/tq/queue.rb', line 8

def client
  @client
end

Instance Method Details

#extend!(task, secs = nil) ⇒ Object

note: does not currently work; filed bug report code.google.com/p/googleappengine/issues/detail?id=11838



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tq/queue.rb', line 41

def extend!(task, secs=nil)
  secs = secs.nil? ? @options['lease_secs'] : secs
  opts = @options
  results = client.execute!(
              :api_method => api.tasks.update,
              :parameters => { :newLeaseSeconds => secs, 
                               :project => opts['project'], 
                               :taskqueue => opts['name'], 
                               :task => task.id
                             }
            )
  new_task(results.data)
end

#finish!(task) ⇒ Object

note: you must have previously leased given task



73
74
75
76
77
78
79
80
81
82
# File 'lib/tq/queue.rb', line 73

def finish!(task)
  opts = @options
  client.execute!( :api_method => api.tasks.delete,
                   :parameters => { :project   => opts['project'],
                                    :taskqueue => opts['name'],
                                    :task      => task.id
                                  }
                )
  return
end

#lease!(opts = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tq/queue.rb', line 26

def lease!(opts={})
  opts = @options.merge(opts)
  results = client.execute!(  
              :api_method => api.tasks.lease,
              :parameters => { :leaseSecs => opts['lease_secs'], 
                               :project => opts['project'], 
                               :taskqueue => opts['name'], 
                               :numTasks => opts['num_tasks']
                             }
            )
  items = (results.data && results.data['items']) || []
  items.map {|t| new_task(t) } 
end

#name(_) ⇒ Object



22
23
24
# File 'lib/tq/queue.rb', line 22

def name(_)
  options({'name' => _})
end

#options(_) ⇒ Object



14
15
16
# File 'lib/tq/queue.rb', line 14

def options(_)
  Queue.new @client, @api, @options.merge(_)
end

#project(_) ⇒ Object



18
19
20
# File 'lib/tq/queue.rb', line 18

def project(_)
  options({'project' => _})
end

#push!(payload, tag = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tq/queue.rb', line 55

def push!(payload, tag=nil)
  opts = @options
  body = { 'queueName'     => opts['name'],
           'payloadBase64' => encode(payload)
         }
  body['tag'] = tag if tag
  
  results = client.execute!( 
              :api_method => api.tasks.insert,
              :parameters => { :project   => opts['project'],
                               :taskqueue => opts['name']
                             },
              :body_object => body
            )
  new_task(results.data)
end