Class: Tobox::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/tobox/fetcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(label, configuration) ⇒ Fetcher

Returns a new instance of Fetcher.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tobox/fetcher.rb', line 7

def initialize(label, configuration)
  @label = label
  @configuration = configuration

  @logger = @configuration.default_logger

  @db = configuration.database

  @table = configuration[:table]
  @exponential_retry_factor = configuration[:exponential_retry_factor]

  max_attempts = configuration[:max_attempts]

  @ds = @db[@table]

  run_at_conds = [
    { Sequel[@table][:run_at] => nil },
    (Sequel.expr(Sequel[@table][:run_at]) < Sequel::CURRENT_TIMESTAMP)
  ].reduce { |agg, cond| Sequel.expr(agg) | Sequel.expr(cond) }

  @pick_next_sql = @ds.where(Sequel[@table][:attempts] < max_attempts) # filter out exhausted attempts
                      .where(run_at_conds)
                      .order(Sequel.desc(:run_at, nulls: :first), :id)

  @batch_size = configuration[:batch_size]

  @before_event_handlers = Array(@configuration.lifecycle_events[:before_event])
  @after_event_handlers = Array(@configuration.lifecycle_events[:after_event])
  @error_event_handlers = Array(@configuration.lifecycle_events[:error_event])
end

Instance Method Details

#fetch_events(&blk) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tobox/fetcher.rb', line 38

def fetch_events(&blk)
  num_events = 0
  events_tr do
    events = nil
    # @type var events: Array[event]?

    event_id_tr do
      events = do_fetch_events
    end

    if events && !events.empty?
      with_events(events) do |events|
        num_events = events.size

        prepare_events(events, &blk)
      end
    end
  end

  num_events
end