debian-mirror-gitlab/lib/gitlab/daemon.rb

64 lines
1 KiB
Ruby
Raw Normal View History

2017-09-10 17:25:29 +05:30
module Gitlab
class Daemon
def self.initialize_instance(*args)
raise "#{name} singleton instance already initialized" if @instance
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
@instance = new(*args)
Kernel.at_exit(&@instance.method(:stop))
@instance
end
def self.instance
@instance ||= initialize_instance
end
attr_reader :thread
def thread?
!thread.nil?
end
def initialize
@mutex = Mutex.new
end
def enabled?
true
end
def start
return unless enabled?
@mutex.synchronize do
return thread if thread?
@thread = Thread.new { start_working }
end
end
def stop
@mutex.synchronize do
return unless thread?
stop_working
if thread
thread.wakeup if thread.alive?
2018-03-17 18:26:18 +05:30
thread.join unless Thread.current == thread
2017-09-10 17:25:29 +05:30
@thread = nil
end
end
end
private
def start_working
raise NotImplementedError
end
def stop_working
# no-ops
end
end
end