forked from brightroll/rq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadminoper.rb
75 lines (61 loc) · 1.49 KB
/
adminoper.rb
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
module RQ
class AdminOper
attr_reader :admin_status
attr_reader :oper_status
def initialize(dirname, filename)
raise ArgumentError, "#{dirname} doesn't exist" unless File.directory? dirname
@down_file = File.join(dirname, filename + '.down')
@pause_file = File.join(dirname, filename + '.pause')
@oper_status = 'UP'
update!
end
# Combined admin/oper status report
def status
@oper_status != 'UP' ? @oper_status : @admin_status
end
def update!
if File.exist?(@down_file)
@admin_status = 'DOWN'
elsif File.exist?(@pause_file)
@admin_status = 'PAUSE'
else
@admin_status = 'UP'
end
end
def set_admin_status(stat)
success = case stat
when 'UP'
delete_file(@down_file)
when 'DOWN'
create_file(@down_file)
when 'PAUSE'
create_file(@pause_file)
when 'RESUME'
delete_file(@pause_file)
end
# Change internal state based on file changes
update!
# Return success/failure of file change actions
success
end
def set_oper_status(stat)
@oper_status = stat
end
private
def create_file(file)
unless File.exist?(file)
File.new(file, File::CREAT, 0644) rescue nil
else
true
end
end
def delete_file(file)
if File.exist?(file)
count = File.unlink(file) rescue 0
count > 0
else
true
end
end
end
end