DEV: Review fixes (#10641)

See comments in https://review.discourse.org/t/dev-imap-log-to-database-10435/14337/6 for context.
This commit is contained in:
Martin Brennan 2020-09-10 13:41:46 +10:00 committed by GitHub
parent e6ca1b4326
commit 7f2f87bf59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 19 deletions

View File

@ -1,17 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
class ImapSyncLog < ActiveRecord::Base class ImapSyncLog < ActiveRecord::Base
RETAIN_LOGS_DAYS = 5 RETAIN_LOGS_DAYS ||= 5
belongs_to :group belongs_to :group
def self.levels def self.levels
@levels ||= Enum.new( @levels ||= Enum.new(:debug, :info, :warn, :error)
debug: 1,
info: 2,
warn: 3,
error: 4
)
end end
def self.log(message, level, group_id = nil) def self.log(message, level, group_id = nil)

View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
class MakeImapSyncLogColsNotNull < ActiveRecord::Migration[6.0]
def change
change_column_null :imap_sync_logs, :message, false
change_column_null :imap_sync_logs, :level, false
end
end

View File

@ -68,9 +68,7 @@ class Demon::EmailSync < ::Demon::Base
@sync_data.each do |db, sync_data| @sync_data.each do |db, sync_data|
sync_data.each do |_, data| sync_data.each do |_, data|
data[:thread].kill kill_and_disconnect!(data)
data[:thread].join
data[:syncer]&.disconnect! rescue nil
end end
end end
@ -104,9 +102,7 @@ class Demon::EmailSync < ::Demon::Base
next true if all_dbs.include?(db) next true if all_dbs.include?(db)
sync_data.each do |_, data| sync_data.each do |_, data|
data[:thread].kill kill_and_disconnect!(data)
data[:thread].join
data[:syncer]&.disconnect!
end end
false false
@ -130,10 +126,7 @@ class Demon::EmailSync < ::Demon::Base
ImapSyncLog.warn("Thread for group is dead", group_id) ImapSyncLog.warn("Thread for group is dead", group_id)
end end
data[:thread].kill kill_and_disconnect!(data)
data[:thread].join
data[:syncer]&.disconnect!
false false
end end
@ -166,4 +159,14 @@ class Demon::EmailSync < ::Demon::Base
STDERR.puts e.backtrace.join("\n") STDERR.puts e.backtrace.join("\n")
exit 1 exit 1
end end
def kill_and_disconnect!(data)
data[:thread].kill
data[:thread].join
begin
data[:syncer]&.disconnect!
rescue Net::IMAP::ResponseError => err
puts "[EmailSync] Encountered a response error when disconnecting: #{err.to_s}"
end
end
end end

View File

@ -10,8 +10,8 @@ describe Jobs::CleanupImapSyncLog do
log2 = ImapSyncLog.log("Test log 2", :debug) log2 = ImapSyncLog.log("Test log 2", :debug)
log3 = ImapSyncLog.log("Test log 3", :debug) log3 = ImapSyncLog.log("Test log 3", :debug)
log2.update(created_at: Time.now - 6.days) log2.update(created_at: 6.days.ago)
log3.update(created_at: Time.now - 7.days) log3.update(created_at: 7.days.ago)
job_class.execute({}) job_class.execute({})