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, 
  'max_tries' => -1 
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Queue.



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

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.



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

def api
  @api
end

#clientObject (readonly)

Returns the value of attribute client.



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

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



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tq/queue.rb', line 51

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



83
84
85
86
87
88
89
90
91
92
# File 'lib/tq/queue.rb', line 83

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



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tq/queue.rb', line 36

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



28
29
30
# File 'lib/tq/queue.rb', line 28

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

#option(key) ⇒ Object



32
33
34
# File 'lib/tq/queue.rb', line 32

def option(key)
  @options[key]
end

#options(_) ⇒ Object



20
21
22
# File 'lib/tq/queue.rb', line 20

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

#project(_) ⇒ Object



24
25
26
# File 'lib/tq/queue.rb', line 24

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

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tq/queue.rb', line 65

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