Class: Skiplock::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/skiplock/manager.rb

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



3
4
5
6
7
8
# File 'lib/skiplock/manager.rb', line 3

def initialize
  @config = Skiplock::DEFAULT_CONFIG.dup
  @config.merge!(YAML.load_file('config/skiplock.yml')) rescue nil
  @config.symbolize_keys!
  async if (caller.any?{ |l| l =~ %r{/rack/} } && @config[:workers] == 0)
end

Instance Method Details

#asyncObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/skiplock/manager.rb', line 10

def async
  setup_logger
  configure
  Worker.cleanup(@hostname)
  @worker = Worker.generate(capacity: @config[:max_threads], hostname: @hostname)
  Cron.setup if @worker.master
  @worker.start(**@config)
  at_exit { @worker.shutdown }
rescue Exception => ex
  @logger.error(ex.to_s)
  @logger.error(ex.backtrace.join("\n"))
end

#standalone(**options) ⇒ Object



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
61
62
63
# File 'lib/skiplock/manager.rb', line 23

def standalone(**options)
  @config.merge!(options)
  @config[:standalone] = true
  @config[:workers] = 1 if @config[:workers] <= 0
  setup_logger
  configure
  banner
  @parent_id = Process.pid
  @shutdown = false
  Signal.trap('INT') { @shutdown = true }
  Signal.trap('TERM') { @shutdown = true }
  Signal.trap('HUP') { setup_logger }
  Worker.cleanup(@hostname)
  @worker = Worker.generate(capacity: @config[:max_threads], hostname: @hostname)
  ActiveRecord::Base.connection.disconnect! if @config[:workers] > 1
  (@config[:workers] - 1).times do |n|
    fork do
      sleep(0.25*n + 1)
      ActiveRecord::Base.establish_connection
      worker = Worker.generate(capacity: @config[:max_threads], hostname: @hostname, master: false)
      worker.start(worker_num: n + 1, **@config)
      loop do
        sleep 0.5
        break if @shutdown || Process.ppid != @parent_id
      end
      worker.shutdown
    end
  end
  ActiveRecord::Base.establish_connection if @config[:workers] > 1
  @worker.start(**@config)
  loop do
    sleep 0.5
    break if @shutdown
  end
  @logger.info "[Skiplock] Terminating signal... Waiting for jobs to finish (up to #{@config[:graceful_shutdown]} seconds)..." if @config[:graceful_shutdown]
  Process.waitall
  @worker.shutdown
rescue Exception => ex
  @logger.error(ex.to_s)
  @logger.error(ex.backtrace.join("\n"))
end