Class: Beanpicker::Server

Inherits:
Object
  • Object
show all
Includes:
MsgLogger
Defined in:
lib/beanpicker/job_server.rb

Overview

Used by combine to run a server of jobs

Instance Method Summary collapse

Methods included from MsgLogger

#debug, #error, #fatal, #info, #log_handler=, #msg, #warn

Constructor Details

#initialize(args = ARGV) ⇒ Server

expect a list of files to load or pick from ARGV



15
16
17
# File 'lib/beanpicker/job_server.rb', line 15

def initialize(args=ARGV)
  @args = args
end

Instance Method Details

#log_handlerObject

Use the Beanpicker::log_handler to log messages



63
64
65
# File 'lib/beanpicker/job_server.rb', line 63

def log_handler
  Beanpicker::log_handler
end

#runObject

Create all workers and verify if exists any Worker or Worker::Child to start a loop. If no only exit



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/beanpicker/job_server.rb', line 21

def run
  debug "Hiring workers..."
  for arg in @args
    Worker.new arg
  end

  w = c = 0
  for worker in Beanpicker::workers
    w += 1
    m = "Worker: #{worker.name} hired to do"
    f = {}
    for child in worker.childs
      c += 1
      f[child.job_name] ||= 0
      f[child.job_name] += 1
    end
    for job in f.keys.sort
      m << " #{job}##{f[job]}"
    end
    debug m
  end

  if w == 0
    fatal ["NOBODY WANT TO WORK TO YOU!!",
           "Have you specified a file?",
           "e.g. #{$0} lib/my_file_with_jobs.rb"].join("\n")
    exit
  end

  if c == 0
    fatal ["ALL YOUR #{w} WORKER#{"S" if w > 1} ARE LAZY AND DON'T WANT TO DO ANYTHING!!",
           "Have you specified a job in any of yous files?",
           "e.g. job('no.lazy.worker') { |args| puts args[:my_money] }"].join("\n")
    exit
  end

  debug "#{w} worker#{"s" if w > 1} hired to do #{c} job#{"s" if c > 1}, counting the money..."
  
  loop { sleep 1 }
end