Class: Utils::ProbeServer

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

Defined Under Namespace

Classes: LogWrapper

Instance Method Summary collapse

Constructor Details

#initializeProbeServer

Returns a new instance of ProbeServer.



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

def initialize
  @server         = UnixSocks::Server.new(socket_name: 'probe.sock', runtime_dir: Dir.pwd)
  @history        = [].freeze
  @jobs_queue     = Queue.new
  @current_job_id = 0
end

Instance Method Details

#clearObject



215
216
217
# File 'lib/utils/probe_server.rb', line 215

def clear
  system "clear"
end

#helpObject



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/utils/probe_server.rb', line 139

def help
  docs      = doc_annotations.sort_by(&:first)
  docs_size = docs.map { |a| a.first.size }.max
  format = "%-#{docs_size}s %-3s %s"
  output_message [
    (format % %w[ command sho description ]).on_color(20).white
  ] << docs.map { |cmd, doc|
    shortcut = shortcut_of(cmd) and shortcut = "(#{shortcut})"
    format % [ cmd, shortcut, doc ]
  }
end

#history_clearObject



187
188
189
190
# File 'lib/utils/probe_server.rb', line 187

def history_clear
  @history = []
  true
end

#history_listObject



182
183
184
# File 'lib/utils/probe_server.rb', line 182

def history_list
  output_message @history
end

#inspectObject Also known as: to_s



128
129
130
# File 'lib/utils/probe_server.rb', line 128

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

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



153
154
155
156
157
# File 'lib/utils/probe_server.rb', line 153

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

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



170
171
172
173
174
175
176
177
178
# File 'lib/utils/probe_server.rb', line 170

def job_repeat(job_id = @history.last)
  ProcessJob === 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

#next_job_idObject



223
224
225
# File 'lib/utils/probe_server.rb', line 223

def next_job_id
  @current_job_id += 1
end

#output_message(msg, type: nil) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/utils/probe_server.rb', line 227

def output_message(msg, type: nil)
  msg.respond_to?(:to_a) and msg = msg.to_a * "\n"
  msg =
    case type
    when :success
      msg.on_color(22).white
    when :info
      msg.on_color(20).white
    when :warn
      msg.on_color(94).white
    when :failure
      msg.on_color(124).blink.white
    else
      msg
    end
  STDOUT.puts msg
  STDOUT.flush
  self
end


96
97
98
99
100
# File 'lib/utils/probe_server.rb', line 96

def print(*msg)
  if msg.first !~ /^irb: warn: can't alias / # shut your god d*mn wh*re mouth
    super
  end
end

#shutdownObject



163
164
165
166
# File 'lib/utils/probe_server.rb', line 163

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

#startObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/utils/probe_server.rb', line 102

def start
  output_message "Starting probe server listening to #{@server.server_socket_path}.", type: :info
  work_loop = Thread.new do
    loop do
      job = @jobs_queue.pop
      run_job job
    end
  end
  begin
    receive_loop.join
  rescue Interrupt
    ARGV.clear << '-f'
    output_message %{\nEntering interactive mode.}, type: :info
    help
    begin
      old, $VERBOSE = $VERBOSE, nil
      examine(self)
    ensure
      $VERBOSE = old
    end
    @server.remove_socket_path
    output_message "Quitting interactive mode, but still listening to #{@server.server_socket_path}.", type: :info
    retry
  end
end