DEV: IMAP debugging improvements (#11784)
Improvements to make console access to IncomingEmail more pleasant, and stopping certain IMAP logs from landing in the DB because they just create too much noise,
This commit is contained in:
parent
b902de1727
commit
f34fa999a2
|
@ -9,20 +9,25 @@ class ImapSyncLog < ActiveRecord::Base
|
||||||
@levels ||= Enum.new(:debug, :info, :warn, :error)
|
@levels ||= Enum.new(:debug, :info, :warn, :error)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.log(message, level, group_id = nil)
|
def self.log(message, level, group_id = nil, db = true)
|
||||||
now = Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")
|
now = Time.now.strftime("%Y-%m-%d %H:%M:%S.%L")
|
||||||
new_log = create(message: message, level: ImapSyncLog.levels[level], group_id: group_id)
|
|
||||||
|
new_log = if db
|
||||||
|
create(message: message, level: ImapSyncLog.levels[level], group_id: group_id)
|
||||||
|
end
|
||||||
|
|
||||||
if ENV["DEBUG_IMAP"]
|
if ENV["DEBUG_IMAP"]
|
||||||
Rails.logger.send(:warn, "#{level[0].upcase}, [#{now}] [IMAP] (group_id #{group_id}) #{message}")
|
Rails.logger.send(:warn, "#{level[0].upcase}, [#{now}] [IMAP] (group_id #{group_id}) #{message}")
|
||||||
else
|
else
|
||||||
Rails.logger.send(level, "#{level[0].upcase}, [#{now}] [IMAP] (group_id #{group_id}) #{message}")
|
Rails.logger.send(level, "#{level[0].upcase}, [#{now}] [IMAP] (group_id #{group_id}) #{message}")
|
||||||
end
|
end
|
||||||
|
|
||||||
new_log
|
new_log
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.debug(message, group_or_id)
|
def self.debug(message, group_or_id, db: true)
|
||||||
group_id = group_or_id.is_a?(Integer) ? group_or_id : group_or_id.id
|
group_id = group_or_id.is_a?(Integer) ? group_or_id : group_or_id.id
|
||||||
log(message, :debug, group_id)
|
log(message, :debug, group_id, db)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.info(message, group_or_id)
|
def self.info(message, group_or_id)
|
||||||
|
|
|
@ -29,6 +29,8 @@ class IncomingEmail < ActiveRecord::Base
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
|
scope :without_raw, -> { select(self.column_names - ["raw"]) }
|
||||||
|
|
||||||
def self.created_via_types
|
def self.created_via_types
|
||||||
@types ||= Enum.new(
|
@types ||= Enum.new(
|
||||||
handle_mail: 1,
|
handle_mail: 1,
|
||||||
|
@ -38,6 +40,18 @@ class IncomingEmail < ActiveRecord::Base
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def as_mail_message
|
||||||
|
@mail_message ||= Mail.new(self.raw)
|
||||||
|
end
|
||||||
|
|
||||||
|
def raw_headers
|
||||||
|
as_mail_message.header.raw_source
|
||||||
|
end
|
||||||
|
|
||||||
|
def raw_body
|
||||||
|
as_mail_message.body
|
||||||
|
end
|
||||||
|
|
||||||
def to_addresses_split
|
def to_addresses_split
|
||||||
self.to_addresses&.split(";") || []
|
self.to_addresses&.split(";") || []
|
||||||
end
|
end
|
||||||
|
|
|
@ -166,6 +166,10 @@ class Post < ActiveRecord::Base
|
||||||
includes(:post_details).find_by(post_details: { key: key, value: value })
|
includes(:post_details).find_by(post_details: { key: key, value: value })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.find_by_number(topic_id, post_number)
|
||||||
|
find_by(topic_id: topic_id, post_number: post_number)
|
||||||
|
end
|
||||||
|
|
||||||
def whisper?
|
def whisper?
|
||||||
post_type == Post.types[:whisper]
|
post_type == Post.types[:whisper]
|
||||||
end
|
end
|
||||||
|
|
|
@ -23,7 +23,7 @@ class Demon::EmailSync < ::Demon::Base
|
||||||
def start_thread(db, group)
|
def start_thread(db, group)
|
||||||
Thread.new do
|
Thread.new do
|
||||||
RailsMultisite::ConnectionManagement.with_connection(db) do
|
RailsMultisite::ConnectionManagement.with_connection(db) do
|
||||||
ImapSyncLog.debug("Thread started for group #{group.name} in db #{db}", group)
|
ImapSyncLog.debug("Thread started for group #{group.name} in db #{db}", group, db: false)
|
||||||
begin
|
begin
|
||||||
syncer = Imap::Sync.new(group)
|
syncer = Imap::Sync.new(group)
|
||||||
rescue Net::IMAP::NoResponseError => e
|
rescue Net::IMAP::NoResponseError => e
|
||||||
|
@ -44,7 +44,7 @@ class Demon::EmailSync < ::Demon::Base
|
||||||
)
|
)
|
||||||
|
|
||||||
if !syncer.can_idle? && status[:remaining] == 0
|
if !syncer.can_idle? && status[:remaining] == 0
|
||||||
ImapSyncLog.debug("Going to sleep for group #{group.name} in db #{db} to wait for new emails", group)
|
ImapSyncLog.debug("Going to sleep for group #{group.name} in db #{db} to wait for new emails", group, db: false)
|
||||||
|
|
||||||
# Thread goes into sleep for a bit so it is better to return any
|
# Thread goes into sleep for a bit so it is better to return any
|
||||||
# connection back to the pool.
|
# connection back to the pool.
|
||||||
|
@ -133,7 +133,7 @@ class Demon::EmailSync < ::Demon::Base
|
||||||
# Spawn new threads for groups that are now synchronized.
|
# Spawn new threads for groups that are now synchronized.
|
||||||
groups.each do |group_id, group|
|
groups.each do |group_id, group|
|
||||||
if !@sync_data[db][group_id]
|
if !@sync_data[db][group_id]
|
||||||
ImapSyncLog.debug("Starting thread for group #{group.name} mailbox #{group.imap_mailbox_name}", group)
|
ImapSyncLog.debug("Starting thread for group #{group.name} mailbox #{group.imap_mailbox_name}", group, db: false)
|
||||||
|
|
||||||
@sync_data[db][group_id] = {
|
@sync_data[db][group_id] = {
|
||||||
thread: start_thread(db, group),
|
thread: start_thread(db, group),
|
||||||
|
|
|
@ -60,7 +60,7 @@ module Imap
|
||||||
ActiveRecord::Base.connection_handler.clear_active_connections!
|
ActiveRecord::Base.connection_handler.clear_active_connections!
|
||||||
|
|
||||||
idle_polling_mins = SiteSetting.imap_polling_period_mins.minutes.to_i
|
idle_polling_mins = SiteSetting.imap_polling_period_mins.minutes.to_i
|
||||||
ImapSyncLog.debug("Going IDLE for #{idle_polling_mins} seconds to wait for more work", @group)
|
ImapSyncLog.debug("Going IDLE for #{idle_polling_mins} seconds to wait for more work", @group, db: false)
|
||||||
|
|
||||||
@provider.imap.idle(idle_polling_mins) do |resp|
|
@provider.imap.idle(idle_polling_mins) do |resp|
|
||||||
if resp.kind_of?(Net::IMAP::UntaggedResponse) && resp.name == 'EXISTS'
|
if resp.kind_of?(Net::IMAP::UntaggedResponse) && resp.name == 'EXISTS'
|
||||||
|
|
|
@ -17,4 +17,9 @@ describe Jobs::CleanupImapSyncLog do
|
||||||
|
|
||||||
expect(ImapSyncLog.count).to eq(1)
|
expect(ImapSyncLog.count).to eq(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "does not write the log to the db if specified" do
|
||||||
|
ImapSyncLog.debug("test", Fabricate(:group), db: false)
|
||||||
|
expect(ImapSyncLog.count).to eq(0)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue