Class: Chronicle::ApiLogs::Flusher

Inherits:
Object
  • Object
show all
Defined in:
app/services/chronicle/api_logs/flusher.rb

Overview

Reads all per-PID buffer files, bulk-inserts their contents into chronicle_api_logs, upserts api_routes, and deletes the consumed files.

Atomic-rename strategy: each buffer file is renamed to a ‘.flushing-*` name before reading, so concurrent writers (same PID) recreate the original file and never lose entries.

Constant Summary collapse

INSERT_BATCH_SIZE =
1000

Class Method Summary collapse

Class Method Details

.callObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/chronicle/api_logs/flusher.rb', line 15

def call
  paths = claim_files
  return { files: 0, records: 0 } if paths.empty?

  records = paths.flat_map { |p| read_jsonl(p) }
  return cleanup_and_return(paths, 0) if records.empty?

  insert_logs(records)
  sync_routes(records)

  cleanup_and_return(paths, records.size)
end

.sync_routes(records) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/chronicle/api_logs/flusher.rb', line 28

def sync_routes(records)
  now = Time.current
  pairs = records.filter_map do |r|
    path = r['api_endpoint']
    method = r['http_method']
    next if path.blank? || method.blank?
    [path, method]
  end.uniq

  return if pairs.empty?

  rows = pairs.map do |path, method|
    { path: path, http_method: method, first_seen_at: now, created_at: now, updated_at: now }
  end

  ApiRoute.insert_all(rows, unique_by: 'index_chronicle_api_routes_on_path_and_method') # rubocop:disable Rails/SkipsModelValidations
end