Class: Tetrahedron::Service

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

Direct Known Subclasses

Database

Defined Under Namespace

Classes: Configuration, Provider

Constant Summary collapse

Unconfigured =
Class.new(Tetrahedron::Error)
Misconfigured =
Class.new(Tetrahedron::Error)
AlreadyConfigured =
Class.new(Tetrahedron::Error)
Unreachable =
Class.new(Tetrahedron::Error)

Class Method Summary collapse

Class Method Details

.configure(&configurator) ⇒ Object

Raises:



24
25
26
27
28
29
# File 'lib/tetrahedron/service.rb', line 24

def self.configure(&configurator)
  raise AlreadyConfigured if self.configured?
  provider = configurator.call()
  raise Misconfigured unless provider.is_a? Provider
  self.class_variable_set(:@@provider, provider)
end

.configured!Object

Raises:



20
21
22
# File 'lib/tetrahedron/service.rb', line 20

def self.configured!
  raise Unconfigured unless self.configured?
end

.configured?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/tetrahedron/service.rb', line 16

def self.configured?
  self.class_variable_defined?(:@@provider)
end

.install(application) ⇒ Object



3
4
5
6
7
# File 'lib/tetrahedron/service.rb', line 3

def self.install(application)
  service = Class.new(self)
  service.class_variable_set(:@@application, application)
  application.const_set(self.to_s.split('::').last, service)
end

.wait_until_reachable(opts = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tetrahedron/service.rb', line 36

def self.wait_until_reachable(opts={})
  Timeout::timeout(opts.fetch(:timeout, 15)) do
    while true
      begin
        case opts[:protocol]
        when :tcp
          TCPSocket.new(opts[:host], opts[:port]).close
          return true
        when :udp
          raise "This makes no sense!"
        end
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError
        # We're all good citizens, right?
        Kernel.sleep(1)
      end
    end
  end
rescue Timeout::Error
  false
end

.wait_until_reachable!(opts = {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/tetrahedron/service.rb', line 57

def self.wait_until_reachable!(opts={})
  unless self.wait_until_reachable(opts)
    $stderr.puts "Unable to reach #{opts[:host]}:#{opts[:port]}!"
    raise Unreachable
  end
end