Class: Sparrow::Queues::Memory

Inherits:
Object
  • Object
show all
Includes:
Miscel
Defined in:
lib/sparrow/queues/memory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Miscel

#base_dir, #log_path, #logger, #options, #options=, #pid_dir

Constructor Details

#initialize(queue_name) ⇒ Memory

Returns a new instance of Memory.



11
12
13
14
15
16
17
# File 'lib/sparrow/queues/memory.rb', line 11

def initialize(queue_name)
  self.queue_name = queue_name
  self.queue_data = []
  self.count_pop = 0
  self.count_push = 0
  recover!
end

Instance Attribute Details

#count_popObject

Returns the value of attribute count_pop.



8
9
10
# File 'lib/sparrow/queues/memory.rb', line 8

def count_pop
  @count_pop
end

#count_pushObject

Returns the value of attribute count_push.



9
10
11
# File 'lib/sparrow/queues/memory.rb', line 9

def count_push
  @count_push
end

#queue_dataObject

Returns the value of attribute queue_data.



7
8
9
# File 'lib/sparrow/queues/memory.rb', line 7

def queue_data
  @queue_data
end

#queue_nameObject

Returns the value of attribute queue_name.



6
7
8
# File 'lib/sparrow/queues/memory.rb', line 6

def queue_name
  @queue_name
end

Instance Method Details

#clear!Object



29
30
31
32
# File 'lib/sparrow/queues/memory.rb', line 29

def clear!
  self.queue_data = []
  self.sqlite.clear!
end

#countObject



34
35
36
# File 'lib/sparrow/queues/memory.rb', line 34

def count
  queue_data.length
end

#popObject



19
20
21
22
# File 'lib/sparrow/queues/memory.rb', line 19

def pop
  self.count_pop += 1
  self.queue_data.shift
end

#push(value) ⇒ Object



24
25
26
27
# File 'lib/sparrow/queues/memory.rb', line 24

def push(value)
  self.count_push += 1
  self.queue_data.push(value)
end

#recover!Object



48
49
50
51
52
# File 'lib/sparrow/queues/memory.rb', line 48

def recover!
  logger.debug "Recovering queue"
  self.queue_data.concat(self.sqlite.all)
  self.sqlite.clear!
end

#shutdown!Object



44
45
46
# File 'lib/sparrow/queues/memory.rb', line 44

def shutdown!
  self.to_disk!
end

#sqliteObject



54
55
56
# File 'lib/sparrow/queues/memory.rb', line 54

def sqlite
  @sqlite ||= Sparrow::Queues::Sqlite.new(self.queue_name)
end

#to_disk!Object



38
39
40
41
42
# File 'lib/sparrow/queues/memory.rb', line 38

def to_disk!
  copy = self.queue_data.dup
  self.sqlite.insert(copy)
  self.queue_data = self.queue_data - copy
end