Class: Gitlab::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/daemon.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDaemon

Returns a new instance of Daemon.


23
24
25
# File 'lib/gitlab/daemon.rb', line 23

def initialize
  @mutex = Mutex.new
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread


17
18
19
# File 'lib/gitlab/daemon.rb', line 17

def thread
  @thread
end

Class Method Details

.initialize_instance(*args) ⇒ Object


5
6
7
8
9
10
11
# File 'lib/gitlab/daemon.rb', line 5

def self.initialize_instance(*args)
  raise "#{name} singleton instance already initialized" if @instance

  @instance = new(*args)
  Kernel.at_exit(&@instance.method(:stop))
  @instance
end

.instance(*args) ⇒ Object


13
14
15
# File 'lib/gitlab/daemon.rb', line 13

def self.instance(*args)
  @instance ||= initialize_instance(*args)
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/gitlab/daemon.rb', line 27

def enabled?
  true
end

#startObject


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gitlab/daemon.rb', line 35

def start
  return unless enabled?

  @mutex.synchronize do
    break thread if thread?

    if start_working
      @thread = Thread.new do
        Thread.current.name = thread_name
        run_thread
      end
    end
  end
end

#stopObject


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gitlab/daemon.rb', line 50

def stop
  @mutex.synchronize do
    break unless thread?

    stop_working

    if thread
      thread.wakeup if thread.alive?
      begin
        thread.join unless Thread.current == thread
      rescue Exception # rubocop:disable Lint/RescueException
      end
      @thread = nil
    end
  end
end

#thread?Boolean

Returns:

  • (Boolean)

19
20
21
# File 'lib/gitlab/daemon.rb', line 19

def thread?
  !thread.nil?
end

#thread_nameObject


31
32
33
# File 'lib/gitlab/daemon.rb', line 31

def thread_name
  self.class.name.demodulize.underscore
end