Don't send MQTT throttle emails more than once per day.
parent
7dd178e2d8
commit
3252e8cc7e
|
@ -201,8 +201,13 @@ class Device < ApplicationRecord
|
|||
"see https://developer.farm.bot/docs/connectivity-issues"
|
||||
def self.connection_warning(username)
|
||||
device_id = username.split("_").last.to_i || 0
|
||||
self
|
||||
.find(device_id)
|
||||
.tell(TOO_MANY_CONNECTIONS, ["fatal_email"]) if self.exists?(device_id)
|
||||
device = self.find_by(id: device_id)
|
||||
return unless device
|
||||
|
||||
last_sent_at = device.mqtt_rate_limit_email_sent_at || 4.years.ago
|
||||
if last_sent_at < 1.day.ago
|
||||
device.update_attributes!(mqtt_rate_limit_email_sent_at: Time.now)
|
||||
device.tell(TOO_MANY_CONNECTIONS, ["fatal_email"])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
class AddMqttThrottleEmailSentAtToDevice < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_column :devices,
|
||||
:mqtt_rate_limit_email_sent_at,
|
||||
:datetime
|
||||
end
|
||||
end
|
|
@ -206,7 +206,8 @@ CREATE TABLE public.devices (
|
|||
mounted_tool_id bigint,
|
||||
created_at timestamp without time zone,
|
||||
updated_at timestamp without time zone,
|
||||
serial_number character varying(32)
|
||||
serial_number character varying(32),
|
||||
mqtt_rate_limit_email_sent_at timestamp without time zone
|
||||
);
|
||||
|
||||
|
||||
|
@ -3019,6 +3020,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||
('20190621160042'),
|
||||
('20190621202204'),
|
||||
('20190701155706'),
|
||||
('20190709194037');
|
||||
('20190709194037'),
|
||||
('20190715214412');
|
||||
|
||||
|
||||
|
|
|
@ -102,4 +102,11 @@ describe Device do
|
|||
expect(results).to_not include(🚑)
|
||||
expect(results).to_not include(🍞)
|
||||
end
|
||||
|
||||
it "throttled emails about MQTT rate limiting" do
|
||||
device.update_attributes!(mqtt_rate_limit_email_sent_at: 2.days.ago)
|
||||
Device.connection_warning("device_#{device.id.to_s}")
|
||||
time = device.reload.mqtt_rate_limit_email_sent_at
|
||||
expect(time).to be > 1.minute.ago
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue