Class: RestFtpDaemon::JobQueue

Inherits:
Queue
  • Object
show all
Defined in:
lib/rest-ftp-daemon/job_queue.rb

Instance Method Summary collapse

Constructor Details

#initializeJobQueue

Returns a new instance of JobQueue.



6
7
8
9
10
11
12
13
14
15
# File 'lib/rest-ftp-daemon/job_queue.rb', line 6

def initialize
  @queued = []
  @popped = []

  @waiting = []
  @queued.taint          # enable tainted communication
  @waiting.taint
  self.taint
  @mutex = Mutex.new
end

Instance Method Details

#allObject



31
32
33
# File 'lib/rest-ftp-daemon/job_queue.rb', line 31

def all
  @queued + @popped
end

#all_sizeObject



34
35
36
# File 'lib/rest-ftp-daemon/job_queue.rb', line 34

def all_size
  popped_size + queued_size
end

#clearObject



80
81
82
# File 'lib/rest-ftp-daemon/job_queue.rb', line 80

def clear
  @queued.clear
end

#empty?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/rest-ftp-daemon/job_queue.rb', line 76

def empty?
  @queued.empty?
end

#num_waitingObject



84
85
86
# File 'lib/rest-ftp-daemon/job_queue.rb', line 84

def num_waiting
  @waiting.size
end

#pop(non_block = false) ⇒ Object Also known as: shift, deq

Retrieves data from the queue. If the queue is empty, the calling thread is suspended until data is pushed onto the queue. If non_block is true, the thread isn’t suspended, and an exception is raised.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rest-ftp-daemon/job_queue.rb', line 60

def pop(non_block=false)
  @mutex.synchronize{
    while true
      if @queued.empty?
        raise ThreadError, "queue empty" if non_block
        @waiting.push Thread.current
        @mutex.sleep
      else
        return pick
      end
    end
  }
end

#poppedObject



24
25
26
# File 'lib/rest-ftp-daemon/job_queue.rb', line 24

def popped
  @popped
end

#popped_sizeObject



27
28
29
# File 'lib/rest-ftp-daemon/job_queue.rb', line 27

def popped_size
  @popped.length
end

#push(obj) ⇒ Object Also known as: <<, enq



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rest-ftp-daemon/job_queue.rb', line 38

def push(obj)
  # Check that itme responds to "priorty" method
  raise "object should respond to priority method" unless obj.respond_to? :priority

  @mutex.synchronize{
    @queued.push obj
    begin
      t = @waiting.shift
      t.wakeup if t
    rescue ThreadError
      retry
    end
  }
end

#queuedObject



17
18
19
# File 'lib/rest-ftp-daemon/job_queue.rb', line 17

def queued
  @queued
end

#queued_sizeObject



20
21
22
# File 'lib/rest-ftp-daemon/job_queue.rb', line 20

def queued_size
  @queued.length
end