Class: Honcho::Adapters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/honcho/adapters/base.rb

Direct Known Subclasses

Resque, Sidekiq

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, redis:, runner:) ⇒ Base

Returns a new instance of Base.



4
5
6
7
8
9
10
# File 'lib/honcho/adapters/base.rb', line 4

def initialize(config:, redis:, runner:)
  @config = config
  @redis = redis
  @runner = runner
  @running = false
  @stopping = false
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/honcho/adapters/base.rb', line 12

def config
  @config
end

#redisObject (readonly)

Returns the value of attribute redis.



12
13
14
# File 'lib/honcho/adapters/base.rb', line 12

def redis
  @redis
end

#runnerObject (readonly)

Returns the value of attribute runner.



12
13
14
# File 'lib/honcho/adapters/base.rb', line 12

def runner
  @runner
end

#runningObject (readonly)

Returns the value of attribute running.



12
13
14
# File 'lib/honcho/adapters/base.rb', line 12

def running
  @running
end

#stoppingObject (readonly)

Returns the value of attribute stopping.



12
13
14
# File 'lib/honcho/adapters/base.rb', line 12

def stopping
  @stopping
end

Instance Method Details

#check_for_workObject



18
19
20
21
22
23
24
# File 'lib/honcho/adapters/base.rb', line 18

def check_for_work
  if run?
    start
  else
    stop
  end
end

#commandsObject



30
31
32
# File 'lib/honcho/adapters/base.rb', line 30

def commands
  Array(config['command'])
end

#nameObject



26
27
28
# File 'lib/honcho/adapters/base.rb', line 26

def name
  config['name']
end

#pathObject



34
35
36
# File 'lib/honcho/adapters/base.rb', line 34

def path
  config['path']
end

#really_stopObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/honcho/adapters/base.rb', line 82

def really_stop
  @stopping = false
  return unless running?
  log(name, "STOPPING\n")
  running.each do |(pid, wout)|
    Process.kill('-TERM', pid)
    wout.close
  end
  @running = false
end

#run?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/honcho/adapters/base.rb', line 38

def run?
  work_to_do? || work_being_done?
end

#running?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/honcho/adapters/base.rb', line 42

def running?
  @running != false
end

#should_stop?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/honcho/adapters/base.rb', line 78

def should_stop?
  stopping? && stopping <= 0
end

#startObject



50
51
52
53
54
55
# File 'lib/honcho/adapters/base.rb', line 50

def start
  @stopping = false
  return if running?
  log(name, "STARTING\n")
  @running = start_command
end

#start_commandObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/honcho/adapters/base.rb', line 57

def start_command
  commands.map do |cmd|
    rout, wout = IO.pipe
    pid = spawn(path, cmd, wout)
    Thread.new do
      log(name, rout.gets) until rout.eof?
    end
    [pid, wout]
  end
end

#stopObject



68
69
70
71
72
73
74
75
76
# File 'lib/honcho/adapters/base.rb', line 68

def stop
  return unless running?
  if should_stop?
    really_stop
  else
    @stopping ||= stop_delay
    @stopping -= interval
  end
end

#stopping?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/honcho/adapters/base.rb', line 46

def stopping?
  @stopping != false
end

#total_countObject



93
94
95
# File 'lib/honcho/adapters/base.rb', line 93

def total_count
  queued_count + busy_count
end

#typeObject



14
15
16
# File 'lib/honcho/adapters/base.rb', line 14

def type
  self.class.name.split(':').last.downcase
end