Class: WithLock::Server
- Inherits:
-
Object
show all
- Extended by:
- Common
- Defined in:
- lib/with_lock/server.rb
Class Method Summary
collapse
Methods included from Common
default_settings, load_settings, local_settings_filename, logger, pid, pidfile, running?, settings, settings_filename, url
Class Method Details
.count(name) ⇒ Object
23
24
25
26
27
|
# File 'lib/with_lock/server.rb', line 23
def self.count(name)
locks[name][:count]
rescue => e
0
end
|
.daemon_settings ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/with_lock/server.rb', line 86
def self.daemon_settings
{
:multiple => false,
:ontop => false,
:backtrace => true,
:log_output => true,
:monitor => false,
:dir_mode => :normal,
:dir => 'tmp/pids'
}
end
|
.decrement(owner, name) ⇒ Object
67
68
69
70
71
72
|
# File 'lib/with_lock/server.rb', line 67
def self.decrement(owner,name)
if mine?(owner,name)
locks[name][:count] -= 1
end
count(name)
end
|
.get(owner, name, timeout = 5) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/with_lock/server.rb', line 37
def self.get(owner,name,timeout=5)
endtime = Time.now.to_f + timeout.to_f
owner_uri, owner_pid = owner.split('|')
while !mine?(owner,name) && (Time.now.to_f < endtime)
mutex.synchronize do
if !locked?(name)
locks[name] = {owner: owner, count: 1}
end
end
sleep 0.01 unless mine?(owner,name)
end
mine?(owner,name)
end
|
.increment(owner, name) ⇒ Object
60
61
62
63
64
65
|
# File 'lib/with_lock/server.rb', line 60
def self.increment(owner,name)
if mine?(owner,name)
locks[name][:count] += 1
end
count(name)
end
|
.locked?(name) ⇒ Boolean
29
30
31
|
# File 'lib/with_lock/server.rb', line 29
def self.locked?(name)
!locks[name].nil?end
|
.locks ⇒ Object
74
75
76
|
# File 'lib/with_lock/server.rb', line 74
def self.locks
@@locks ||= {}
end
|
.mine?(owner, name) ⇒ Boolean
33
34
35
|
# File 'lib/with_lock/server.rb', line 33
def self.mine?(owner,name)
locked?(name) && locks[name][:owner].eql?(owner)
end
|
.mutex ⇒ Object
7
8
9
|
# File 'lib/with_lock/server.rb', line 7
def self.mutex
@@mutex ||= Mutex.new
end
|
.owner_pid(name) ⇒ Object
17
18
19
20
21
|
# File 'lib/with_lock/server.rb', line 17
def self.owner_pid(name)
locks[name][:pid]
rescue => e
''
end
|
.owner_uri?(name) ⇒ Boolean
11
12
13
14
15
|
# File 'lib/with_lock/server.rb', line 11
def self.owner_uri?(name)
locks[name][:owner]
rescue => e
''
end
|
.release(owner, name) ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/with_lock/server.rb', line 51
def self.release(owner,name)
if mine?(owner,name)
locks.delete(name)
true
else
false
end
end
|
.run_service ⇒ Object
108
109
110
111
112
|
# File 'lib/with_lock/server.rb', line 108
def self.run_service
return if started?
DRb.start_service(WithLock::Server::url,WithLock::Server)
DRb.thread.join
end
|
.start_service ⇒ Object
98
99
100
101
102
103
104
105
106
|
# File 'lib/with_lock/server.rb', line 98
def self.start_service
return if started?
FileUtils.rm(pidfile) if File.exists?(pidfile)
options = daemon_settings.merge(:ARGV => ['start'])
Daemons.run_proc('with_lock', options) do
DRb.start_service(WithLock::Server::url,WithLock::Server)
DRb.thread.join
end
end
|
.started? ⇒ Boolean
82
83
84
|
# File 'lib/with_lock/server.rb', line 82
def self.started?
pid.to_i > 0 && running?(pid)
end
|
.stop_service ⇒ Object
114
115
116
117
118
|
# File 'lib/with_lock/server.rb', line 114
def self.stop_service
return unless started?
`kill #{pid}`
FileUtils.rm_f(pidfile)
end
|
.write_url(url) ⇒ Object
78
79
80
|
# File 'lib/with_lock/server.rb', line 78
def self.write_url(url)
File.open(File.join(settings['directory'],'with_lock'),'w'){|file| file.write(url)}
end
|