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
|