Class: Desi::ProcessManager
- Inherits:
-
Object
- Object
- Desi::ProcessManager
- 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
Instance Method Summary collapse
-
#cluster_health ⇒ Hash
protected
Return cluster health data straight from the cluster.
-
#has_pid? ⇒ Boolean
Whether the pidfile actually holds a PID.
-
#initialize(opts = {}) ⇒ ProcessManager
constructor
A new instance of ProcessManager.
-
#pid ⇒ String
PID as retrieved from the pidfile.
-
#restart ⇒ self
Restart the cluster.
-
#running_version ⇒ String?
Release number of the currently running cluster.
-
#start ⇒ self
Start the cluster.
-
#status ⇒ String
Get information about the cluster’s status.
-
#stop ⇒ self
Stop the cluster.
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_health ⇒ Hash (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
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
113 114 115 |
# File 'lib/desi/process_manager.rb', line 113 def has_pid? pid && !pid.empty? end |
#pid ⇒ String
PID as retrieved from the pidfile
120 121 122 |
# File 'lib/desi/process_manager.rb', line 120 def pid @pid ||= File.read(pidfile) if pidfile.exist? end |
#restart ⇒ self
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.
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.
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_version ⇒ String?
Release number of the currently running cluster
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 |
#start ⇒ self
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.
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 |
#status ⇒ String
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"
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 |
#stop ⇒ self
This method will also output its result on STDOUT if @verbose is true
Stop the cluster
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 |