Module: Prefab::PeriodicSync

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

Constant Summary collapse

LOG =
Prefab::InternalLogger.new(self)

Instance Method Summary collapse

Instance Method Details

#instance_hashObject



35
36
37
# File 'lib/prefab/periodic_sync.rb', line 35

def instance_hash
  @client.instance_hash
end

#on_prepare_dataObject



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

def on_prepare_data
  # noop -- override as you wish
end

#poolObject



54
55
56
57
58
59
60
61
62
# File 'lib/prefab/periodic_sync.rb', line 54

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



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

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

#prepare_dataObject



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

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

  on_prepare_data

  to_ship
end

#start_periodic_sync(sync_interval) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/prefab/periodic_sync.rb', line 39

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



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

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