Class: Gitlab::Daemon
- Inherits:
-
Object
- Object
- Gitlab::Daemon
- Defined in:
- lib/gitlab/daemon.rb
Overview
DEPRECATED. Use Gitlab::BackgroundTask for new code instead.
Direct Known Subclasses
HealthChecks::Server, Memory::ReportsDaemon, Metrics::Exporter::BaseExporter, Metrics::Samplers::BaseSampler, ProcessSupervisor, SidekiqDaemon::Monitor
Instance Attribute Summary collapse
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Class Method Summary collapse
-
.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.
- .instance ⇒ Object
Instance Method Summary collapse
- #enabled? ⇒ Boolean
-
#initialize(**options) ⇒ Daemon
constructor
Possible options: - synchronous [Boolean] if true, turns ‘start` into a blocking call.
- #start ⇒ Object
- #stop ⇒ Object
- #thread? ⇒ Boolean
- #thread_name ⇒ Object
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(**) @synchronous = [:synchronous] @mutex = Mutex.new end |
Instance Attribute Details
#thread ⇒ Object (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, **) if @instance if recreate @instance.stop else raise "#{name} singleton instance already initialized" end end @instance = new(*args, **) Kernel.at_exit(&@instance.method(:stop)) @instance end |
.instance ⇒ Object
24 25 26 |
# File 'lib/gitlab/daemon.rb', line 24 def self.instance(...) @instance ||= initialize_instance(...) end |
Instance Method Details
#enabled? ⇒ Boolean
41 42 43 |
# File 'lib/gitlab/daemon.rb', line 41 def enabled? true end |
#start ⇒ Object
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 |
#stop ⇒ Object
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
30 31 32 |
# File 'lib/gitlab/daemon.rb', line 30 def thread? !thread.nil? end |
#thread_name ⇒ Object
45 46 47 |
# File 'lib/gitlab/daemon.rb', line 45 def thread_name self.class.name.demodulize.underscore end |