module Beanpicker

The fucking master job DSL to beanstalkd

Just use it and go to beach ;)

Constants

VERSION

A array with the version [MAJOR, MINOR, PATCH]

VERSION_STRING

A string of the array joined by “.”

Public Instance Methods

add_worker(worker) click to toggle source

Add a worker to the list of workers

# File lib/beanpicker.rb, line 254
def add_worker(worker)
  workers << worker
end
beanstalk() click to toggle source

Return the default beanstalk connection os create a new one with new_beanstalk

# File lib/beanpicker.rb, line 113
def beanstalk
  @@beanstalk ||= new_beanstalk
end
beanstalk_urls() click to toggle source

Look in ENV and ENV for beanstalk urls and process returning a array.

If don’t find a good url it will return a array with just “localhost:11300”(default beanstalk port)

# File lib/beanpicker.rb, line 125
def beanstalk_urls
  urls = [ENV['BEANSTALK_URL'], ENV['BEANSTALK_URLS']].compact.join(",").split(",").map do |url|
    if url =~ /^beanstalk:\/\//
      uri = URI.parse(url)
      url = "#{uri.host}:#{uri.port}"
    else
      url = url.gsub(/^([^:\/]+)(:(\d+)).*$/) { "#{$1}:#{$3 || 11300}" }
    end
    url =~ /^[^:\/]+:\d+$/ ? url : nil
  end.compact
  urls.empty? ? ["localhost:11300"] : urls
end
default_childs_number() click to toggle source

Return the default number of childs that a Worker should create(1 by default)

This is used by Worker::Child::process

# File lib/beanpicker.rb, line 186
def default_childs_number
  @@default_childs_number ||= 1
end
default_childs_number=(v) click to toggle source

Set the default childs number

# File lib/beanpicker.rb, line 191
def default_childs_number=(v)
  @@default_childs_number = v
end
default_delay() click to toggle source

Return the default delay(0 by default)

This is used by enqueue

# File lib/beanpicker.rb, line 162
def default_delay
  @@default_delay ||= 0
end
default_delay=(v) click to toggle source

Set the default delay

# File lib/beanpicker.rb, line 167
def default_delay=(v)
  @@default_delay = v
end
default_fork_every() click to toggle source

Return if a child should fork every time that a job will process. This option is overwrited by job options and fork_every

This is used by Worker::Child

# File lib/beanpicker.rb, line 199
def default_fork_every
  defined?(@@default_fork_every) ? @@default_fork_every : true
end
default_fork_every=(v) click to toggle source

Set the default_fork_every

# File lib/beanpicker.rb, line 204
def default_fork_every=(v)
  @@default_fork_every = !!v
end
default_fork_master() click to toggle source

Return if a child should fork itself on intialize. This should be used when default_fork_every is false. This option is overwrited by job options and fork_master

Use it only if the jobs need high speed and are “memory leak”-safe

This is used by Worker::Child

# File lib/beanpicker.rb, line 215
def default_fork_master
  defined?(@@default_fork_master) ? @@default_fork_master : false
end
default_fork_master=(v) click to toggle source

Set the default_fork_master

# File lib/beanpicker.rb, line 220
def default_fork_master=(v)
  @@default_fork_master = !!v
end
default_pri() click to toggle source

Return the default priority(65536 by default).

This is used by enqueue

# File lib/beanpicker.rb, line 150
def default_pri
  @@default_pri ||= 65536
end
default_pri=(v) click to toggle source

Set the default priority

# File lib/beanpicker.rb, line 155
def default_pri=(v)
  @@default_pri = v
end
default_ttr() click to toggle source

Set the default time-to-work(120 by default)

This is used by enqueue

# File lib/beanpicker.rb, line 174
def default_ttr
  @@default_ttr ||= 120
end
default_ttr=(v) click to toggle source

Set the default time-to-work

# File lib/beanpicker.rb, line 179
def default_ttr=(v)
  @@default_ttr = v
end
enqueue(jobs, args={}, o={}) click to toggle source

Send a new queue to beanstalkd

The first argument is a String with the name of job or a Array of Strings to do job chains

The second argument should be any object that will be passed in a YAML format to the job

The third argument should be a hash containing :pri(priority) => Integer, :delay => Integer and :ttr(time-to-work) => Integer

If beanstalk raise a Beanstalk::NotConnected, enqueue will create a new instance of beanstalk connection and retry. If it raise again, enqueue will raise the error

# File lib/beanpicker.rb, line 90
def enqueue(jobs, args={}, o={})
  opts = [
    o[:pri]   || default_pri,
    o[:delay] || default_delay,
    o[:ttr]   || default_ttr
  ]

  jobs = [jobs.to_s] unless jobs.is_a?(Array)
  jobs.compact!
  raise ArgumentError, "you need at least 1 job" if jobs.empty?
  job = jobs.first

  beanstalk.use(job)
  beanstalk.yput({ :args => args, :next_jobs => jobs[1..-1]}, *opts)
rescue Beanstalk::NotConnected => e
  raise e if defined?(r)
  r = true
  error exception_message(e, "You have a problem with beanstalkd.\nIs it running?")
  @@beanstalk = new_beanstalk
  retry
end
exception_message(e, msg=nil) click to toggle source

Helper to should a exception message

# File lib/beanpicker.rb, line 139
def exception_message(e, msg=nil)
  m = []
  m << msg if msg
  m << e.message
  m += e.backtrace
  m.join("\n")
end
fork_every() click to toggle source

See default_fork_every

This option overwrite all others

# File lib/beanpicker.rb, line 227
def fork_every
  defined?(@@fork_every) ? @@fork_every : nil
end
fork_every=(v) click to toggle source

Set the fork_every

# File lib/beanpicker.rb, line 232
def fork_every=(v)
  @@fork_every = v.nil? ? nil : !!v
end
fork_master() click to toggle source

See default_fork_master

This option overwrite all others

# File lib/beanpicker.rb, line 239
def fork_master
  defined?(@@fork_master) ? @@fork_master : nil
end
fork_master=(v) click to toggle source

Set the fork_master

# File lib/beanpicker.rb, line 244
def fork_master=(v)
  @@fork_master = v.nil? ? nil : !!v
end
new_beanstalk() click to toggle source

Create a new beanstalk connection using the urls from beanstalk_urls

# File lib/beanpicker.rb, line 118
def new_beanstalk
  Beanstalk::Pool.new(beanstalk_urls)
end
stop_workers() click to toggle source

Call die! for all childs of every worker and clear the list See workers

# File lib/beanpicker.rb, line 260
def stop_workers
  for worker in workers
    for child in worker.childs
      child.die!
    end
  end
  workers.clear
end
workers() click to toggle source

Return a Array with the workers registered

# File lib/beanpicker.rb, line 249
def workers
  @@workers ||= []
end