Class: Meeseeks::Harvester

Inherits:
Object
  • Object
show all
Defined in:
lib/meeseeks/harvester.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue:, http_trap:, interval: 60, max_batch_size: 100) ⇒ Harvester

Returns a new instance of Harvester.



11
12
13
14
15
16
17
18
# File 'lib/meeseeks/harvester.rb', line 11

def initialize(queue:, http_trap:, interval: 60, max_batch_size: 100)
  @queue = queue
  @http_trap = http_trap
  @interval = interval
  @max_batch_size = max_batch_size
  @cycle_count = 0
  @lock = Mutex.new
end

Instance Attribute Details

#cycle_countObject (readonly)

Returns the value of attribute cycle_count.



9
10
11
# File 'lib/meeseeks/harvester.rb', line 9

def cycle_count
  @cycle_count
end

#intervalObject (readonly)

Returns the value of attribute interval.



9
10
11
# File 'lib/meeseeks/harvester.rb', line 9

def interval
  @interval
end

#max_batch_sizeObject (readonly)

Returns the value of attribute max_batch_size.



9
10
11
# File 'lib/meeseeks/harvester.rb', line 9

def max_batch_size
  @max_batch_size
end

Instance Method Details

#drainObject



54
55
56
57
58
# File 'lib/meeseeks/harvester.rb', line 54

def drain
  @lock.synchronize do
    drain_queue_async
  end
end

#killObject



46
47
48
# File 'lib/meeseeks/harvester.rb', line 46

def kill
  @thread&.kill if @thread&.alive?
end

#running?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/meeseeks/harvester.rb', line 50

def running?
  @thread&.alive?
end

#startObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/meeseeks/harvester.rb', line 27

def start
  @continue = true
  return if @thread&.alive?

  @thread = Thread.new do
    loop do
      sleep(@interval)
      break unless @continue

      @cycle_count += 1
      drain
    end
  end
end

#statsObject



20
21
22
23
24
25
# File 'lib/meeseeks/harvester.rb', line 20

def stats
  {
    cycle_count: @cycle_count,
    running: running?
  }
end

#stopObject



42
43
44
# File 'lib/meeseeks/harvester.rb', line 42

def stop
  @continue = false
end