This repository was archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocker.rb
More file actions
126 lines (94 loc) · 3.33 KB
/
locker.rb
File metadata and controls
126 lines (94 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env ruby
#lock file helper
require 'yaml'
require 'mailer'
class Locker
def initialize ( hsh,logger )
@logger = logger
end
def lock
end
def unlock
end
def active?
end
end
class YamlLock < Locker
def initialize ( hsh,logger,path )
supper(hsh, logger)
@lock_path = path
##
# Should be configurable...
@lock_time_out = 2*60 #2 min..
end
def lock
end
def unlock
end
def active?
end
private
def lock_check ( )
return false if ( !File.exists?(@lock_path))
return false if ( File.size?(@lock_path))
lock_cfg = load_lock()
return false if (!lock_cfg)
return false if ((Time.now - lock_cfg["lock_tm"]) > 2)
return true
end
def load_lock ( )
return File.open(@lock_path) { |x| YAML.load(x) }
end
end
class RunLock
##
# Hsh is a hash
# should look like
# [lockfile] => "pathtolockfile"
# [message] => "lock file msg..."
# [mailer] -> {mailer conf}
def initialize ( hsh )
@hsh = hsh
@pid = Process.pid
if ( !File.exists?(hsh['lockfile']) )
lock()
else
##
# lock allready exists, read it, then decide if to email out warning...
lock_info = YAML.load(File.open(hsh['lockfile']) )
if (lock_info['lock_time'].to_f < (Time.now - 24*60*60).to_f)
if ( hsh['alert'] )
body = []
body << "A lock is still active."
body << "It looks like "
body << "-----------------------------"
body << YAML.dump(lock_info )
Mailer.deliver_message(hsh["mailer"], body)
end
end
if (!File.exists?("/proc/" + lock_info["pid"].to_s) )
if ( hsh['alert'] )
body = []
body << "A lock is still active but the pid (#{hsh['pid']}) is not active."
body << "-----------------------------"
body << YAML.dump(lock_info )
Mailer.deliver_message(hsh["mailer"], body)
end
end
raise RuntimeError, "Lock still active."
end
end
def lock ( )
conf = {
"message" => @hsh["message"],
"pid" => @pid,
"lock_time" => Time.now
}
File.open( @hsh["lockfile"], 'w' ) do |out|
YAML.dump( conf, out )
end
end
def unlock( )
File.unlink(@hsh["lockfile"])
end
end