Class: Spool::Pool

Inherits:
Object
  • Object
show all
Defined in:
lib/spool/pool.rb

Constant Summary collapse

CHECK_TIMEOUT =
0.01
SIGNALS =
{
  INT:  :stop!,
  TERM: :stop!,
  QUIT: :stop,
  HUP:  :reload,
  USR2: :restart,
  TTIN: :incr,
  TTOU: :decr
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = nil, &block) ⇒ Pool

Returns a new instance of Pool.



18
19
20
21
22
23
# File 'lib/spool/pool.rb', line 18

def initialize(configuration=nil, &block)
  @configuration = configuration || DSL.configure(&block)
  @processes = []
  @running = false
  @actions_queue = []
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



16
17
18
# File 'lib/spool/pool.rb', line 16

def configuration
  @configuration
end

#processesObject (readonly)

Returns the value of attribute processes.



16
17
18
# File 'lib/spool/pool.rb', line 16

def processes
  @processes
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/spool/pool.rb', line 25

def running?
  @running
end

#startObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/spool/pool.rb', line 40

def start
  @running = true

  handle_signals

  File.write configuration.pid_file, Process.pid if configuration.pid_file

  configuration.processes.times.map do
    processes << Spawner.spawn(configuration)
  end

  logger.info(self.class) { "SPOOL START childrens: #{processes.map(&:pid)}" }

  while running?
    action = actions_queue.pop
    
    if action
      logger.info(self.class) { "Starting action #{action[:name]} with params: [#{action[:args].join(', ')}]" }
      send action[:name], *action[:args] 
    end

    if running?
      check_status
      sleep CHECK_TIMEOUT
    end
  end

  logger.info(self.class) { "Spool finished successfully!" }
end

#stopped?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/spool/pool.rb', line 29

def stopped?
  !running?
end