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
|
# File 'lib/mobot/server.rb', line 6
def self.start
lock_file = File.open('/tmp/mobot.lock', 'w')
if lock_file.flock(File::LOCK_EX | File::LOCK_NB)
server = UNIXServer.new(SOCKET_FILE)
parent_process_id = Process.pid
at_exit do
File.unlink(SOCKET_FILE) if Process.pid == parent_process_id
end
trap(:INT) { exit }
puts "mobot reporting for duty"
loop do
client = server.accept
command = client.read
pid = fork do
$stdout.reopen(client)
puts "mobot received command: #{command}"
begin
exec(command)
rescue Exception => e
puts "something very bad happended. #{e.message}"
end
end
Process.wait(pid)
client.close
end
else
p "there can only be one. bye bye."
end
end
|