Module: Stalker

Extended by:
Stalker
Included in:
Stalker
Defined in:
lib/stalker.rb

Defined Under Namespace

Classes: BadURL, JobTimeout, NoJobsDefined, NoSuchJob

Instance Method Summary collapse

Instance Method Details

#all_jobsObject



175
176
177
# File 'lib/stalker.rb', line 175

def all_jobs
  @@handlers.keys
end

#beanstalkObject



142
143
144
# File 'lib/stalker.rb', line 142

def beanstalk
  @@beanstalk ||= Beanstalk::Pool.new(beanstalk_addresses)
end

#beanstalk_addressesObject



153
154
155
156
# File 'lib/stalker.rb', line 153

def beanstalk_addresses
  uris = beanstalk_url.split(/[\s,]+/)
  uris.map {|uri| beanstalk_host_and_port(uri)}
end

#beanstalk_host_and_port(uri_string) ⇒ Object

Raises:



158
159
160
161
162
# File 'lib/stalker.rb', line 158

def beanstalk_host_and_port(uri_string)
  uri = URI.parse(uri_string)
  raise(BadURL, uri_string) if uri.scheme != 'beanstalk'
  "#{uri.host}:#{uri.port || 11300}"
end

#beanstalk_urlObject



146
147
148
149
# File 'lib/stalker.rb', line 146

def beanstalk_url
  return @@url if defined?(@@url) and @@url
  ENV['BEANSTALK_URL'] || 'beanstalk://localhost/'
end

#before(&block) ⇒ Object



29
30
31
32
# File 'lib/stalker.rb', line 29

def before(&block)
  @@before_handlers ||= []
  @@before_handlers << block
end

#clear!Object



183
184
185
186
187
# File 'lib/stalker.rb', line 183

def clear!
  @@handlers = nil
  @@before_handlers = nil
  @@error_handler = nil
end

#connect(url) ⇒ Object



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

def connect(url)
  @@url = url
  beanstalk
end

#enqueue(job, args = {}, opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/stalker.rb', line 14

def enqueue(job, args={}, opts={})
  pri   = opts[:pri]   || 65536
  delay = [0, opts[:delay].to_i].max  
  ttr   = opts[:ttr]   || 120
  beanstalk.use job
  beanstalk.put [ job, args ].to_json, pri, delay, ttr
rescue Beanstalk::NotConnected => e
  failed_connection(e)
end

#error(&blk) ⇒ Object



34
35
36
# File 'lib/stalker.rb', line 34

def error(&blk)
  @@error_handler = blk
end

#error_handlerObject



179
180
181
# File 'lib/stalker.rb', line 179

def error_handler
  @@error_handler
end

#exception_message(e) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/stalker.rb', line 164

def exception_message(e)
  msg = [ "Exception #{e.class} -> #{e.message}" ]

  base = File.expand_path(Dir.pwd) + '/'
  e.backtrace.each do |t|
    msg << "   #{File.expand_path(t).gsub(/#{base}/, '')}"
  end

  msg.join("\n")
end

#failed_connection(e) ⇒ Object



108
109
110
111
112
113
# File 'lib/stalker.rb', line 108

def failed_connection(e)
  log_error exception_message(e)
  log_error "*** Failed connection to #{beanstalk_url}"
  log_error "*** Check that beanstalkd is running (or set a different BEANSTALK_URL)"
  exit 1
end

#job(j, &block) ⇒ Object



24
25
26
27
# File 'lib/stalker.rb', line 24

def job(j, &block)
  @@handlers ||= {}
  @@handlers[j] = block
end

#log(msg) ⇒ Object



134
135
136
# File 'lib/stalker.rb', line 134

def log(msg)
  puts msg
end

#log_error(msg) ⇒ Object



138
139
140
# File 'lib/stalker.rb', line 138

def log_error(msg)
  STDERR.puts msg
end

#log_job_begin(name, args) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/stalker.rb', line 115

def log_job_begin(name, args)
  args_flat = unless args.empty?
    '(' + args.inject([]) do |accum, (key,value)|
      accum << "#{key}=#{value}"
    end.join(' ') + ')'
  else
    ''
  end

  log [ "Working", name, args_flat ].join(' ')
  @job_begun = Time.now
end

#log_job_end(name, failed = false) ⇒ Object



128
129
130
131
132
# File 'lib/stalker.rb', line 128

def log_job_end(name, failed=false)
  ellapsed = Time.now - @job_begun
  ms = (ellapsed.to_f * 1000).to_i
  log "Finished #{name} in #{ms}ms #{failed ? ' (failed)' : ''}"
end

#prep(jobs = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stalker.rb', line 41

def prep(jobs=nil)
  raise NoJobsDefined unless defined?(@@handlers)
  @@error_handler = nil unless defined?(@@error_handler)

  jobs ||= all_jobs

  jobs.each do |job|
    raise(NoSuchJob, job) unless @@handlers[job]
  end

  log "Working #{jobs.size} jobs: [ #{jobs.join(' ')} ]"

  jobs.each { |job| beanstalk.watch(job) }

  beanstalk.list_tubes_watched.each do |server, tubes|
    tubes.each { |tube| beanstalk.ignore(tube) unless jobs.include?(tube) }
  end
rescue Beanstalk::NotConnected => e
  failed_connection(e)
end

#work(jobs = nil) ⇒ Object



62
63
64
65
# File 'lib/stalker.rb', line 62

def work(jobs=nil)
  prep(jobs)
  loop { work_one_job }
end

#work_one_jobObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/stalker.rb', line 69

def work_one_job
  job = beanstalk.reserve
  name, args = JSON.parse job.body
  log_job_begin(name, args)
  handler = @@handlers[name]
  raise(NoSuchJob, name) unless handler

  begin
    Timeout::timeout(job.ttr - 1) do
      if defined? @@before_handlers and @@before_handlers.respond_to? :each
        @@before_handlers.each do |block|
          block.call(name)
        end
      end
      handler.call(args)
    end
  rescue Timeout::Error
    raise JobTimeout, "#{name} hit #{job.ttr-1}s timeout"
  end

  job.delete
  log_job_end(name)
rescue Beanstalk::NotConnected => e
  failed_connection(e)
rescue SystemExit
  raise
rescue => e
  log_error exception_message(e)
  job.bury rescue nil
log_job_end(name, 'failed') if @job_begun
  if error_handler
    if error_handler.arity == 1
      error_handler.call(e)
    else
      error_handler.call(e, name, args)
    end
  end
end