-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathstackify-api-ruby.rb
More file actions
212 lines (177 loc) · 6.12 KB
/
stackify-api-ruby.rb
File metadata and controls
212 lines (177 loc) · 6.12 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
205
206
207
208
209
210
211
212
require 'stackify/version'
require 'stackify/utils/methods'
require 'core_ext/core_ext' unless defined? Rails
module Stackify
INTERNAL_LOG_PREFIX = '[Stackify]'.freeze
STATUSES = { working: 'working', terminating: 'terminating', terminated: 'terminated'}
MODES = { logging: :logging, metrics: :metrics, both: :both }
TRANSPORT = [DEFAULT = 'default', UNIX_SOCKET = 'agent_socket', AGENT_HTTP = 'agent_http']
autoload :Backtrace, 'stackify/utils/backtrace'
autoload :MsgObject, 'stackify/utils/msg_object'
autoload :Configuration, 'stackify/utils/configuration'
autoload :HttpClient, 'stackify/http_client'
autoload :Authorizable, 'stackify/authorization/authorizable'
autoload :EnvDetails, 'stackify/env_details'
autoload :Scheduler, 'stackify/scheduler'
autoload :ScheduleTask, 'stackify/schedule_task'
autoload :ScheduleDelay, 'stackify/schedule_delay'
autoload :Worker, 'stackify/workers/worker'
autoload :AuthWorker, 'stackify/workers/auth_worker'
autoload :LogsSenderWorker, 'stackify/workers/logs_sender_worker'
autoload :MsgsQueueWorker, 'stackify/workers/msgs_queue_worker'
autoload :AddMsgWorker, 'stackify/workers/add_msg_worker'
autoload :MsgsQueue, 'stackify/msgs_queue'
autoload :LoggerClient, 'stackify/logger_client'
autoload :AgentClient, 'stackify/agent_client'
autoload :TransportSelector, 'stackify/transport_selector'
autoload :LogsSender, 'stackify/logs_sender'
autoload :AgentBaseSender, 'stackify/agent_base_sender'
autoload :UnixSocketSender, 'stackify/unix_socket_sender'
autoload :AgentHTTPSender, 'stackify/agent_http_sender'
autoload :LoggerProxy, 'stackify/logger_proxy'
autoload :StackifiedError, 'stackify/error'
autoload :StringException, 'stackify/error'
autoload :ErrorsGovernor, 'stackify/errors_governor'
autoload :Metrics, 'stackify/metrics/metrics'
autoload :Rum, 'stackify/rum'
include Authorizable
class << self
attr_writer :config
def configuration
@config ||= Stackify::Configuration.new
end
def rum
@rum ||= Stackify::Rum.new(configuration)
end
def setup
@workers = []
yield(configuration) if block_given?
configuration.validate_transport_type
if configuration.is_valid?
@status = STATUSES[:working]
else
msg = "Stackify's configuration is not valid!"
configuration.errors.each do |error_msg|
msg += "\n" + error_msg
end
raise msg
end
end
def msgs_queue
@msgs_queue ||= Stackify::MsgsQueue.new
end
def logger_client
@logger_client ||= Stackify::LoggerClient.new
end
def agent_client
@agent_client ||= Stackify::AgentClient.new
end
def get_transport
@logger_client.get_transport
end
def send_unix_socket
@unix_socket ||= Stackify::UnixSocketSender.new
end
def logger
self.configuration.logger
end
def shutdown_all caller_obj=nil
Stackify.status = Stackify::STATUSES[:terminating]
@workers.each do |worker|
worker.shutdown! unless worker.equal? caller_obj
end
end
def alive_adding_msg_workers
@workers.select{ |w| w.alive? && w.type == :add_msg }
end
def delete_worker worker
@workers.delete worker
end
def add_dependant_worker worker
@workers << worker
end
def status
@status
end
def log_internal_error msg
Stackify.logger.error (::Stackify::INTERNAL_LOG_PREFIX){ msg }
end
def internal_log level, msg
Stackify.logger.send(level.downcase.to_sym, Stackify::INTERNAL_LOG_PREFIX){ msg }
end
def run
Stackify::Utils.is_api_enabled
Stackify.internal_log :info, "Stackify.run() transportType: #{Stackify.configuration.transport} | API version: #{Stackify::VERSION} | Ruby version: #{RUBY_VERSION}"
if Stackify.configuration.api_enabled
if Stackify.is_valid?
# check transport types
case Stackify.configuration.transport
when Stackify::DEFAULT
if Stackify.is_valid?
at_exit { make_remained_job }
t1 = Thread.new { Stackify.authorize }
case Stackify.configuration.mode
when MODES[:both]
t2 = start_logging
t3 = start_metrics
when MODES[:logging]
t2 = start_logging
when MODES[:metrics]
t3 = start_metrics
end
t1.join
t3.join if t3
else
Stackify.log_internal_error "Stackify is not properly configured! Errors: #{Stackify.configuration.errors}"
end
when Stackify::UNIX_SOCKET, Stackify::AGENT_HTTP
case Stackify.configuration.mode
when MODES[:logging]
start_logging
when MODES[:both]
start_logging
start_metrics
when MODES[:metrics]
start_metrics
end
else
Stackify.log_internal_error "Stackify is not properly configured! Errors: #{Stackify.configuration.errors}"
end
end
end
end
def start_logging
msgs_queue
end
def start_metrics
Thread.new { Stackify::Metrics.metrics_client.start }
end
def workers
@workers
end
def make_remained_job
@status = STATUSES[:terminating]
Stackify.msgs_queue.push_remained_msgs
end
def is_valid?
configuration.is_valid?
end
def terminating?
@status == STATUSES[:terminating]
end
def terminated?
@status == STATUSES[:terminated]
end
def working?
@status == STATUSES[:working]
end
def status= status
if STATUSES.has_value? status
@status = status
else
raise "method 'status=' should get one of arguments #{STATUSES.values}, not a #{status}"
end
end
end
end
require 'stackify/engine' if defined? Rails