Class: Desi::ProcessManager

Inherits:
Object
  • Object
show all
Defined in:
lib/desi/process_manager.rb

Overview

The ProcessManager will start, stop and restart a local Elastic Search node instance, in addition to reporting its status

Examples:

Start up the instance and check its status

Desi::ProcessManager.new.start.status #=> "OK. Elastic Search cluster 'elasticsearch' (v0.19.9) is running on 1 node(s) with status green"

Retrieve the currently running cluster’s version

Desi::ProcessManager.new.running_version #=> "0.19.9"

Retrieve a distant cluster’s version

Desi::ProcessManager.new(host: "http://somewhere.com:9200").running_version #=> "0.18.5"

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ProcessManager

Returns a new instance of ProcessManager.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/desi/process_manager.rb', line 26

def initialize(opts = {})
  @host = opts.fetch(:host) { Desi.configuration.server }
  @verbose = opts[:verbose]
  @foreground = opts[:foreground]
  @local_install = LocalInstall.new
  @client = opts.fetch(:http_client_factory, Desi::HttpClient).new(@host)

  @tail_after_start = opts[:tail]

  if @tail_after_start && @foreground
    $stderr.puts "Cannot tail logs when starting ES in foreground mode"
    exit 1
  end
end

Instance Method Details

#cluster_healthHash (protected)

Return cluster health data straight from the cluster

see www.elasticsearch.org/guide/reference/api/admin-cluster-health.html for further information on the response’s structure

Returns:

  • (Hash)


165
166
167
# File 'lib/desi/process_manager.rb', line 165

def cluster_health
  @cluster_health ||= OpenStruct.new(JSON.parse(@client.get('/_cluster/health').body))
end

#has_pid?Boolean

Whether the pidfile actually holds a PID

Returns:

  • (Boolean)


122
123
124
# File 'lib/desi/process_manager.rb', line 122

def has_pid?
  pid && !pid.empty?
end

#pidString

PID as retrieved from the pidfile

Returns:

  • (String)


129
130
131
# File 'lib/desi/process_manager.rb', line 129

def pid
  @pid ||= File.read(pidfile) if pidfile.exist?
end

#restartself

Note:

We use the pidfile to determine if the cluster is up. If a node was started with another tool, you may end up with more than 1 node running.

Note:

This method will also output its result on STDOUT if @verbose is true

Restart the cluster

Stop the cluster (if its up) and start it again.

Returns:

  • (self)


74
75
76
77
78
79
# File 'lib/desi/process_manager.rb', line 74

def restart
  puts " * (Re)starting cluster" if @verbose
  stop if has_pid?
  puts " * Elastic Search #{running_version} started" if start_cluster && @verbose
  self
end

#running_versionString?

Release number of the currently running cluster

Examples:

Desi::ProcessManager.new.running_version #=> "0.19.9"

Returns:

  • (String, nil)


140
141
142
143
144
145
146
# File 'lib/desi/process_manager.rb', line 140

def running_version
  begin
    JSON.parse(@client.get('/').body)["version"]["number"]
  rescue
    nil
  end
end

#show_tail(options = {}) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/desi/process_manager.rb', line 148

def show_tail(options = {})
  puts "Will tail ES logs…" if @verbose
  executable = "tail"
  lines = Integer(options.fetch(:lines, 10))

  system "#{executable} -n #{lines} -f #{logfile}"
end

#startself

Note:

This method will also output its result on STDOUT if @verbose is true

Start the cluster

This will be a no-op if the cluster is already started.

Returns:

  • (self)


51
52
53
54
55
56
57
58
# File 'lib/desi/process_manager.rb', line 51

def start
  if cluster_ready?
    puts "ES cluster is already running" if @verbose
  else
    puts " * Elastic Search #{running_version} started" if start_cluster && @verbose
  end
  self
end

#statusString

Note:

This method will also output its result on STDOUT if @verbose is true

Get information about the cluster’s status

Desi::ProcessManager.new.status #=> "OK. Elastic Search cluster 'elasticsearch' (v0.19.9) is running on 1 node(s) with status green"

Returns:

  • (String)


109
110
111
112
113
114
115
116
117
# File 'lib/desi/process_manager.rb', line 109

def status
  if version = running_version
    msg = "OK. Elastic Search cluster '#{cluster_health.cluster_name}' (v#{version}) is running on #{cluster_health.number_of_nodes} node(s) with status #{cluster_health.status}"
  else
    msg = "KO. No Elastic Search instance was found running on #{@host}"
  end
  puts msg if @verbose
  msg
end

#stopself

Note:

This method will also output its result on STDOUT if @verbose is true

Stop the cluster

Returns:

  • (self)


89
90
91
92
93
94
95
96
97
# File 'lib/desi/process_manager.rb', line 89

def stop
  if pid
    puts " * Will stop instance with pid #{pid}" if @verbose
    stop_cluster
  else
    puts " * No pidfile detected!. Won't stop" if @verbose
  end
  self
end