Class: Lescopr::Modes::DirectMode

Inherits:
Object
  • Object
show all
Defined in:
lib/lescopr/modes/direct.rb

Overview

DirectMode — in-memory queue + HTTPS batch flush to /logs/ingest.

Designed for serverless / ephemeral Ruby processes:

- AWS Lambda (ruby runtime)
- Heroku one-off dynos
- Short-lived Rake tasks or scripts

Guarantees:

- Periodic flush every FLUSH_INTERVAL seconds (background thread)
- Guaranteed flush on process exit via at_exit hook
- Re-queue on network error (up to MAX_QUEUE entries)

Constant Summary collapse

FLUSH_INTERVAL =

seconds

15
BATCH_SIZE =
100
MAX_QUEUE =
500

Instance Method Summary collapse

Constructor Details

#initialize(http_client) ⇒ DirectMode

Returns a new instance of DirectMode.



24
25
26
27
28
29
30
# File 'lib/lescopr/modes/direct.rb', line 24

def initialize(http_client)
  @http_client = http_client
  @queue       = []
  @mutex       = Mutex.new
  @stop_event  = false
  @thread      = nil
end

Instance Method Details

#add_log(entry) ⇒ Object

── Public API ─────────────────────────────────────────────────────────



50
51
52
53
54
55
56
57
# File 'lib/lescopr/modes/direct.rb', line 50

def add_log(entry)
  @mutex.synchronize do
    @queue << entry
    if @queue.size > MAX_QUEUE
      @queue = @queue.last(MAX_QUEUE / 2)
    end
  end
end

#flush_syncObject

── Flush ──────────────────────────────────────────────────────────────



61
62
63
64
65
66
67
68
# File 'lib/lescopr/modes/direct.rb', line 61

def flush_sync
  loop do
    batch = @mutex.synchronize { @queue.shift(BATCH_SIZE) }
    break if batch.empty?

    send_batch(batch)
  end
end

#startObject

── Lifecycle ──────────────────────────────────────────────────────────



34
35
36
37
38
39
40
# File 'lib/lescopr/modes/direct.rb', line 34

def start
  install_exit_hook!
  start_flush_thread!

  Lescopr::Monitoring::Logger.new.info("[DIRECT] Mode direct actif (HTTPS batch)")
  self
end

#stopObject



42
43
44
45
46
# File 'lib/lescopr/modes/direct.rb', line 42

def stop
  @stop_event = true
  @thread&.kill
  flush_sync
end