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.



25
26
27
28
29
30
# File 'lib/desi/process_manager.rb', line 25

def initialize(opts = {})
  @host = opts.fetch(:host, 'http://127.0.0.1:9200')
  @verbose = opts[:verbose]
  @local_install = LocalInstall.new
  @client = opts.fetch(:http_client_factory, Desi::HttpClient).new(@host)
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)


148
149
150
# File 'lib/desi/process_manager.rb', line 148

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)


113
114
115
# File 'lib/desi/process_manager.rb', line 113

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

#pidString

PID as retrieved from the pidfile

Returns:

  • (String)


120
121
122
# File 'lib/desi/process_manager.rb', line 120

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)


65
66
67
68
69
70
# File 'lib/desi/process_manager.rb', line 65

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)


131
132
133
134
135
136
137
# File 'lib/desi/process_manager.rb', line 131

def running_version
  begin
    JSON.parse(@client.get('/').body)["version"]["number"]
  rescue
    nil
  end
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)


42
43
44
45
46
47
48
49
# File 'lib/desi/process_manager.rb', line 42

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)


100
101
102
103
104
105
106
107
108
# File 'lib/desi/process_manager.rb', line 100

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)


80
81
82
83
84
85
86
87
88
# File 'lib/desi/process_manager.rb', line 80

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