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.
- #show_tail(options = {}) ⇒ Object
-
#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.
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_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
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
122 123 124 |
# File 'lib/desi/process_manager.rb', line 122 def has_pid? pid && !pid.empty? end |
#pid ⇒ String
PID as retrieved from the pidfile
129 130 131 |
# File 'lib/desi/process_manager.rb', line 129 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.
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_version ⇒ String?
Release number of the currently running cluster
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( = {}) puts "Will tail ES logs…" if @verbose executable = "tail" lines = Integer(.fetch(:lines, 10)) system "#{executable} -n #{lines} -f #{logfile}" 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.
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 |
#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"
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 |
#stop ⇒ self
This method will also output its result on STDOUT if @verbose is true
Stop the cluster
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 |