summaryrefslogtreecommitdiff
path: root/lib/gitlab/daemon.rb
blob: 49828e54d7e962f114b438855721d4163f2835cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# frozen_string_literal: true

module Gitlab
  # DEPRECATED. Use Gitlab::BackgroundTask for new code instead.
  class Daemon
    # 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.
    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

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

    attr_reader :thread

    def thread?
      !thread.nil?
    end

    # Possible options:
    # - synchronous [Boolean] if true, turns `start` into a blocking call
    def initialize(**options)
      @synchronous = options[:synchronous]
      @mutex = Mutex.new
    end

    def enabled?
      true
    end

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

    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

    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

    private

    # Executed in lock context before starting thread
    # Needs to return success
    def start_working
      true
    end

    # Executed in separate thread
    def run_thread
      raise NotImplementedError
    end

    # Executed in lock context
    def stop_working
      # no-ops
    end
  end
end