Class: WorkQueue::Socket
- Inherits:
-
Object
- Object
- WorkQueue::Socket
- Defined in:
- lib/scout/work_queue/socket.rb
Instance Attribute Summary collapse
-
#cleaned ⇒ Object
Returns the value of attribute cleaned.
-
#exception ⇒ Object
Returns the value of attribute exception.
-
#read_sem ⇒ Object
Returns the value of attribute read_sem.
-
#sread ⇒ Object
Returns the value of attribute sread.
-
#swrite ⇒ Object
Returns the value of attribute swrite.
-
#write_sem ⇒ Object
Returns the value of attribute write_sem.
Instance Method Summary collapse
- #abort(exception) ⇒ Object
- #clean ⇒ Object
- #close_read ⇒ Object
- #close_write ⇒ Object
- #closed_read? ⇒ Boolean
- #closed_write? ⇒ Boolean
- #dump(obj) ⇒ Object
-
#initialize(serializer = nil) ⇒ Socket
constructor
A new instance of Socket.
- #load ⇒ Object
- #pop ⇒ Object (also: #read)
-
#push(obj) ⇒ Object
(also: #write)
{{{ ACCESSOR.
- #socket_id ⇒ Object
Constructor Details
#initialize(serializer = nil) ⇒ Socket
Returns a new instance of Socket.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/scout/work_queue/socket.rb', line 7 def initialize(serializer = nil) @sread, @swrite = Open.pipe @serializer = serializer || Marshal @key = "/" + rand(1000000000).to_s << '.' << Process.pid.to_s; @write_sem = @key + '.in' @read_sem = @key + '.out' Log.debug "Creating socket semaphores: #{@key}" ScoutSemaphore.create_semaphore(@write_sem,1) ScoutSemaphore.create_semaphore(@read_sem,1) end |
Instance Attribute Details
#cleaned ⇒ Object
Returns the value of attribute cleaned.
6 7 8 |
# File 'lib/scout/work_queue/socket.rb', line 6 def cleaned @cleaned end |
#exception ⇒ Object
Returns the value of attribute exception.
6 7 8 |
# File 'lib/scout/work_queue/socket.rb', line 6 def exception @exception end |
#read_sem ⇒ Object
Returns the value of attribute read_sem.
6 7 8 |
# File 'lib/scout/work_queue/socket.rb', line 6 def read_sem @read_sem end |
#sread ⇒ Object
Returns the value of attribute sread.
6 7 8 |
# File 'lib/scout/work_queue/socket.rb', line 6 def sread @sread end |
#swrite ⇒ Object
Returns the value of attribute swrite.
6 7 8 |
# File 'lib/scout/work_queue/socket.rb', line 6 def swrite @swrite end |
#write_sem ⇒ Object
Returns the value of attribute write_sem.
6 7 8 |
# File 'lib/scout/work_queue/socket.rb', line 6 def write_sem @write_sem end |
Instance Method Details
#abort(exception) ⇒ Object
124 125 126 127 |
# File 'lib/scout/work_queue/socket.rb', line 124 def abort(exception) @exception = exception @swrite.close unless closed_write? end |
#clean ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/scout/work_queue/socket.rb', line 24 def clean return if @cleaned @cleaned = true @sread.close unless @sread.closed? @swrite.close unless @swrite.closed? Log.low "Destroying socket semaphores: #{[@key] * ", "}" ScoutSemaphore.delete_semaphore(@write_sem) ScoutSemaphore.delete_semaphore(@read_sem) end |
#close_read ⇒ Object
105 106 107 |
# File 'lib/scout/work_queue/socket.rb', line 105 def close_read @sread.close unless closed_read? end |
#close_write ⇒ Object
100 101 102 103 |
# File 'lib/scout/work_queue/socket.rb', line 100 def close_write self.dump ClosedStream.new @swrite.close unless closed_write? end |
#closed_read? ⇒ Boolean
92 93 94 |
# File 'lib/scout/work_queue/socket.rb', line 92 def closed_read? @sread.closed? end |
#closed_write? ⇒ Boolean
96 97 98 |
# File 'lib/scout/work_queue/socket.rb', line 96 def closed_write? @swrite.closed? end |
#dump(obj) ⇒ Object
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 |
# File 'lib/scout/work_queue/socket.rb', line 35 def dump(obj) stream = @swrite obj.concurrent_stream = nil if obj.respond_to?(:concurrent_stream) case obj when Integer size_head = [obj,"I"].pack 'La' str = size_head when nil size_head = [0,"N"].pack 'La' str = size_head when Annotation::AnnotatedObject payload = @serializer.dump(obj) size_head = [payload.bytesize,"S"].pack 'La' str = size_head << payload when String payload = obj size_head = [payload.bytesize,"C"].pack 'La' str = size_head << payload else payload = @serializer.dump(obj) size_head = [payload.bytesize,"S"].pack 'La' str = size_head << payload end write_length = str.length wrote = stream.write(str) while wrote < write_length wrote += stream.write(str[wrote..-1]) end end |
#load ⇒ Object
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 |
# File 'lib/scout/work_queue/socket.rb', line 66 def load stream = @sread size_head = Open.read_stream stream, 5 size, type = size_head.unpack('La') return nil if type == "N" return size.to_i if type == "I" begin payload = Open.read_stream stream, size case type when "S" begin @serializer.load(payload) rescue Exception Log.exception $! raise $! end when "C" payload end rescue TryAgain retry end end |
#pop ⇒ Object Also known as: read
116 117 118 119 120 121 122 |
# File 'lib/scout/work_queue/socket.rb', line 116 def pop ScoutSemaphore.synchronize(@read_sem) do res = self.load raise res if ClosedStream === res res end end |
#push(obj) ⇒ Object Also known as: write
{{{ ACCESSOR
110 111 112 113 114 |
# File 'lib/scout/work_queue/socket.rb', line 110 def push(obj) ScoutSemaphore.synchronize(@write_sem) do self.dump(obj) end end |
#socket_id ⇒ Object
20 21 22 |
# File 'lib/scout/work_queue/socket.rb', line 20 def socket_id @key end |