Double send e-stop email fix

pull/864/head
Rick Carlino 2018-05-24 12:16:16 -05:00
parent 8487f92301
commit d35b0c0e4c
6 changed files with 28 additions and 10 deletions

View File

@ -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

View File

@ -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"

View File

@ -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")

View File

@ -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

View File

@ -0,0 +1,5 @@
class AddSentAtToLog < ActiveRecord::Migration[5.2]
def change
add_column :logs, :sent_at, :datetime
end
end

View File

@ -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"