Class: TinyWorkService
- Inherits:
-
Object
- Object
- TinyWorkService
- Defined in:
- lib/tiny_work_service.rb
Overview
usage:
s = TinyWorkService.new(1234)
s.stop!
Instance Method Summary collapse
-
#<<(j) ⇒ Object
enqueue a job.
-
#call(m) ⇒ Object
interface for TinyTCPService.
-
#initialize(port, label = 'TinyWorkService') ⇒ TinyWorkService
constructor
A new instance of TinyWorkService.
-
#join ⇒ Object
join the service Thread, if you want to wait until it’s done.
-
#shift ⇒ Object
return the first job in the work queue, if there is one present otherwise, return nil.
- #stop! ⇒ Object
Constructor Details
#initialize(port, label = 'TinyWorkService') ⇒ TinyWorkService
Returns a new instance of TinyWorkService.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/tiny_work_service.rb', line 7 def initialize(port, label='TinyWorkService') @service = TinyTCPService.new(port) @service.msg_handler = self @jobs = Queue.new @label = label @thread = Thread.new do loop do break unless @service.running? print "\r#{@label} #{@jobs.length.to_s.rjust(6)} jobs #{@service.num_clients.to_s.rjust(4)} workers\e[K" sleep 0.5 end end end |
Instance Method Details
#<<(j) ⇒ Object
enqueue a job
44 45 46 |
# File 'lib/tiny_work_service.rb', line 44 def <<(j) @jobs << j end |
#call(m) ⇒ Object
interface for TinyTCPService
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/tiny_work_service.rb', line 24 def call(m) raise TinyTCPService::BadClient.new("nil message") if m.nil? case when m[0] == '+' # add a job to the queue self << m[1..] 'ok' # ok, job received when m[0] == '-' # take a job from the queue shift || '' else raise TinyTCPService::BadClient.new("Client sent invalid message: `#{m[..50]}'") end end |
#join ⇒ Object
join the service Thread, if you want to wait until it’s done
39 40 41 |
# File 'lib/tiny_work_service.rb', line 39 def join @thread.join end |
#shift ⇒ Object
return the first job in the work queue, if there is one present otherwise, return nil
50 51 52 53 |
# File 'lib/tiny_work_service.rb', line 50 def shift return nil if @jobs.empty? @jobs.shift end |
#stop! ⇒ Object
55 56 57 |
# File 'lib/tiny_work_service.rb', line 55 def stop! @service.stop! end |