Module: Tapsoob::ProgressEvent

Defined in:
lib/tapsoob/progress_event.rb

Class Method Summary collapse

Class Method Details

.clear_throttle(table_name) ⇒ Object

Clear throttle state for a table (call when table completes)



45
46
47
# File 'lib/tapsoob/progress_event.rb', line 45

def self.clear_throttle(table_name)
  @last_progress_time.delete(table_name)
end

.data_complete(table_count, record_count) ⇒ Object



63
64
65
# File 'lib/tapsoob/progress_event.rb', line 63

def self.data_complete(table_count, record_count)
  emit('data_complete', tables: table_count, records: record_count)
end

.data_start(table_count, record_count) ⇒ Object

Data events



59
60
61
# File 'lib/tapsoob/progress_event.rb', line 59

def self.data_start(table_count, record_count)
  emit('data_start', tables: table_count, records: record_count)
end

.emit(event_type, data = {}) ⇒ Object

Emit structured JSON progress events to STDERR for machine parsing Only emits when enabled (typically when CLI progress bars are disabled)



20
21
22
23
24
25
26
27
28
29
# File 'lib/tapsoob/progress_event.rb', line 20

def self.emit(event_type, data = {})
  return unless @enabled
  event = {
    event: event_type,
    timestamp: Time.now.utc.iso8601
  }.merge(data)

  STDERR.puts "PROGRESS: #{JSON.generate(event)}"
  STDERR.flush
end

.enabled=(value) ⇒ Object



10
11
12
# File 'lib/tapsoob/progress_event.rb', line 10

def self.enabled=(value)
  @enabled = value
end

.enabled?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/tapsoob/progress_event.rb', line 14

def self.enabled?
  @enabled
end

.error(message, context = {}) ⇒ Object

Error events



105
106
107
# File 'lib/tapsoob/progress_event.rb', line 105

def self.error(message, context = {})
  emit('error', { message: message }.merge(context))
end

.indexes_complete(table_count) ⇒ Object



91
92
93
# File 'lib/tapsoob/progress_event.rb', line 91

def self.indexes_complete(table_count)
  emit('indexes_complete', tables: table_count)
end

.indexes_start(table_count) ⇒ Object

Index events



87
88
89
# File 'lib/tapsoob/progress_event.rb', line 87

def self.indexes_start(table_count)
  emit('indexes_start', tables: table_count)
end

.schema_complete(table_count) ⇒ Object



54
55
56
# File 'lib/tapsoob/progress_event.rb', line 54

def self.schema_complete(table_count)
  emit('schema_complete', tables: table_count)
end

.schema_start(table_count) ⇒ Object

Schema events



50
51
52
# File 'lib/tapsoob/progress_event.rb', line 50

def self.schema_start(table_count)
  emit('schema_start', tables: table_count)
end

.sequences_completeObject



100
101
102
# File 'lib/tapsoob/progress_event.rb', line 100

def self.sequences_complete
  emit('sequences_complete')
end

.sequences_startObject

Sequence events



96
97
98
# File 'lib/tapsoob/progress_event.rb', line 96

def self.sequences_start
  emit('sequences_start')
end

.should_emit_progress?(table_name) ⇒ Boolean

Check if enough time has passed to emit a progress event for this table

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tapsoob/progress_event.rb', line 32

def self.should_emit_progress?(table_name)
  now = Time.now
  last_time = @last_progress_time[table_name]

  if last_time.nil? || (now - last_time) >= @progress_throttle
    @last_progress_time[table_name] = now
    true
  else
    false
  end
end

.table_complete(table_name, record_count) ⇒ Object



81
82
83
84
# File 'lib/tapsoob/progress_event.rb', line 81

def self.table_complete(table_name, record_count)
  clear_throttle(table_name)  # Clean up throttle state
  emit('table_complete', table: table_name, records: record_count)
end

.table_progress(table_name, current, total) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/tapsoob/progress_event.rb', line 73

def self.table_progress(table_name, current, total)
  # Throttle progress events to avoid spam
  return unless should_emit_progress?(table_name)

  percentage = total > 0 ? ((current.to_f / total) * 100).round(1) : 0
  emit('table_progress', table: table_name, current: current, total: total, percentage: percentage)
end

.table_start(table_name, record_count, workers: 1) ⇒ Object

Table-level events



68
69
70
71
# File 'lib/tapsoob/progress_event.rb', line 68

def self.table_start(table_name, record_count, workers: 1)
  clear_throttle(table_name)  # Reset throttle for new table
  emit('table_start', table: table_name, records: record_count, workers: workers)
end