Class: Diametric::RestService

Inherits:
Object
  • Object
show all
Includes:
ServiceBase
Defined in:
lib/diametric/rest_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ServiceBase

included

Constructor Details

#initialize(conf = "datomic_version.yml", dest = "vendor/datomic") ⇒ RestService


16
17
18
19
20
21
22
# File 'lib/diametric/rest_service.rb', line 16

def initialize(conf="datomic_version.yml", dest="vendor/datomic")
 @conf = conf
 @dest = dest
 datomic_type, datomic_version = RestService.datomic_version(conf)
 @datomic_home = File.join(File.dirname(__FILE__), "../..", dest, "datomic-#{datomic_type}-#{datomic_version}")
 @pid = nil
end

Instance Attribute Details

#datomic_homeObject

Returns the value of attribute datomic_home


13
14
15
# File 'lib/diametric/rest_service.rb', line 13

def datomic_home
  @datomic_home
end

#datomic_versionObject

Returns the value of attribute datomic_version


13
14
15
# File 'lib/diametric/rest_service.rb', line 13

def datomic_version
  @datomic_version
end

#datomic_version_noObject

Returns the value of attribute datomic_version_no


13
14
15
# File 'lib/diametric/rest_service.rb', line 13

def datomic_version_no
  @datomic_version_no
end

#db_aliasObject

Returns the value of attribute db_alias


14
15
16
# File 'lib/diametric/rest_service.rb', line 14

def db_alias
  @db_alias
end

#hostObject

Returns the value of attribute host


14
15
16
# File 'lib/diametric/rest_service.rb', line 14

def host
  @host
end

#pidObject

Returns the value of attribute pid


13
14
15
# File 'lib/diametric/rest_service.rb', line 13

def pid
  @pid
end

#portObject

Returns the value of attribute port


14
15
16
# File 'lib/diametric/rest_service.rb', line 14

def port
  @port
end

#uriObject

Returns the value of attribute uri


14
15
16
# File 'lib/diametric/rest_service.rb', line 14

def uri
  @uri
end

Class Method Details

.datomic_command(datomic_home) ⇒ Object


7
8
9
10
# File 'lib/diametric/rest_service.rb', line 7

def datomic_command(datomic_home)
  classpath = datomic_classpath(datomic_home)
  ["java -server -Xmx1g", "-cp", classpath, "clojure.main", "-i", "#{datomic_home}/bin/bridge.clj", "--main datomic.rest"].flatten.join(" ")
end

Instance Method Details

#port_available?(uri) ⇒ Boolean


52
53
54
55
56
57
# File 'lib/diametric/rest_service.rb', line 52

def port_available?(uri)
  Net::HTTP.get_response(uri)
  false
rescue Errno::ECONNREFUSED
  true
end

#ready?(uri) ⇒ Boolean


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/diametric/rest_service.rb', line 59

def ready?(uri)
  while true
    begin
      Net::HTTP.get_response(uri)
      return true
    rescue
      sleep 1
      redo
    end
  end
  true
end

#start(opts = {}) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/diametric/rest_service.rb', line 24

def start(opts={})
  return if @pid
  RestService.download(@conf, @dest)
  command = RestService.datomic_command(@datomic_home)

  require 'socket'
  @host = opts[:host] ? opts[:host] : Socket.gethostname
  @port = opts[:port] ? opts[:port] : 9000
  @db_alias = opts[:db_alias] ? opts[:db_alias] : "free"
  @uri = opts[:uri] ? opts[:uri] : "datomic:mem://"

  uri = URI("http://#{@host}:#{@port}/")

  unless port_available?(uri)
    puts "Somebody is using #{@port}. Choose other."
    return
  end

  temp_pid = spawn("#{command} -p #{@port} #{@db_alias} #{@uri}")

  @pid = temp_pid if ready?(uri)
end

#stopObject


47
48
49
50
# File 'lib/diametric/rest_service.rb', line 47

def stop
  Process.kill("HUP", @pid) if @pid
  @pid = nil
end