-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.rb
More file actions
74 lines (58 loc) · 1.5 KB
/
Copy pathworker.rb
File metadata and controls
74 lines (58 loc) · 1.5 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
require 'socket'
module JobQueue
class Client
def initialize(host, port)
@host = host
@port = port
end
def socket
TCPSocket.new(@host, @port)
end
def queue(hsh = {})
data = Marshal.dump(hsh)
sock = self.socket
sock.print("QUEUE #{data.size}\n\n#{data}")
line = sock.gets("\n\n")
sock.close
return line.to_i
end
def check(key)
sock = self.socket
sock.print("CHECK #{key}\n\n")
line = sock.gets("\n\n").rstrip
job = {}
case line
when /\ANOTFOUND/
job[:status] = :not_found
when /\AWORKING/
job[:status] = :working
when /\ADONE (\d+)/
serialized_data = sock.read($1.to_i)
job[:status] = :done
# Marshal will throw an exception if it encounters an unknown class
# TODO: catpure the exception? maybe.
job[:result] = Marshal.load(serialized_data)
else
job[:status] = :invalid
end
sock.close
return job
end
def list
sock = self.socket
socket.print("LIST\n\n")
length = sock.readline(" ").to_i
# Marshal will throw an exception if it encounters an unknown class
# TODO: catpure the exception? maybe.
Marhsal.load(sock.read(length))
end
end
end
if __FILE__ == $0
require 'my_worker'
client = JobQueue::Client.new("localhost", 2202)
key = client.queue({:worker => MyWorker})
while( line = client.check(key) =~ /WORKING/ )
puts "check"
end
end