Class: Gitlab::Daemon

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

Overview

DEPRECATED. Use Gitlab::BackgroundTask for new code instead.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Daemon

Possible options:

  • synchronous [Boolean] if true, turns ‘start` into a blocking call



36
37
38
39
# File 'lib/gitlab/daemon.rb', line 36

def initialize(**options)
  @synchronous = options[:synchronous]
  @mutex = Mutex.new
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



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

def thread
  @thread
end

Class Method Details

.initialize_instance(*args, recreate: false, **options) ⇒ Object

Options:

  • recreate: We usually only allow a single instance per process to exist;

    this can be overridden with this switch, so that existing
    instances are stopped and recreated.
    


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

def self.initialize_instance(*args, recreate: false, **options)
  if @instance
    if recreate
      @instance.stop
    else
      raise "#{name} singleton instance already initialized"
    end
  end

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

.instanceObject



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

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

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/gitlab/daemon.rb', line 41

def enabled?
  true
end

#startObject



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

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

      @thread.join if @synchronous

      @thread
    end
  end
end

#stopObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gitlab/daemon.rb', line 68

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)


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

def thread?
  !thread.nil?
end

#thread_nameObject



45
46
47
# File 'lib/gitlab/daemon.rb', line 45

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