Class: Gretl::Client

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

Instance Method Summary collapse

Instance Method Details

#activeObject



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

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

#inactiveObject



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

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

#port(query) ⇒ Object



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

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

#portsObject



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

def ports = Gretl._fetch_ports

#remove(query) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
99
# File 'lib/gretl.rb', line 94

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)


56
57
58
59
60
61
62
63
# File 'lib/gretl.rb', line 56

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)


74
75
76
77
78
79
# File 'lib/gretl.rb', line 74

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)


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

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)


65
66
67
68
69
70
71
72
# File 'lib/gretl.rb', line 65

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)


81
82
83
84
85
86
# File 'lib/gretl.rb', line 81

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



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

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



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

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