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.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/desi/process_manager.rb', line 25 def initialize(opts = {}) @host = opts.fetch(:host, 'http://127.0.0.1:9200') @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
164 165 166 |
# File 'lib/desi/process_manager.rb', line 164 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
121 122 123 |
# File 'lib/desi/process_manager.rb', line 121 def has_pid? pid && !pid.empty? end |
#pid ⇒ String
PID as retrieved from the pidfile
128 129 130 |
# File 'lib/desi/process_manager.rb', line 128 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.
73 74 75 76 77 78 |
# File 'lib/desi/process_manager.rb', line 73 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
139 140 141 142 143 144 145 |
# File 'lib/desi/process_manager.rb', line 139 def running_version begin JSON.parse(@client.get('/').body)["version"]["number"] rescue nil end end |
#show_tail(options = {}) ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/desi/process_manager.rb', line 147 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.
50 51 52 53 54 55 56 57 |
# File 'lib/desi/process_manager.rb', line 50 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"
108 109 110 111 112 113 114 115 116 |
# File 'lib/desi/process_manager.rb', line 108 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
88 89 90 91 92 93 94 95 96 |
# File 'lib/desi/process_manager.rb', line 88 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 |