Class: Herb::Dev::ServerEntry
- Inherits:
-
Object
- Object
- Herb::Dev::ServerEntry
- Defined in:
- lib/herb/dev/server_entry.rb
Constant Summary collapse
- SERVERS_DIR =
File.("~/.herb/dev-servers").freeze
- REQUIRED_KEYS =
["pid", "port", "project", "started_at"].freeze
Instance Attribute Summary collapse
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
Class Method Summary collapse
- .all ⇒ Object
- .find_by_port(port) ⇒ Object
- .find_by_project(project_path) ⇒ Object
- .process_alive?(pid) ⇒ Boolean
- .stop_all ⇒ Object
Instance Method Summary collapse
- #alive? ⇒ Boolean
-
#initialize(pid:, port:, project:, started_at: Time.now.utc.iso8601) ⇒ ServerEntry
constructor
A new instance of ServerEntry.
- #project_name ⇒ Object
- #remove ⇒ Object
- #save ⇒ Object
- #stop! ⇒ Object
- #to_hash ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(pid:, port:, project:, started_at: Time.now.utc.iso8601) ⇒ ServerEntry
Returns a new instance of ServerEntry.
14 15 16 17 18 19 |
# File 'lib/herb/dev/server_entry.rb', line 14 def initialize(pid:, port:, project:, started_at: Time.now.utc.iso8601) @pid = pid @port = port @project = project @started_at = started_at end |
Instance Attribute Details
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
12 13 14 |
# File 'lib/herb/dev/server_entry.rb', line 12 def pid @pid end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
12 13 14 |
# File 'lib/herb/dev/server_entry.rb', line 12 def port @port end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
12 13 14 |
# File 'lib/herb/dev/server_entry.rb', line 12 def project @project end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
12 13 14 |
# File 'lib/herb/dev/server_entry.rb', line 12 def started_at @started_at end |
Class Method Details
.all ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/herb/dev/server_entry.rb', line 58 def all FileUtils.mkdir_p(SERVERS_DIR) Dir.glob(File.join(SERVERS_DIR, "*.json")).filter_map do |path| entry = load_file(path) if entry&.alive? entry else begin File.delete(path) rescue StandardError nil end nil end end end |
.find_by_port(port) ⇒ Object
77 78 79 |
# File 'lib/herb/dev/server_entry.rb', line 77 def find_by_port(port) all.find { |entry| entry.port == port } end |
.find_by_project(project_path) ⇒ Object
81 82 83 |
# File 'lib/herb/dev/server_entry.rb', line 81 def find_by_project(project_path) all.find { |entry| entry.project == project_path } end |
.process_alive?(pid) ⇒ Boolean
89 90 91 92 93 94 95 96 |
# File 'lib/herb/dev/server_entry.rb', line 89 def process_alive?(pid) return false unless pid Process.kill(0, pid) true rescue Errno::ESRCH, Errno::EPERM false end |
.stop_all ⇒ Object
85 86 87 |
# File 'lib/herb/dev/server_entry.rb', line 85 def stop_all all.each(&:stop!) end |
Instance Method Details
#alive? ⇒ Boolean
32 33 34 |
# File 'lib/herb/dev/server_entry.rb', line 32 def alive? self.class.process_alive?(pid) end |
#project_name ⇒ Object
44 45 46 |
# File 'lib/herb/dev/server_entry.rb', line 44 def project_name project&.split("/")&.last || "unknown" end |
#remove ⇒ Object
26 27 28 29 30 |
# File 'lib/herb/dev/server_entry.rb', line 26 def remove File.delete(file_path) rescue StandardError nil end |
#save ⇒ Object
21 22 23 24 |
# File 'lib/herb/dev/server_entry.rb', line 21 def save FileUtils.mkdir_p(SERVERS_DIR) File.write(file_path, to_json) end |
#stop! ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/herb/dev/server_entry.rb', line 48 def stop! Process.kill("INT", pid) remove true rescue Errno::ESRCH remove false end |
#to_hash ⇒ Object
36 37 38 |
# File 'lib/herb/dev/server_entry.rb', line 36 def to_hash { pid: pid, port: port, project: project, started_at: started_at } end |
#to_json ⇒ Object
40 41 42 |
# File 'lib/herb/dev/server_entry.rb', line 40 def to_json(*) JSON.generate(to_hash) end |