Module: Chapman

Extended by:
Chapman
Included in:
Chapman
Defined in:
lib/chapman.rb,
lib/chapman/worker.rb,
lib/chapman/version.rb,
lib/chapman/exceptions.rb

Defined Under Namespace

Modules: Exceptions Classes: Worker

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#all_jobsObject



101
102
103
# File 'lib/chapman.rb', line 101

def all_jobs
  @@handlers.keys
end

#beanstalk_urlObject



96
97
98
99
# File 'lib/chapman.rb', line 96

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

#before(&block) ⇒ Object



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

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

#before_handlersObject



109
110
111
# File 'lib/chapman.rb', line 109

def before_handlers
  @@before_handlers ||= []
end

#error(&blk) ⇒ Object



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

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

#error_handlerObject



113
114
115
# File 'lib/chapman.rb', line 113

def error_handler
  @@error_handler ||= nil
end

#job(j, &block) ⇒ Object



10
11
12
13
# File 'lib/chapman.rb', line 10

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

#job_handlersObject



105
106
107
# File 'lib/chapman.rb', line 105

def job_handlers
  @@handlers ||= {}
end

#log(msg) ⇒ Object



88
89
90
# File 'lib/chapman.rb', line 88

def log(msg)
  puts msg
end

#log_error(msg) ⇒ Object



92
93
94
# File 'lib/chapman.rb', line 92

def log_error(msg)
  STDERR.puts msg
end

#maintain_workersObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chapman.rb', line 58

def maintain_workers
  running.each_with_index do |runner, index|
    if not runner[:thread].alive?
      log "Dead Thread detected: #{runner[:thread].inspect}"
      w = Creeper::Worker.new()
      t =  Thread.new do
        w.work(jobs)
    end
      running[index] = {thread: t, worker: w}
    end
  end
end

#murder_workers!Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/chapman.rb', line 71

def murder_workers!
  running.each do |runner|
    if runner[:worker].job_in_progress?
      log "Murder [scheduling]"
    else
      log "Murder [now]"
      runner[:thread].kill
    end
  end
end

#reap_workersObject



82
83
84
85
86
# File 'lib/chapman.rb', line 82

def reap_workers
  running.each do |runner|
    runner[:thread].join
  end
end

#reset!Object



117
118
119
120
121
122
123
# File 'lib/chapman.rb', line 117

def reset!
  @@soft_quit       = false
  @@running         = []
  @@handlers        = nil
  @@before_handlers = nil
  @@error_handler   = nil
end

#runningObject



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

def running
  @@running ||= []
end

#soft_quit=(soft_quit) ⇒ Object



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

def soft_quit=(soft_quit)
  @@soft_quit = soft_quit
end

#soft_quit?Boolean

Returns:

  • (Boolean)


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

def soft_quit?
  @@soft_quit ||= false
end

#work(jobs = nil, thread_count = 1) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chapman.rb', line 36

def work(jobs=nil, thread_count=1)

  # start a worker thread
  thread_count.times do
    w = Chapman::Worker.new()
    t = Thread.new { w.work(jobs) }
    running << {thread: t, worker: w}
  end

  # keep them alive
  while not soft_quit?
    maintain_workers
    sleep 60
  end

  murder_workers!

  reap_workers

  log "SEPPUKU!!"
end