mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 10:50:26 -06:00
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)
|
||||
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")
|
||||
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"]
|
||||
Rails.logger.send(:warn, "#{level[0].upcase}, [#{now}] [IMAP] (group_id #{group_id}) #{message}")
|
||||
else
|
||||
Rails.logger.send(level, "#{level[0].upcase}, [#{now}] [IMAP] (group_id #{group_id}) #{message}")
|
||||
end
|
||||
|
||||
new_log
|
||||
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
|
||||
log(message, :debug, group_id)
|
||||
log(message, :debug, group_id, db)
|
||||
end
|
||||
|
||||
def self.info(message, group_or_id)
|
||||
|
@ -29,6 +29,8 @@ class IncomingEmail < ActiveRecord::Base
|
||||
SQL
|
||||
end
|
||||
|
||||
scope :without_raw, -> { select(self.column_names - ["raw"]) }
|
||||
|
||||
def self.created_via_types
|
||||
@types ||= Enum.new(
|
||||
handle_mail: 1,
|
||||
@ -38,6 +40,18 @@ class IncomingEmail < ActiveRecord::Base
|
||||
)
|
||||
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
|
||||
self.to_addresses&.split(";") || []
|
||||
end
|
||||
|
@ -166,6 +166,10 @@ class Post < ActiveRecord::Base
|
||||
includes(:post_details).find_by(post_details: { key: key, value: value })
|
||||
end
|
||||
|
||||
def self.find_by_number(topic_id, post_number)
|
||||
find_by(topic_id: topic_id, post_number: post_number)
|
||||
end
|
||||
|
||||
def whisper?
|
||||
post_type == Post.types[:whisper]
|
||||
end
|
||||
|
@ -23,7 +23,7 @@ class Demon::EmailSync < ::Demon::Base
|
||||
def start_thread(db, group)
|
||||
Thread.new 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
|
||||
syncer = Imap::Sync.new(group)
|
||||
rescue Net::IMAP::NoResponseError => e
|
||||
@ -44,7 +44,7 @@ class Demon::EmailSync < ::Demon::Base
|
||||
)
|
||||
|
||||
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
|
||||
# connection back to the pool.
|
||||
@ -133,7 +133,7 @@ class Demon::EmailSync < ::Demon::Base
|
||||
# Spawn new threads for groups that are now synchronized.
|
||||
groups.each do |group_id, group|
|
||||
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] = {
|
||||
thread: start_thread(db, group),
|
||||
|
@ -60,7 +60,7 @@ module Imap
|
||||
ActiveRecord::Base.connection_handler.clear_active_connections!
|
||||
|
||||
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|
|
||||
if resp.kind_of?(Net::IMAP::UntaggedResponse) && resp.name == 'EXISTS'
|
||||
|
@ -17,4 +17,9 @@ describe Jobs::CleanupImapSyncLog do
|
||||
|
||||
expect(ImapSyncLog.count).to eq(1)
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user