Class: BeanstalkCommander

Inherits:
Object
  • Object
show all
Defined in:
bin/ayl_beanstalk

Overview

This class provides the commands for the script. Each public instance method on the class is exposed as a command by the script. So, for example, if the user specifies ‘beanstalk_info.rb statistics’ on the command-line, the script will invoke the BeanstalkCommander#statistics method.

When the user asks the script for help (‘beanstalk_info.rb –help’), the help implementation will ask for the public instance methods for the BeanstalkCommander class so that we can present the user with the valid commands

Instance Method Summary collapse

Constructor Details

#initialize(host, port, tube) ⇒ BeanstalkCommander

Returns a new instance of BeanstalkCommander.



29
30
31
32
33
# File 'bin/ayl_beanstalk', line 29

def initialize(host, port, tube)
  @host = host
  @port = port
  @tube = tube
end

Instance Method Details

#delete(job_number) ⇒ Object

Deletes the job with the specified job id.



86
87
88
89
# File 'bin/ayl_beanstalk', line 86

def delete(job_number)
  raise "Must specify a job number to delete" unless job_number
  pool.open_connections.first.delete(job_number)
end

#eat_all_jobsObject



170
171
172
173
174
175
176
177
178
# File 'bin/ayl_beanstalk', line 170

def eat_all_jobs
  pool.watch(@tube)
  while (!(job = pool.reserve(0)).nil?)
    puts "Reserved job"
    format_job(job)
    job.delete
    puts "Deleted job"
  end
end

#eat_job(*args) ⇒ Object

Reserves the current job on the queue, displays it, and removes it from the queue. The operation is performed against the current queue.



159
160
161
162
163
164
165
166
167
168
# File 'bin/ayl_beanstalk', line 159

def eat_job(*args)
  # Reserve the next job on the queue
  pool.watch(@tube)
  job = pool.reserve(10)
  raise "No job to reserve" unless job
  puts "Reserved job"
  format_job(job)
  job.delete
  puts "Deleted job"
end

#job_statistics(job_number) ⇒ Object

Lists the statistics for the specified job number



38
39
40
41
# File 'bin/ayl_beanstalk', line 38

def job_statistics(job_number)
  raise "Must specify a job number to get the statistics for" unless job_number
  format_statistics(pool.open_connections.first.job_stats(job_number))
end

#kick(*args) ⇒ Object

Kick any buried/delayed jobs in te queue



103
104
105
106
# File 'bin/ayl_beanstalk', line 103

def kick(*args)
  pool.use(@tube)
  pool.open_connections.first.kick 1
end

#kill_workerObject



137
138
139
# File 'bin/ayl_beanstalk', line 137

def kill_worker
  put_ayl_job 'Kernel.exit'
end

#list_tubes(*args) ⇒ Object

Lists the tubes active on beanstalk



63
64
65
66
67
68
69
70
71
# File 'bin/ayl_beanstalk', line 63

def list_tubes(*args)
  tubes = pool.list_tubes
  puts "Tubes in use"
  puts "------------"
  tubes.each_pair do | host, tubes |
    puts host
    tubes.each { | tube | puts " - #{tube}" }
  end
end

#move_jobs(to_tube) ⇒ Object

General purpose tube cleaning mechanism. This allows you to move all the jobs from one tube to another within beanstalk. The tube specified on the command line (-t tube), will be used as the ‘from-tube’ and the argument to the move_jobs command is the ‘to-tube’. When done, you will be a a time-out error (because the ‘reserve’ command timed out without finding a job).

It is fast and awesome.



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'bin/ayl_beanstalk', line 189

def move_jobs(to_tube)
  pool.watch(@tube)
  pool.use(to_tube)
  while true
    job = pool.reserve(0) 
    if job
      pool.put(job.body)
      job.delete
    else
      break
    end
  end
end

#peek_buried(*args) ⇒ Object

Displays the most current job on the buried queue



120
121
122
123
124
# File 'bin/ayl_beanstalk', line 120

def peek_buried(*args)
  pool.use(@tube)
  job = pool.peek_buried
  format_job(job)
end

#peek_delayed(*args) ⇒ Object

Displays the most current job on the delayed queue



111
112
113
114
115
# File 'bin/ayl_beanstalk', line 111

def peek_delayed(*args)
  pool.use(@tube)
  job = pool.peek_delayed
  format_job(job)
end

#peek_job(job_number) ⇒ Object

Displays the job information for the specified job id without reserving the job or removing it from the queue.



77
78
79
80
81
# File 'bin/ayl_beanstalk', line 77

def peek_job(job_number)
  raise "Must specify a job number to peek" unless job_number
  job = pool.peek_job(job_number)
  format_job(job)
end

#peek_ready(*args) ⇒ Object

Displays the most current job on the ready queue



94
95
96
97
98
# File 'bin/ayl_beanstalk', line 94

def peek_ready(*args)
  pool.use(@tube)
  job = pool.peek_ready
  format_job(job)
end

#put_ayl_job(job_body) ⇒ Object



141
142
143
144
145
146
147
# File 'bin/ayl_beanstalk', line 141

def put_ayl_job(job_body)
  raise "Must specify a job body to put" unless job_body
  puts "Putting (#{job_body}) on tube: #{@tube}"
  pool.use(@tube)
  pool.put({:type => :ayl, :code => job_body}.to_yaml)
  puts "Job was put"
end

#put_job(job_body) ⇒ Object

Puts a generic job on the queue (job is specified on the command line)



129
130
131
132
133
134
135
# File 'bin/ayl_beanstalk', line 129

def put_job(job_body)
  raise "Must specify a job body to put" unless job_body
  puts "Putting (#{job_body}) on tube: #{@tube}"
  pool.use(@tube)
  pool.put(job_body)
  puts "Job was put"
end

#put_sleep_job(*args) ⇒ Object



149
150
151
152
153
# File 'bin/ayl_beanstalk', line 149

def put_sleep_job(*args)
  puts "Putting job on tube '#{@tube}' that will sleep for 20 seconds."
  pool.use(@tube)
  pool.put(SLEEP_COMMAND.to_yaml)
end

#statistics(*args) ⇒ Object

Lists the overall statistics for beanstalk



54
55
56
57
58
# File 'bin/ayl_beanstalk', line 54

def statistics(*args)
  puts "Overall Statistics"
  puts "------------------"
  format_statistics(pool.stats)
end

#tube_statistics(*args) ⇒ Object

Lists the statistics for the current tube on beanstalk



46
47
48
49
# File 'bin/ayl_beanstalk', line 46

def tube_statistics(*args)
  puts "Statistics for Tube: #{@tube}"
  format_statistics(pool.stats_tube(@tube))
end