Class: Utils::ProbeServer

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

Defined Under Namespace

Classes: Job, LogWrapper

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ ProbeServer

Returns a new instance of ProbeServer.



44
45
46
47
48
49
50
# File 'lib/utils/probe_server.rb', line 44

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

Instance Method Details

#clearObject



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

def clear
  system "clear"
end

#helpObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/utils/probe_server.rb', line 89

def help
  docs      = doc_annotations.sort_by(&:first)
  docs_size = docs.map { |a| a.first.size }.max
  format = '%-20s %-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



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

def history_clear
  @history = []
  true
end

#history_listObject



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

def history_list
  output_message @history
end

#inspectObject Also known as: to_s



78
79
80
# File 'lib/utils/probe_server.rb', line 78

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

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



103
104
105
106
107
# File 'lib/utils/probe_server.rb', line 103

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

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



120
121
122
123
124
125
126
127
128
# File 'lib/utils/probe_server.rb', line 120

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

#next_job_idObject



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

def next_job_id
  @current_job_id += 1
end

#output_message(msg, type: nil) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/utils/probe_server.rb', line 177

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


52
53
54
55
56
# File 'lib/utils/probe_server.rb', line 52

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

#shutdownObject



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

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

#startObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/utils/probe_server.rb', line 58

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: :info
    help
    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