Class: RedisQueuedLocks::Swarm::SwarmElement::Threaded Private

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/redis_queued_locks/swarm/swarm_element/threaded.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/ClassLength

Since:

  • 1.9.0

Direct Known Subclasses

ProbeHosts

Constant Summary

Constants included from Utilities

Utilities::RACTOR_LIVENESS_PATTERN, Utilities::RACTOR_STATUS_PATTERN, Utilities::RUBY_OVER_4

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities

clock_gettime, ractor_alive?, ractor_status, ruby_over_4?, run_non_critical, thread_state

Constructor Details

#initialize(rql_client) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Since:

  • 1.9.0



55
56
57
58
59
60
61
62
# File 'lib/redis_queued_locks/swarm/swarm_element/threaded.rb', line 55

def initialize(rql_client)
  @rql_client = rql_client
  @swarm_element = nil
  @main_loop = nil # NOTE: strongly technical variable refenrece (see attr_reader docs)
  @swarm_element_commands = nil
  @swarm_element_results = nil
  @sync = RedisQueuedLocks::Utilities::Lock.new
end

Instance Attribute Details

#main_loop ⇒ Thread, NilClass (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The main loop reference is only used inside the swarm element thread and swarm element termination. It should not be used everywhere else! This is strongly technical variable refenrece.

Returns:

  • (Thread, NilClass)

Since:

  • 1.9.0



30
31
32
# File 'lib/redis_queued_locks/swarm/swarm_element/threaded.rb', line 30

def main_loop
  @main_loop
end

#rql_client ⇒ RedisQueuedLocks::Client (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 1.9.0



14
15
16
# File 'lib/redis_queued_locks/swarm/swarm_element/threaded.rb', line 14

def rql_client
  @rql_client
end

#swarm_element ⇒ Thread, NilClass (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Thread, NilClass)

Since:

  • 1.9.0



20
21
22
# File 'lib/redis_queued_locks/swarm/swarm_element/threaded.rb', line 20

def swarm_element
  @swarm_element
end

#swarm_element_commands ⇒ Thread::SizedQueue, NilClass (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Thread::SizedQueue, NilClass)

Since:

  • 1.9.0



36
37
38
# File 'lib/redis_queued_locks/swarm/swarm_element/threaded.rb', line 36

def swarm_element_commands
  @swarm_element_commands
end

#swarm_element_results ⇒ Thread::SizedQueue, NilClass (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Thread::SizedQueue, NilClass)

Since:

  • 1.9.0



42
43
44
# File 'lib/redis_queued_locks/swarm/swarm_element/threaded.rb', line 42

def swarm_element_results
  @swarm_element_results
end

#sync ⇒ RedisQueuedLocks::Utilities::Lock (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 1.9.0



48
49
50
# File 'lib/redis_queued_locks/swarm/swarm_element/threaded.rb', line 48

def sync
  @sync
end

Instance Method Details

#enabled? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

Since:

  • 1.9.0



109
110
111
# File 'lib/redis_queued_locks/swarm/swarm_element/threaded.rb', line 109

def enabled? # steep:ignore
  # NOTE: provide an <is enabled> logic here by analyzing the redis queued locks config.
end

#reswarm_if_dead! ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Since:

  • 19.0.0



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/redis_queued_locks/swarm/swarm_element/threaded.rb', line 82

def reswarm_if_dead!
  return unless enabled?

  sync.synchronize do
    if swarmed__stopped?
      swarm_loop__start
    elsif swarmed__dead? || idle?
      swarm!
      swarm_loop__start
    end
  end
end

#status ⇒ Hash<Symbol,Boolean|Hash<Symbol,String|Boolean>>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Style/RedundantBegin

Returns:

  • (Hash<Symbol,Boolean|Hash<Symbol,String|Boolean>>) —

    Format: { enabled: , thread: { running: , state: , }, main_loop: { running: , state: } }

Since:

  • 1.9.0



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/redis_queued_locks/swarm/swarm_element/threaded.rb', line 129

def status
  sync.synchronize do
    thread_running = swarmed__alive?
    thread_state = swarmed? ? thread_state(swarm_element) : 'non_initialized' # steep:ignore

    main_loop_running = swarmed__running?
    main_loop_state = begin
      if main_loop_running
        swarm_loop__status[:result][:main_loop][:state] # steep:ignore
      else
        'non_initialized'
      end
    end

    {
      enabled: enabled?,
      thread: {
        running: thread_running,
        state: thread_state
      },
      main_loop: {
        running: main_loop_running,
        state: main_loop_state
      }
    }
  end
end

#try_kill! ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Since:

  • 1.9.0



99
100
101
102
103
# File 'lib/redis_queued_locks/swarm/swarm_element/threaded.rb', line 99

def try_kill!
  sync.synchronize do
    swarm_element__termiante
  end
end

#try_swarm! ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Since:

  • 1.9.0



68
69
70
71
72
73
74
75
76
# File 'lib/redis_queued_locks/swarm/swarm_element/threaded.rb', line 68

def try_swarm!
  return unless enabled?

  sync.synchronize do
    swarm_element__termiante
    swarm!
    swarm_loop__start
  end
end