Module: Prefab::PeriodicSync

Included in:
ContextShapeAggregator, EvaluationSummaryAggregator, ExampleContextsAggregator, LogPathAggregator
Defined in:
lib/prefab/periodic_sync.rb

Constant Summary collapse

LOG =
Prefab::InternalLogger.new("periodsync")

Instance Method Summary collapse

Instance Method Details

#on_prepare_dataObject



26
27
28
# File 'lib/prefab/periodic_sync.rb', line 26

def on_prepare_data
  # noop -- override as you wish
end

#poolObject



49
50
51
52
53
54
55
56
57
# File 'lib/prefab/periodic_sync.rb', line 49

def pool
  @pool ||= Concurrent::ThreadPoolExecutor.new(
    fallback_policy: :discard,
    max_queue: 5,
    max_threads: 4,
    min_threads: 1,
    name: @name
  )
end

#post(url, data) ⇒ Object



30
31
32
# File 'lib/prefab/periodic_sync.rb', line 30

def post(url, data)
  @client.post(url, data)
end

#prepare_dataObject



17
18
19
20
21
22
23
24
# File 'lib/prefab/periodic_sync.rb', line 17

def prepare_data
  to_ship = @data.dup
  @data.clear

  on_prepare_data

  to_ship
end

#start_periodic_sync(sync_interval) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/prefab/periodic_sync.rb', line 34

def start_periodic_sync(sync_interval)
  @start_at = Prefab::TimeHelpers.now_in_ms

  @sync_interval = calculate_sync_interval(sync_interval)

  Thread.new do
    LOG.debug "Initialized #{@name} instance_hash=#{@client.instance_hash}"

    loop do
      sleep @sync_interval.call
      sync
    end
  end
end

#syncObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/prefab/periodic_sync.rb', line 6

def sync
  return if @data.size.zero?

  LOG.debug "Syncing #{@data.size} items"

  start_at_was = @start_at
  @start_at = Prefab::TimeHelpers.now_in_ms

  flush(prepare_data, start_at_was)
end