مراية لـ
https://github.com/postalserver/postal.git
تم المزامنة 2025-12-01 05:43:04 +00:00
feat: more consistent logging
هذا الالتزام موجود في:
1
Gemfile
1
Gemfile
@@ -20,6 +20,7 @@ gem "hashie"
|
|||||||
gem "highline", require: false
|
gem "highline", require: false
|
||||||
gem "jwt"
|
gem "jwt"
|
||||||
gem "kaminari"
|
gem "kaminari"
|
||||||
|
gem "klogger-logger"
|
||||||
gem "mail"
|
gem "mail"
|
||||||
gem "moonrope"
|
gem "moonrope"
|
||||||
gem "mysql2"
|
gem "mysql2"
|
||||||
|
|||||||
@@ -147,6 +147,10 @@ GEM
|
|||||||
activerecord
|
activerecord
|
||||||
kaminari-core (= 1.2.2)
|
kaminari-core (= 1.2.2)
|
||||||
kaminari-core (1.2.2)
|
kaminari-core (1.2.2)
|
||||||
|
klogger-logger (1.3.2)
|
||||||
|
concurrent-ruby (>= 1.0, < 2.0)
|
||||||
|
json
|
||||||
|
rouge (>= 3.30, < 5.0)
|
||||||
loofah (2.22.0)
|
loofah (2.22.0)
|
||||||
crass (~> 1.0.2)
|
crass (~> 1.0.2)
|
||||||
nokogiri (>= 1.12.0)
|
nokogiri (>= 1.12.0)
|
||||||
@@ -229,6 +233,7 @@ GEM
|
|||||||
regexp_parser (2.7.0)
|
regexp_parser (2.7.0)
|
||||||
resolv (0.2.2)
|
resolv (0.2.2)
|
||||||
rexml (3.2.5)
|
rexml (3.2.5)
|
||||||
|
rouge (4.2.0)
|
||||||
rspec (3.12.0)
|
rspec (3.12.0)
|
||||||
rspec-core (~> 3.12.0)
|
rspec-core (~> 3.12.0)
|
||||||
rspec-expectations (~> 3.12.0)
|
rspec-expectations (~> 3.12.0)
|
||||||
@@ -345,6 +350,7 @@ DEPENDENCIES
|
|||||||
jquery-rails
|
jquery-rails
|
||||||
jwt
|
jwt
|
||||||
kaminari
|
kaminari
|
||||||
|
klogger-logger
|
||||||
mail
|
mail
|
||||||
moonrope
|
moonrope
|
||||||
mysql2
|
mysql2
|
||||||
|
|||||||
@@ -53,41 +53,42 @@ class WebhookRequest < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def deliver
|
def deliver
|
||||||
logger = Postal.logger_for(:webhooks)
|
|
||||||
payload = { event: event, timestamp: created_at.to_f, payload: self.payload, uuid: uuid }.to_json
|
payload = { event: event, timestamp: created_at.to_f, payload: self.payload, uuid: uuid }.to_json
|
||||||
logger.info "[#{id}] Sending webhook request to `#{url}`"
|
Postal.logger.tagged(event: event, url: url, component: "webhooks") do
|
||||||
result = Postal::HTTP.post(url, sign: true, json: payload, timeout: 5)
|
Postal.logger.info "Sending webhook request"
|
||||||
self.attempts += 1
|
result = Postal::HTTP.post(url, sign: true, json: payload, timeout: 5)
|
||||||
self.retry_after = RETRIES[self.attempts]&.from_now
|
self.attempts += 1
|
||||||
server.message_db.webhooks.record(
|
self.retry_after = RETRIES[self.attempts]&.from_now
|
||||||
event: event,
|
server.message_db.webhooks.record(
|
||||||
url: url,
|
event: event,
|
||||||
webhook_id: webhook_id,
|
url: url,
|
||||||
attempt: self.attempts,
|
webhook_id: webhook_id,
|
||||||
timestamp: Time.now.to_f,
|
attempt: self.attempts,
|
||||||
payload: self.payload.to_json,
|
timestamp: Time.now.to_f,
|
||||||
uuid: uuid,
|
payload: self.payload.to_json,
|
||||||
status_code: result[:code],
|
uuid: uuid,
|
||||||
body: result[:body],
|
status_code: result[:code],
|
||||||
will_retry: (retry_after ? 0 : 1)
|
body: result[:body],
|
||||||
)
|
will_retry: (retry_after ? 0 : 1)
|
||||||
|
)
|
||||||
|
|
||||||
if result[:code] >= 200 && result[:code] < 300
|
if result[:code] >= 200 && result[:code] < 300
|
||||||
logger.info "[#{id}] -> Received #{result[:code]} status code. That's OK."
|
Postal.logger.info "Received #{result[:code]} status code. That's OK."
|
||||||
destroy
|
|
||||||
webhook&.update_column(:last_used_at, Time.now)
|
|
||||||
true
|
|
||||||
else
|
|
||||||
logger.error "[#{id}] -> Received #{result[:code]} status code. That's not OK."
|
|
||||||
self.error = "Couldn't send to URL. Code received was #{result[:code]}"
|
|
||||||
if retry_after
|
|
||||||
logger.info "[#{id}] -> Will retry #{retry_after} (this was attempt #{self.attempts})"
|
|
||||||
save
|
|
||||||
else
|
|
||||||
logger.info "[#{id}] -> Have tried #{self.attempts} times. Giving up."
|
|
||||||
destroy
|
destroy
|
||||||
|
webhook&.update_column(:last_used_at, Time.now)
|
||||||
|
true
|
||||||
|
else
|
||||||
|
Postal.logger.error "Received #{result[:code]} status code. That's not OK."
|
||||||
|
self.error = "Couldn't send to URL. Code received was #{result[:code]}"
|
||||||
|
if retry_after
|
||||||
|
Postal.logger.info "Will retry #{retry_after} (this was attempt #{self.attempts})"
|
||||||
|
save
|
||||||
|
else
|
||||||
|
Postal.logger.info "Have tried #{self.attempts} times. Giving up."
|
||||||
|
destroy
|
||||||
|
end
|
||||||
|
false
|
||||||
end
|
end
|
||||||
false
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -38,9 +38,11 @@ module Postal
|
|||||||
require "postal/tracking_middleware"
|
require "postal/tracking_middleware"
|
||||||
config.middleware.insert_before ActionDispatch::HostAuthorization, Postal::TrackingMiddleware
|
config.middleware.insert_before ActionDispatch::HostAuthorization, Postal::TrackingMiddleware
|
||||||
|
|
||||||
config.logger = Postal.logger_for(:rails)
|
|
||||||
|
|
||||||
config.hosts << Postal.config.web.host
|
config.hosts << Postal.config.web.host
|
||||||
|
|
||||||
|
if Postal.config.logging.rails_log == false
|
||||||
|
config.logger = Logger.new("/dev/null")
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ module Clockwork
|
|||||||
|
|
||||||
configure do |config|
|
configure do |config|
|
||||||
config[:tz] = "UTC"
|
config[:tz] = "UTC"
|
||||||
config[:logger] = Postal.logger_for(:cron)
|
config[:logger] = Postal.logger
|
||||||
end
|
end
|
||||||
|
|
||||||
every 1.minute, "every-1-minutes" do
|
every 1.minute, "every-1-minutes" do
|
||||||
|
|||||||
@@ -72,12 +72,6 @@ Rails.application.configure do
|
|||||||
# require 'syslog/logger'
|
# require 'syslog/logger'
|
||||||
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
|
# config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
|
||||||
|
|
||||||
if ENV["RAILS_LOG_TO_STDOUT"].present?
|
|
||||||
logger = ActiveSupport::Logger.new($stdout)
|
|
||||||
logger.formatter = config.log_formatter
|
|
||||||
config.logger = ActiveSupport::TaggedLogging.new(logger)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Do not dump schema after migrations.
|
# Do not dump schema after migrations.
|
||||||
config.active_record.dump_schema_after_migration = false
|
config.active_record.dump_schema_after_migration = false
|
||||||
end
|
end
|
||||||
|
|||||||
51
config/initializers/logging.rb
Normal file
51
config/initializers/logging.rb
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
begin
|
||||||
|
def add_exception_to_payload(payload, event)
|
||||||
|
return unless exception = event.payload[:exception_object]
|
||||||
|
|
||||||
|
payload[:exception_class] = exception.class.name
|
||||||
|
payload[:exception_message] = exception.message
|
||||||
|
payload[:exception_backtrace] = exception.backtrace[0, 4].join("\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args|
|
||||||
|
event = ActiveSupport::Notifications::Event.new(*args)
|
||||||
|
|
||||||
|
payload = {
|
||||||
|
event: "request",
|
||||||
|
transaction: event.transaction_id,
|
||||||
|
controller: event.payload[:controller],
|
||||||
|
action: event.payload[:action],
|
||||||
|
format: event.payload[:format],
|
||||||
|
method: event.payload[:method],
|
||||||
|
path: event.payload[:path],
|
||||||
|
request_id: event.payload[:request].request_id,
|
||||||
|
ip_address: event.payload[:request].ip,
|
||||||
|
status: event.payload[:status],
|
||||||
|
view_runtime: event.payload[:view_runtime],
|
||||||
|
db_runtime: event.payload[:db_runtime]
|
||||||
|
}
|
||||||
|
|
||||||
|
add_exception_to_payload(payload, event)
|
||||||
|
|
||||||
|
string = "#{payload[:method]} #{payload[:path]} (#{payload[:status]})"
|
||||||
|
|
||||||
|
if payload[:exception_class]
|
||||||
|
Postal.logger.error(string, **payload)
|
||||||
|
else
|
||||||
|
Postal.logger.info(string, **payload)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ActiveSupport::Notifications.subscribe "deliver.action_mailer" do |*args|
|
||||||
|
event = ActiveSupport::Notifications::Event.new(*args)
|
||||||
|
|
||||||
|
Postal.logger.info({
|
||||||
|
event: "send_email",
|
||||||
|
transaction: event.transaction_id,
|
||||||
|
message_id: event.payload[:message_id],
|
||||||
|
subject: event.payload[:subject],
|
||||||
|
from: event.payload[:from],
|
||||||
|
to: event.payload[:to].is_a?(Array) ? event.payload[:to].join(", ") : event.payload[:to].to_s
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "postal/config"
|
require "postal/config"
|
||||||
|
|
||||||
if Postal.config&.smtp
|
if Postal.config&.smtp
|
||||||
ActionMailer::Base.delivery_method = :smtp
|
ActionMailer::Base.delivery_method = :smtp
|
||||||
ActionMailer::Base.smtp_settings = { address: Postal.config.smtp.host, user_name: Postal.config.smtp.username, password: Postal.config.smtp.password, port: Postal.config.smtp.port || 25 }
|
ActionMailer::Base.smtp_settings = { address: Postal.config.smtp.host, user_name: Postal.config.smtp.username, password: Postal.config.smtp.password, port: Postal.config.smtp.port || 25 }
|
||||||
|
|||||||
@@ -51,13 +51,11 @@ rabbitmq:
|
|||||||
tls_ca_certificates: <%= ENV.fetch('RABBITMQ_TLS_CA_CERTIFICATES', '/etc/ssl/certs/ca-certificates.crt'.split(',').inspect) %>
|
tls_ca_certificates: <%= ENV.fetch('RABBITMQ_TLS_CA_CERTIFICATES', '/etc/ssl/certs/ca-certificates.crt'.split(',').inspect) %>
|
||||||
|
|
||||||
logging:
|
logging:
|
||||||
stdout: <%= ENV.fetch('LOGGING_STDOUT', 'false') %>
|
rails_log: <%= ENV.fetch('LOGGING_RAILS_LOG', 'false') %>
|
||||||
root: <%= ENV.fetch('LOGGING_ROOT', '') %>
|
|
||||||
max_log_file_size: <%= ENV.fetch('LOGGING_MAX_LOG_FILES', '20') %>
|
|
||||||
max_log_files: <%= ENV.fetch('LOGGING_MAX_LOG_FILES', '10') %>
|
|
||||||
graylog:
|
graylog:
|
||||||
host: <%= ENV.fetch('GRAYLOG_HOST', '') %>
|
host: <%= ENV.fetch('GRAYLOG_HOST', '') %>
|
||||||
port: <%= ENV.fetch('GRAYLOG_PORT', '12201') %>
|
port: <%= ENV.fetch('GRAYLOG_PORT', '12201') %>
|
||||||
|
facility: <%= ENV.fetch('GRAYLOG_FACILITY', 'postal') %>
|
||||||
|
|
||||||
workers:
|
workers:
|
||||||
threads: <%= ENV.fetch('WORKER_THREADS', '4') %>
|
threads: <%= ENV.fetch('WORKER_THREADS', '4') %>
|
||||||
|
|||||||
@@ -8,9 +8,6 @@ web_server:
|
|||||||
smtp_server:
|
smtp_server:
|
||||||
port: 2525
|
port: 2525
|
||||||
|
|
||||||
logging:
|
|
||||||
stdout: false
|
|
||||||
|
|
||||||
main_db:
|
main_db:
|
||||||
host: mariadb
|
host: mariadb
|
||||||
username: root
|
username: root
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require "logger"
|
|
||||||
|
|
||||||
module Postal
|
|
||||||
|
|
||||||
class AppLogger < Logger
|
|
||||||
|
|
||||||
def initialize(log_name, *args)
|
|
||||||
@log_name = log_name
|
|
||||||
super(*args)
|
|
||||||
self.formatter = LogFormatter.new
|
|
||||||
end
|
|
||||||
|
|
||||||
def add(severity, message = nil, progname = nil)
|
|
||||||
super
|
|
||||||
if severity >= @level && n = self.class.graylog_notifier
|
|
||||||
begin
|
|
||||||
if message.nil?
|
|
||||||
message = block_given? ? yield : progname
|
|
||||||
end
|
|
||||||
message = message.to_s.force_encoding("UTF-8").scrub
|
|
||||||
message_without_ansi = begin
|
|
||||||
message.gsub(/\e\[([\d;]+)?m/, "")
|
|
||||||
rescue StandardError
|
|
||||||
message
|
|
||||||
end
|
|
||||||
n.notify!(short_message: message_without_ansi, log_name: @log_name, facility: "postal", application_name: "postal", process_name: ENV.fetch("PROC_NAME", nil), pid: Process.pid)
|
|
||||||
rescue StandardError
|
|
||||||
# Can't log this to GELF. Soz.
|
|
||||||
end
|
|
||||||
end
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.graylog?
|
|
||||||
!!Postal.config.logging.graylog&.host
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.graylog_notifier
|
|
||||||
@graylog_notifier ||= graylog? ? GELF::Notifier.new(Postal.config.logging.graylog.host, Postal.config.logging.graylog.port) : nil
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
class LogFormatter
|
|
||||||
|
|
||||||
TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%3N"
|
|
||||||
COLORS = [32, 34, 35, 31, 32, 33].freeze
|
|
||||||
|
|
||||||
def call(severity, datetime, progname, msg)
|
|
||||||
time = datetime.strftime(TIME_FORMAT)
|
|
||||||
if number = ENV["PROC_NAME"]
|
|
||||||
id = number.split(".").last.to_i
|
|
||||||
proc_text = "\e[#{COLORS[id % COLORS.size]}m[#{ENV['PROC_NAME']}:#{Process.pid}]\e[0m"
|
|
||||||
else
|
|
||||||
proc_text = "[#{Process.pid}]"
|
|
||||||
end
|
|
||||||
"#{proc_text} [#{time}] #{severity} -- : #{msg}\n"
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
@@ -49,14 +49,6 @@ module Postal
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.log_root
|
|
||||||
if config.logging.root
|
|
||||||
@log_root ||= Pathname.new(config.logging.root)
|
|
||||||
else
|
|
||||||
@log_root ||= app_root.join("log")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.config_file_path
|
def self.config_file_path
|
||||||
if env == "default"
|
if env == "default"
|
||||||
@config_file_path ||= File.join(config_root, "postal.yml")
|
@config_file_path ||= File.join(config_root, "postal.yml")
|
||||||
@@ -93,16 +85,11 @@ module Postal
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.logger_for(name)
|
def self.logger
|
||||||
@loggers ||= {}
|
@logger ||= begin
|
||||||
@loggers[name.to_sym] ||= begin
|
k = Klogger.new(nil, destination: Rails.env.test? ? "/dev/null" : $stdout, highlight: Rails.env.development?)
|
||||||
require "postal/app_logger"
|
k.add_destination(graylog_logging_destination) if config.logging&.graylog&.host.present?
|
||||||
if config.logging.stdout || ENV["LOG_TO_STDOUT"]
|
k
|
||||||
Postal::AppLogger.new(name, $stdout)
|
|
||||||
else
|
|
||||||
FileUtils.mkdir_p(log_root)
|
|
||||||
Postal::AppLogger.new(name, log_root.join("#{name}.log"), config.logging.max_log_files, config.logging.max_log_file_size.megabytes)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -187,4 +174,19 @@ module Postal
|
|||||||
config.general.use_ip_pools?
|
config.general.use_ip_pools?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.graylog_logging_destination
|
||||||
|
@graylog_destination ||= begin
|
||||||
|
notifier = GELF::Notifier.new(config.logging.graylog.host, config.logging.graylog.port, "WAN")
|
||||||
|
proc do |_logger, payload, group_ids|
|
||||||
|
short_message = payload.delete(:message) || "[message missing]"
|
||||||
|
notifier.notify!(short_message: short_message, **{
|
||||||
|
facility: config.logging.graylog.facility,
|
||||||
|
_environment: Rails.env.to_s,
|
||||||
|
_version: Postal::VERSION.to_s,
|
||||||
|
_group_ids: group_ids.join(" ")
|
||||||
|
}.merge(payload.transform_keys { |k| "_#{k}".to_sym }.transform_values(&:to_s)))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ module Postal
|
|||||||
private
|
private
|
||||||
|
|
||||||
def log(text)
|
def log(text)
|
||||||
Postal.logger_for(:http_sender).info("[#{@log_id}] #{text}")
|
Postal.logger.info text, id: @log_id, component: "http-sender"
|
||||||
end
|
end
|
||||||
|
|
||||||
def parameters(message, options = {})
|
def parameters(message, options = {})
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ module Postal
|
|||||||
end
|
end
|
||||||
|
|
||||||
def log(text)
|
def log(text)
|
||||||
Worker.logger.info "[#{@id}] #{text}"
|
Worker.logger.info(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.queue(queue, params = {})
|
def self.queue(queue, params = {})
|
||||||
|
|||||||
@@ -325,12 +325,12 @@ module Postal
|
|||||||
result = connection.query(query, cast_booleans: true)
|
result = connection.query(query, cast_booleans: true)
|
||||||
time = Time.now.to_f - start_time
|
time = Time.now.to_f - start_time
|
||||||
logger.debug " \e[4;34mMessageDB Query (#{time.round(2)}s) \e[0m \e[33m#{query}\e[0m"
|
logger.debug " \e[4;34mMessageDB Query (#{time.round(2)}s) \e[0m \e[33m#{query}\e[0m"
|
||||||
if time > 0.5 && query =~ /\A(SELECT|UPDATE|DELETE) /
|
if time.positive? && query =~ /\A(SELECT|UPDATE|DELETE) /
|
||||||
id = Nifty::Utils::RandomString.generate(length: 6).upcase
|
id = Nifty::Utils::RandomString.generate(length: 6).upcase
|
||||||
explain_result = ResultForExplainPrinter.new(connection.query("EXPLAIN #{query}"))
|
explain_result = ResultForExplainPrinter.new(connection.query("EXPLAIN #{query}"))
|
||||||
slow_query_logger.info "[#{id}] EXPLAIN #{query}"
|
logger.info " [#{id}] EXPLAIN #{query}"
|
||||||
ActiveRecord::ConnectionAdapters::MySQL::ExplainPrettyPrinter.new.pp(explain_result, time).split("\n").each do |line|
|
ActiveRecord::ConnectionAdapters::MySQL::ExplainPrettyPrinter.new.pp(explain_result, time).split("\n").each do |line|
|
||||||
slow_query_logger.info "[#{id}] " + line
|
logger.info " [#{id}] " + line
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
result
|
result
|
||||||
@@ -340,10 +340,6 @@ module Postal
|
|||||||
defined?(Rails) ? Rails.logger : Logger.new($stdout)
|
defined?(Rails) ? Rails.logger : Logger.new($stdout)
|
||||||
end
|
end
|
||||||
|
|
||||||
def slow_query_logger
|
|
||||||
Postal.logger_for(:slow_message_db_queries)
|
|
||||||
end
|
|
||||||
|
|
||||||
def with_mysql(&block)
|
def with_mysql(&block)
|
||||||
self.class.connection_pool.use(&block)
|
self.class.connection_pool.use(&block)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ module Postal
|
|||||||
private
|
private
|
||||||
|
|
||||||
def logger
|
def logger
|
||||||
Postal.logger_for(:message_inspection)
|
Postal.logger
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ module Postal
|
|||||||
private
|
private
|
||||||
|
|
||||||
def log(text)
|
def log(text)
|
||||||
Postal.logger_for(:message_requeuer).info text
|
Postal.logger.info text, component: "message-requeuer"
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_exit
|
def check_exit
|
||||||
|
|||||||
@@ -234,7 +234,7 @@ module Postal
|
|||||||
end
|
end
|
||||||
|
|
||||||
def log(text)
|
def log(text)
|
||||||
Postal.logger_for(:smtp_sender).info "[#{@log_id}] #{text}"
|
Postal.logger.info text, id: @log_id, component: "smtp-sender"
|
||||||
end
|
end
|
||||||
|
|
||||||
def destination_host_description
|
def destination_host_description
|
||||||
|
|||||||
@@ -48,16 +48,18 @@ module Postal
|
|||||||
end
|
end
|
||||||
|
|
||||||
def handle(data)
|
def handle(data)
|
||||||
if @state == :preauth
|
Postal.logger.tagged(id: id) do
|
||||||
return proxy(data)
|
if @state == :preauth
|
||||||
end
|
return proxy(data)
|
||||||
|
end
|
||||||
|
|
||||||
log "\e[32m<= #{sanitize_input_for_log(data.strip)}\e[0m"
|
log "\e[32m<= #{sanitize_input_for_log(data.strip)}\e[0m"
|
||||||
if @proc
|
if @proc
|
||||||
@proc.call(data)
|
@proc.call(data)
|
||||||
|
|
||||||
else
|
else
|
||||||
handle_command(data)
|
handle_command(data)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -93,7 +95,7 @@ module Postal
|
|||||||
def log(text)
|
def log(text)
|
||||||
return false unless @logging_enabled
|
return false unless @logging_enabled
|
||||||
|
|
||||||
Postal.logger_for(:smtp_server).debug "[#{id}] #{text}"
|
Postal.logger.debug(text, id: id)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -14,8 +14,10 @@ module Postal
|
|||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
listen
|
logger.tagged(component: "smtp-server") do
|
||||||
run_event_loop
|
listen
|
||||||
|
run_event_loop
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
@@ -264,7 +266,7 @@ module Postal
|
|||||||
end
|
end
|
||||||
|
|
||||||
def logger
|
def logger
|
||||||
Postal.logger_for(:smtp_server)
|
Postal.logger
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -49,33 +49,25 @@ module Postal
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def receive_job(delivery_info, properties, body)
|
def receive_job(delivery_info, properties, message)
|
||||||
message = begin
|
|
||||||
JSON.parse(body)
|
|
||||||
rescue StandardError
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
if message && message["class_name"]
|
if message && message["class_name"]
|
||||||
@running_jobs << message["id"]
|
@running_jobs << message["id"]
|
||||||
set_process_name
|
set_process_name
|
||||||
start_time = Time.now
|
start_time = Time.now
|
||||||
Thread.current[:job_id] = message["id"]
|
Thread.current[:job_id] = message["id"]
|
||||||
logger.info "[#{message['id']}] Started processing \e[34m#{message['class_name']}\e[0m job"
|
logger.info "Processing job"
|
||||||
begin
|
begin
|
||||||
klass = message["class_name"].constantize.new(message["id"], message["params"])
|
klass = message["class_name"].constantize.new(message["id"], message["params"])
|
||||||
klass.perform
|
klass.perform
|
||||||
GC.start
|
GC.start
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
klass.on_error(e) if defined?(klass)
|
klass.on_error(e) if defined?(klass)
|
||||||
logger.warn "[#{message['id']}] \e[31m#{e.class}: #{e.message}\e[0m"
|
logger.exception(e)
|
||||||
e.backtrace.each do |line|
|
|
||||||
logger.warn "[#{message['id']}] " + line
|
|
||||||
end
|
|
||||||
if defined?(Sentry)
|
if defined?(Sentry)
|
||||||
Sentry.capture_exception(e, extra: { job_id: message["id"] })
|
Sentry.capture_exception(e, extra: { job_id: message["id"] })
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
logger.info "[#{message['id']}] Finished processing \e[34m#{message['class_name']}\e[0m job in #{Time.now - start_time}s"
|
logger.info "Finished job", time: (Time.now - start_time).to_i
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
@@ -92,13 +84,21 @@ module Postal
|
|||||||
|
|
||||||
def join_queue(queue)
|
def join_queue(queue)
|
||||||
if @active_queues[queue]
|
if @active_queues[queue]
|
||||||
logger.info "Attempted to join queue #{queue} but already joined."
|
logger.error "attempted to join queue but already joined", queue: queue
|
||||||
else
|
else
|
||||||
consumer = self.class.job_queue(queue).subscribe(manual_ack: true) do |delivery_info, properties, body|
|
consumer = self.class.job_queue(queue).subscribe(manual_ack: true) do |delivery_info, properties, body|
|
||||||
receive_job(delivery_info, properties, body)
|
message = begin
|
||||||
|
JSON.parse(body)
|
||||||
|
rescue StandardError
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
logger.tagged(job_id: message["id"], queue: queue, job_class: message["class_name"]) do
|
||||||
|
receive_job(delivery_info, properties, message)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
@active_queues[queue] = consumer
|
@active_queues[queue] = consumer
|
||||||
logger.info "Joined \e[32m#{queue}\e[0m queue"
|
logger.info "joined queue", queue: queue
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -106,9 +106,9 @@ module Postal
|
|||||||
if consumer = @active_queues[queue]
|
if consumer = @active_queues[queue]
|
||||||
consumer.cancel
|
consumer.cancel
|
||||||
@active_queues.delete(queue)
|
@active_queues.delete(queue)
|
||||||
logger.info "Left \e[32m#{queue}\e[0m queue"
|
logger.info "left queue", queue: queue
|
||||||
else
|
else
|
||||||
logger.info "Not joined #{queue} so cannot leave"
|
logger.error "requested to leave queue, but not joined", queue: queue
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -198,7 +198,7 @@ module Postal
|
|||||||
class << self
|
class << self
|
||||||
|
|
||||||
def logger
|
def logger
|
||||||
Postal.logger_for(:worker)
|
Postal.logger
|
||||||
end
|
end
|
||||||
|
|
||||||
def job_channel
|
def job_channel
|
||||||
|
|||||||
المرجع في مشكلة جديدة
حظر مستخدم