-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathclient.rb
More file actions
204 lines (178 loc) · 5.95 KB
/
Copy pathclient.rb
File metadata and controls
204 lines (178 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# frozen_string_literal: true
require 'thread'
require 'time'
require 'segment/analytics/defaults'
require 'segment/analytics/logging'
require 'segment/analytics/utils'
require 'segment/analytics/worker'
module Segment
class Analytics
class Client
include Segment::Analytics::Utils
include Segment::Analytics::Logging
# @param [Hash] opts
# @option opts [String] :write_key Your project's write_key
# @option opts [FixNum] :max_queue_size Maximum number of calls to be
# remain queued.
# @option opts [Proc] :on_error Handles error calls from the API.
def initialize(opts = {})
symbolize_keys!(opts)
@queue = Queue.new
@test = opts[:test]
@write_key = opts[:write_key]
@max_queue_size = opts[:max_queue_size] || Defaults::Queue::MAX_SIZE
@worker_mutex = Mutex.new
@worker = Worker.new(@queue, @write_key, opts)
@worker_thread = nil
check_write_key!
at_exit { @worker_thread && @worker_thread[:should_exit] = true }
end
# Synchronously waits until the worker has flushed the queue.
#
# Use only for scripts which are not long-running, and will specifically
# exit
def flush
while !@queue.empty? || @worker.is_requesting?
ensure_worker_running
sleep(0.1)
end
end
# @!macro common_attrs
# @option attrs [String] :anonymous_id ID for a user when you don't know
# who they are yet. (optional but you must provide either an
# `anonymous_id` or `user_id`)
# @option attrs [Hash] :context ({})
# @option attrs [Hash] :integrations What integrations this event
# goes to (optional)
# @option attrs [String] :message_id ID that uniquely
# identifies a message across the API. (optional)
# @option attrs [Time] :timestamp When the event occurred (optional)
# @option attrs [String] :user_id The ID for this user in your database
# (optional but you must provide either an `anonymous_id` or `user_id`)
# @option attrs [Hash] :options Options such as user traits (optional)
# Tracks an event
#
# @see https://segment.com/docs/sources/server/ruby/#track
#
# @param [Hash] attrs
#
# @option attrs [String] :event Event name
# @option attrs [Hash] :properties Event properties (optional)
# @macro common_attrs
def track(attrs)
symbolize_keys! attrs
enqueue(FieldParser.parse_for_track(attrs))
end
# Identifies a user
#
# @see https://segment.com/docs/sources/server/ruby/#identify
#
# @param [Hash] attrs
#
# @option attrs [Hash] :traits User traits (optional)
# @macro common_attrs
def identify(attrs)
symbolize_keys! attrs
enqueue(FieldParser.parse_for_identify(attrs))
end
# Aliases a user from one id to another
#
# @see https://segment.com/docs/sources/server/ruby/#alias
#
# @param [Hash] attrs
#
# @option attrs [String] :previous_id The ID to alias from
# @macro common_attrs
def alias(attrs)
symbolize_keys! attrs
enqueue(FieldParser.parse_for_alias(attrs))
end
# Associates a user identity with a group.
#
# @see https://segment.com/docs/sources/server/ruby/#group
#
# @param [Hash] attrs
#
# @option attrs [String] :group_id The ID of the group
# @option attrs [Hash] :traits User traits (optional)
# @macro common_attrs
def group(attrs)
symbolize_keys! attrs
enqueue(FieldParser.parse_for_group(attrs))
end
# Records a page view
#
# @see https://segment.com/docs/sources/server/ruby/#page
#
# @param [Hash] attrs
#
# @option attrs [String] :name Name of the page
# @option attrs [Hash] :properties Page properties (optional)
# @macro common_attrs
def page(attrs)
symbolize_keys! attrs
enqueue(FieldParser.parse_for_page(attrs))
end
# Records a screen view (for a mobile app)
#
# @param [Hash] attrs
#
# @option attrs [String] :name Name of the screen
# @option attrs [Hash] :properties Screen properties (optional)
# @option attrs [String] :category The screen category (optional)
# @macro common_attrs
def screen(attrs)
symbolize_keys! attrs
enqueue(FieldParser.parse_for_screen(attrs))
end
# @return [Fixnum] number of messages in the queue
def queued_messages
@queue.length
end
def test_queue
unless @test
raise 'Test queue only available when setting :test to true.'
end
@test_queue ||= TestQueue.new
end
private
# private: Enqueues the action.
#
# returns Boolean of whether the item was added to the queue.
def enqueue(action)
# add our request id for tracing purposes
action[:messageId] ||= uid
if @test
test_queue << action
return true
end
if @queue.length < @max_queue_size
@queue << action
ensure_worker_running
true
else
logger.warn(
'Queue is full, dropping events. The :max_queue_size configuration parameter can be increased to prevent this from happening.'
)
false
end
end
# private: Checks that the write_key is properly initialized
def check_write_key!
raise ArgumentError, 'Write key must be initialized' if @write_key.nil?
end
def ensure_worker_running
return if worker_running?
@worker_mutex.synchronize do
return if worker_running?
@worker_thread = Thread.new do
@worker.run
end
end
end
def worker_running?
@worker_thread && @worker_thread.alive?
end
end
end
end