Class: Gretl::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gretl.rb

Instance Method Summary collapse

Instance Method Details

#activeObject



75
# File 'lib/gretl.rb', line 75

def active = Gretl._fetch_ports.select(&:active)

#inactiveObject



76
# File 'lib/gretl.rb', line 76

def inactive = Gretl._fetch_ports.reject(&:active)

#port(query) ⇒ Object



74
# File 'lib/gretl.rb', line 74

def port(query) = Gretl._resolve(query)

#portsObject



73
# File 'lib/gretl.rb', line 73

def ports = Gretl._fetch_ports

#remove(query) ⇒ Object

Raises:

  • (ArgumentError)


116
117
118
119
120
121
# File 'lib/gretl.rb', line 116

def remove(query)
  matches = Gretl._resolve(query)
  raise ArgumentError, "No port found matching #{query.inspect}" if matches.empty?
  matches.each { |p| Gretl._request(:delete, "/ports/#{p.port}") }
  matches
end

#start(query) ⇒ Object

Raises:

  • (ArgumentError)


78
79
80
81
82
83
84
85
# File 'lib/gretl.rb', line 78

def start(query)
  matches = Gretl._resolve(query)
  raise ArgumentError, "No port found matching #{query.inspect}" if matches.empty?
  matches.each do |p|
    Gretl._request(:post, "/ports/#{p.port}/start") if !p.active && p.has_command
  end
  matches
end

#start_group(group) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
100
101
# File 'lib/gretl.rb', line 96

def start_group(group)
  matches = Gretl._fetch_ports.select { |p| p.group&.downcase == group.downcase }
  raise ArgumentError, "No ports found in group #{group.inspect}" if matches.empty?
  matches.each { |p| Gretl._request(:post, "/ports/#{p.port}/start") if !p.active && p.has_command }
  matches
end

#status_group(group) ⇒ Object

Raises:

  • (ArgumentError)


110
111
112
113
114
# File 'lib/gretl.rb', line 110

def status_group(group)
  matches = Gretl._fetch_ports.select { |p| p.group&.downcase == group.downcase }
  raise ArgumentError, "No ports found in group #{group.inspect}" if matches.empty?
  matches
end

#stop(query) ⇒ Object

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
# File 'lib/gretl.rb', line 87

def stop(query)
  matches = Gretl._resolve(query)
  raise ArgumentError, "No port found matching #{query.inspect}" if matches.empty?
  matches.each do |p|
    Gretl._request(:post, "/ports/#{p.port}/stop") if p.active
  end
  matches
end

#stop_group(group) ⇒ Object

Raises:

  • (ArgumentError)


103
104
105
106
107
108
# File 'lib/gretl.rb', line 103

def stop_group(group)
  matches = Gretl._fetch_ports.select { |p| p.group&.downcase == group.downcase }
  raise ArgumentError, "No ports found in group #{group.inspect}" if matches.empty?
  matches.each { |p| Gretl._request(:post, "/ports/#{p.port}/stop") if p.active }
  matches
end

#wait_for_active(query, timeout: 30, interval: 0.5) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/gretl.rb', line 123

def wait_for_active(query, timeout: 30, interval: 0.5)
  deadline = Time.now + timeout
  while Time.now < deadline
    Gretl._resolve(query).each { |p| return p if p.active }
    sleep interval
  end
  raise "Port matching #{query.inspect} did not become active within #{timeout}s"
end

#wait_for_inactive(query, timeout: 30, interval: 0.5) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/gretl.rb', line 132

def wait_for_inactive(query, timeout: 30, interval: 0.5)
  deadline = Time.now + timeout
  while Time.now < deadline
    Gretl._resolve(query).each { |p| return p if p.stopped? }
    sleep interval
  end
  raise "Port matching #{query.inspect} did not become inactive within #{timeout}s"
end