Module: Coin

Defined in:
lib/coin.rb,
lib/coin/vault.rb,
lib/coin/version.rb

Defined Under Namespace

Classes: Vault

Constant Summary collapse

VERSION =
"0.1.9"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.portObject


40
41
42
# File 'lib/coin.rb', line 40

def port
  @port ||= 8955
end

.remote_uriObject

Returns the value of attribute remote_uri


49
50
51
# File 'lib/coin.rb', line 49

def remote_uri
  @remote_uri
end

.uriObject


45
46
47
# File 'lib/coin.rb', line 45

def uri
  @uri ||= "druby://localhost:#{port}"
end

Class Method Details

.clearObject


35
36
37
# File 'lib/coin.rb', line 35

def clear
  server.clear
end

.delete(key) ⇒ Object


27
28
29
# File 'lib/coin.rb', line 27

def delete(key)
  server.delete key
end

.lengthObject


31
32
33
# File 'lib/coin.rb', line 31

def length
  server.length
end

.pidObject


92
93
94
# File 'lib/coin.rb', line 92

def pid
  File.read(Coin.pid_file) if File.exist?(Coin.pid_file)
end

.pid_fileObject


88
89
90
# File 'lib/coin.rb', line 88

def pid_file
  "/tmp/coin-pid-63f95cb5-0bae-4f66-88ec-596dfbac9244"
end

.read(key, lifetime = 300) ⇒ Object


10
11
12
13
14
15
16
17
# File 'lib/coin.rb', line 10

def read(key, lifetime=300)
  value = server.read(key)
  if value.nil? && block_given?
    value = yield
    write(key, value, lifetime)
  end
  value
end

.read_and_delete(key) ⇒ Object


19
20
21
# File 'lib/coin.rb', line 19

def read_and_delete(key)
  server.read_and_delete key
end

.remote_serverObject


83
84
85
86
# File 'lib/coin.rb', line 83

def remote_server
  DRb.start_service
  @server = DRbObject.new_with_uri(remote_uri)
end

.serverObject


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/coin.rb', line 54

def server
  return remote_server if remote_uri
  return nil unless ENV["COIN_URI"].nil?

  if server_running?
    if @server
      begin
        @server.ok? if @server
      rescue DRb::DRbConnError => ex
        @server = nil
      end
    end

    if @server.nil?
      begin
        @server = DRbObject.new_with_uri(uri)
        @server.ok?
      rescue DRb::DRbConnError => ex
        @server = nil
      end
    end
  end

  return @server if @server && server_running?

  start_server
  @server = DRbObject.new_with_uri(uri)
end

.server_running?Boolean

Returns:

  • (Boolean)

96
97
98
99
100
101
102
103
104
105
# File 'lib/coin.rb', line 96

def server_running?
  @pid = pid
  return false unless @pid
  begin
    Process.kill(0, @pid.to_i)
  rescue Errno::ESRCH => ex
    return false
  end
  true
end

.start_server(force = nil) ⇒ Object


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/coin.rb', line 107

def start_server(force=nil)
  return if server_running? && !force
  stop_server if force
  ruby = "#{RbConfig::CONFIG["bindir"]}/ruby"
  script = File.expand_path(File.join(File.dirname(__FILE__), "..", "bin", "coin"))
  env = {
    "COIN_URI" => Coin.uri
  }
  pid = spawn(env, "#{ruby} #{script}")
  Process.detach(pid)

  sleep 0.1 while !server_running?
  DRb.start_service
  true
end

.stop_serverObject


123
124
125
126
127
128
# File 'lib/coin.rb', line 123

def stop_server
  Process.kill("HUP", @pid.to_i) if server_running?
  sleep 0.1 while server_running?
  DRb.stop_service
  true
end

.write(key, value, lifetime = 300) ⇒ Object


23
24
25
# File 'lib/coin.rb', line 23

def write(key, value, lifetime=300)
  server.write key, value, lifetime
end