Class: VWO::Services::BatchEventsQueue

Inherits:
Object
  • Object
show all
Includes:
Enums
Defined in:
lib/vwo/services/batch_events_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(batch_config, is_development_mode = false) ⇒ BatchEventsQueue

Returns a new instance of BatchEventsQueue.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vwo/services/batch_events_queue.rb', line 24

def initialize(batch_config, is_development_mode = false)
  @is_development_mode = is_development_mode
  @logger = VWO::Utils::Logger
  @queue = []
   = {}
  @batch_config = batch_config

  if batch_config[:request_time_interval]
    @request_time_interval = batch_config[:request_time_interval]
  else
    @request_time_interval = CONSTANTS::DEFAULT_REQUEST_TIME_INTERVAL
    @logger.log(
      LogLevelEnum::INFO,
      'EVENT_BATCH_DEFAULTS',
      {
        '{file}' => FileNameEnum::BATCH_EVENTS_QUEUE,
        '{parameter}' => 'request_time_interval',
        '{defaultValue}' => "#{@request_time_interval} ms"
      }
    )
  end

  if batch_config[:events_per_request]
    @events_per_request = batch_config[:events_per_request]
  else
    @events_per_request = CONSTANTS::DEFAULT_EVENTS_PER_REQUEST
    @logger.log(
      LogLevelEnum::INFO,
      'EVENT_BATCH_DEFAULTS',
      {
        '{file}' => FileNameEnum::BATCH_EVENTS_QUEUE,
        '{parameter}' => 'events_per_request',
        '{defaultValue}' => @events_per_request.to_s
      }
    )
  end

  @flush_callback = nil
  @flush_callback = batch_config[:flushCallback] if batch_config.key?(:flushCallback) && batch_config[:flushCallback].is_a?(Method)

  @dispatcher = batch_config[:dispatcher]
end

Instance Method Details

#clear_request_timerObject



132
133
134
# File 'lib/vwo/services/batch_events_queue.rb', line 132

def clear_request_timer
  @timer = nil
end

#create_new_batch_timerObject



67
68
69
# File 'lib/vwo/services/batch_events_queue.rb', line 67

def create_new_batch_timer
  @timer = Time.now + @request_time_interval
end

#enqueue(event) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vwo/services/batch_events_queue.rb', line 71

def enqueue(event)
  return true if @is_development_mode

  @queue.push(event)
  (event)
  unless @timer
    create_new_batch_timer
    @thread = Thread.new { flush_when_request_times_up }
  end
  return unless @events_per_request == @queue.length

  flush
  kill_old_thread
end

#flush(manual = false) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/vwo/services/batch_events_queue.rb', line 92

def flush(manual = false)
  if @queue.length > 0
    @logger.log(
      LogLevelEnum::DEBUG,
      'EVENT_BATCH_BEFORE_FLUSHING',
      {
        '{file}' => FileNameEnum::BATCH_EVENTS_QUEUE,
        '{manually}' => manual ? 'manually' : '',
        '{length}' => @queue.length,
        '{timer}' => manual ? 'Timer will be cleared and registered again,' : '',
        '{accountId}' => @batch_config[:account_id]
      }
    )

    @dispatcher.call(@queue, @flush_callback)

    @logger.log(
      LogLevelEnum::INFO,
      'EVENT_BATCH_After_FLUSHING',
      {
        '{file}' => FileNameEnum::BATCH_EVENTS_QUEUE,
        '{manually}' => manual ? 'manually,' : '',
        '{length}' => @queue.length
      }
    )
     = {}
    @queue = []
  else
    @logger.log(
      LogLevelEnum::INFO,
      'Batch queue is empty. Nothing to flush.',
      { '{file}' => FILE }
    )
  end

  clear_request_timer
  @old_thread = @thread if !manual && @thread
  true
end

#flush_when_request_times_upObject



86
87
88
89
90
# File 'lib/vwo/services/batch_events_queue.rb', line 86

def flush_when_request_times_up
  sleep(1) while @timer > Time.now
  flush
  kill_old_thread
end

#kill_old_threadObject



140
141
142
# File 'lib/vwo/services/batch_events_queue.rb', line 140

def kill_old_thread
  @old_thread&.kill
end

#kill_threadObject



136
137
138
# File 'lib/vwo/services/batch_events_queue.rb', line 136

def kill_thread
  @thread&.kill
end

#update_queue_metadata(event) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/vwo/services/batch_events_queue.rb', line 144

def (event)
  case event[:eT]
  when 1
    [VWO::EVENTS::TRACK_USER] = 0 unless .key?(VWO::EVENTS::TRACK_USER)
    [VWO::EVENTS::TRACK_USER] = [VWO::EVENTS::TRACK_USER] + 1
  when 2
    [VWO::EVENTS::TRACK_GOAL] = 0 unless .key?(VWO::EVENTS::TRACK_GOAL)
    [VWO::EVENTS::TRACK_GOAL] = [VWO::EVENTS::TRACK_GOAL] + 1
  when 3
    [VWO::EVENTS::PUSH] = 0 unless .key?(VWO::EVENTS::PUSH)
    [VWO::EVENTS::PUSH] = [VWO::EVENTS::PUSH] + 1
  end
end