From d35b0c0e4c22620fe99dc3ebd68979a1f2e504b9 Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Thu, 24 May 2018 12:16:16 -0500 Subject: [PATCH] Double send e-stop email fix --- app/mailers/fatal_error_mailer.rb | 10 +++++++++- app/mailers/log_delivery_mailer.rb | 5 ++++- app/models/device.rb | 14 +++++++------- app/models/log.rb | 1 + db/migrate/20180524161501_add_sent_at_to_log.rb | 5 +++++ db/schema.rb | 3 ++- 6 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20180524161501_add_sent_at_to_log.rb diff --git a/app/mailers/fatal_error_mailer.rb b/app/mailers/fatal_error_mailer.rb index f17108767..93746685e 100644 --- a/app/mailers/fatal_error_mailer.rb +++ b/app/mailers/fatal_error_mailer.rb @@ -1,8 +1,16 @@ class FatalErrorMailer < ApplicationMailer def fatal_error(device, log) @emails = device.users.pluck(:email) - @message = log.message + @logs = device + .logs + .where(Log::IS_FATAL_EMAIL) + .where(sent_at: nil) + return if @logs.empty? + @message = @logs + .pluck(:message) + .join("\n\n") @device_name = device.name || "Farmbot" mail(to: @emails, subject: "🚨 New error reported by #{@device_name}!") + @logs.update_all(sent_at: Time.now) end end diff --git a/app/mailers/log_delivery_mailer.rb b/app/mailers/log_delivery_mailer.rb index 9ffa64495..4e220c24b 100644 --- a/app/mailers/log_delivery_mailer.rb +++ b/app/mailers/log_delivery_mailer.rb @@ -8,7 +8,10 @@ class LogDeliveryMailer < ApplicationMailer raise LogDispatch::RateLimitError, WHOAH % [device.id] if too_many ld = LogDispatch.where(sent_at: nil, device: device) if(ld.any?) - logs = Log.find(ld.pluck(:log_id)) + logs = Log + .where(id: ld.pluck(:log_id)) + .where + .not(Log::IS_FATAL_EMAIL) @emails = device.users.pluck(:email) @messages = logs.map(&:message) @device_name = device.name || "Farmbot" diff --git a/app/models/device.rb b/app/models/device.rb index a702d9d69..54a41e584 100644 --- a/app/models/device.rb +++ b/app/models/device.rb @@ -123,13 +123,13 @@ class Device < ApplicationRecord # Send a realtime message to a logged in user. def tell(message, channels = [], type = "info") log = Log.new({ device: self, - message: message, - created_at: Time.now, - channels: channels, - major_version: 99, - minor_version: 99, - meta: {}, - type: type }) + message: message, + created_at: Time.now, + channels: channels, + major_version: 99, + minor_version: 99, + meta: {}, + type: type }) json = LogSerializer.new(log).as_json.to_json Transport.current.amqp_send(json, self.id, "logs") diff --git a/app/models/log.rb b/app/models/log.rb index a3920c200..9c8c232a5 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -13,6 +13,7 @@ class Log < ApplicationRecord DISCARD = ["fun", "debug", nil] # self.meta[:type] is used by the bot and the frontend as a sort of TYPES = CeleryScriptSettingsBag::ALLOWED_MESSAGE_TYPES + IS_FATAL_EMAIL = "channels LIKE '%fatal_email%'" # The means by which the message will be sent. Ex: frontend toast notification serialize :channels belongs_to :device diff --git a/db/migrate/20180524161501_add_sent_at_to_log.rb b/db/migrate/20180524161501_add_sent_at_to_log.rb new file mode 100644 index 000000000..dc3738d0a --- /dev/null +++ b/db/migrate/20180524161501_add_sent_at_to_log.rb @@ -0,0 +1,5 @@ +class AddSentAtToLog < ActiveRecord::Migration[5.2] + def change + add_column :logs, :sent_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 3432d4582..aab71fb7b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_05_21_195953) do +ActiveRecord::Schema.define(version: 2018_05_24_161501) do # These are extensions that must be enabled in order to support this database enable_extension "hstore" @@ -252,6 +252,7 @@ ActiveRecord::Schema.define(version: 2018_05_21_195953) do t.integer "x" t.integer "y" t.integer "z" + t.datetime "sent_at" t.index ["created_at"], name: "index_logs_on_created_at" t.index ["device_id"], name: "index_logs_on_device_id" t.index ["type"], name: "index_logs_on_type"