Class: Utils::ProbeServer

Inherits:
Object show all
Defined in:
lib/utils/probe_server.rb

Defined Under Namespace

Classes: Job

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ ProbeServer

Returns a new instance of ProbeServer.



57
58
59
60
61
62
63
# File 'lib/utils/probe_server.rb', line 57

def initialize(uri)
  @uri        = uri
  @history    = [].freeze
  @jobs_queue = Queue.new
  @current_job_id = 0
  Thread.new { work_loop }
end

Instance Method Details

#commandsObject



91
92
93
94
95
# File 'lib/utils/probe_server.rb', line 91

def commands
  annotations = self.class.doc_annotations.sort_by(&:first)
  max_size = annotations.map { |a| a.first.size }.max
  output_message annotations.map { |n, v| "#{n.to_s.ljust(max_size + 1)}#{v}" }
end

#continueObject



106
107
108
109
110
111
# File 'lib/utils/probe_server.rb', line 106

def continue
  mutex.unlock
  true
rescue ThreadError
  false
end

#envObject



184
185
186
# File 'lib/utils/probe_server.rb', line 184

def env
  ENV
end

#history_clearObject



178
179
180
181
# File 'lib/utils/probe_server.rb', line 178

def history_clear
  @history = []
  true
end

#history_listObject



173
174
175
# File 'lib/utils/probe_server.rb', line 173

def history_list
  output_message @history
end

#inspectObject Also known as: to_s



84
85
86
# File 'lib/utils/probe_server.rb', line 84

def inspect
  "#<Probe #queue=#{@jobs_queue.size}>"
end

#jobObject



114
115
116
117
118
# File 'lib/utils/probe_server.rb', line 114

def job
  queue_synchronize do
    @job
  end
end

#job_enqueue(job_args) ⇒ Object Also known as: enqueue



125
126
127
128
129
# File 'lib/utils/probe_server.rb', line 125

def job_enqueue(job_args)
  job = Job.new(self, job_args)
  output_message "#{job.inspect} enqueued.", type: :info
  @jobs_queue.push job
end

#job_kill(signal = :TERM) ⇒ Object



133
134
135
# File 'lib/utils/probe_server.rb', line 133

def job_kill(signal = :TERM)
  @pid and Process.kill signal, @pid
end

#job_repeat(job_id = @history.last) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/utils/probe_server.rb', line 162

def job_repeat(job_id = @history.last)
  Job === job_id and job_id = job_id.id
  if old_job = @history.find { |job| job.id == job_id }
    job_enqueue old_job.args
    true
  else
    false
  end
end

#jobs_clearObject



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/utils/probe_server.rb', line 149

def jobs_clear
  queue_synchronize do
    unless @jobs_queue.empty?
      @jobs_queue.clear
      output_message "Cleared all queued jobs.", type: :warn
      true
    else
      false
    end
  end
end

#jobs_listObject



144
145
146
# File 'lib/utils/probe_server.rb', line 144

def jobs_list
  output_message @jobs_queue.instance_variable_get(:@que)
end

#next_job_idObject



120
121
122
# File 'lib/utils/probe_server.rb', line 120

def next_job_id
  @current_job_id += 1
end

#pauseObject



98
99
100
101
102
103
# File 'lib/utils/probe_server.rb', line 98

def pause
  mutex.lock
  true
rescue ThreadError
  false
end

#shutdownObject



138
139
140
141
# File 'lib/utils/probe_server.rb', line 138

def shutdown
  output_message "Server was shutdown down – HARD!", type: :warn
  exit! 23
end

#startObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/utils/probe_server.rb', line 65

def start
  output_message "Starting probe server listening to #{@uri.inspect}.", type: :info
  DRb.start_service(@uri, self)
  begin
    DRb.thread.join
  rescue Interrupt
    ARGV.clear << '-f'
    output_message %{\nEntering interactive mode: Type "commands" to get help for the commands.}, type: :info
    begin
      old, $VERBOSE = $VERBOSE, nil
      examine(self)
    ensure
      $VERBOSE = old
    end
    output_message "Quitting interactive mode, but still listening to #{@uri.inspect}.", type: :info
    retry
  end
end