cache notifications in memory
test plan: * use canvas and ensure that notifications work Change-Id: Ie46f049aab5dde7167c3ea4228e503c7a686c839 Reviewed-on: https://gerrit.instructure.com/9154 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Cody Cutrer <cody@instructure.com>
This commit is contained in:
parent
0f8f0a9340
commit
f63077aa7f
|
@ -255,7 +255,7 @@ class Alert < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.send_alert(alert, user_ids, student_enrollment)
|
def self.send_alert(alert, user_ids, student_enrollment)
|
||||||
notification = Notification.find_by_name("Alert")
|
notification = Notification.by_name("Alert")
|
||||||
notification.create_message(alert, user_ids, {:asset_context => student_enrollment})
|
notification.create_message(alert, user_ids, {:asset_context => student_enrollment})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -112,7 +112,7 @@ class DelayedMessage < ActiveRecord::Base
|
||||||
user = to.user rescue nil
|
user = to.user rescue nil
|
||||||
context = delayed_messages.select{|m| m.context}.compact.first.try(:context)
|
context = delayed_messages.select{|m| m.context}.compact.first.try(:context)
|
||||||
return nil unless context # the context for this message has already been deleted
|
return nil unless context # the context for this message has already been deleted
|
||||||
notification = Notification.find_by_name('Summaries')
|
notification = Notification.by_name('Summaries')
|
||||||
path = HostUrl.outgoing_email_address
|
path = HostUrl.outgoing_email_address
|
||||||
message = notification.messages.build(
|
message = notification.messages.build(
|
||||||
:subject => notification.subject,
|
:subject => notification.subject,
|
||||||
|
|
|
@ -74,7 +74,21 @@ class Notification < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.summary_notification
|
def self.summary_notification
|
||||||
find_by_name('Summaries')
|
by_name('Summaries')
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.by_name(name)
|
||||||
|
@notifications ||= Notification.all.inject({}){ |h, n| h[n.name] = n; h }
|
||||||
|
if notification = @notifications[name]
|
||||||
|
copy = notification.clone
|
||||||
|
copy.id = notification.id
|
||||||
|
copy.send(:remove_instance_variable, :@new_record)
|
||||||
|
copy
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.reset_cache!
|
||||||
|
@notifications = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def infer_default_content
|
def infer_default_content
|
||||||
|
|
|
@ -60,8 +60,8 @@ module Canvas::AccountReports
|
||||||
def self.message_recipient(account_report, message, csv=nil)
|
def self.message_recipient(account_report, message, csv=nil)
|
||||||
user = account_report.user
|
user = account_report.user
|
||||||
account = account_report.account
|
account = account_report.account
|
||||||
notification = Notification.find_by_name("Report Generated")
|
notification = Notification.by_name("Report Generated")
|
||||||
notification = Notification.find_by_name("Report Generation Failed") if !csv
|
notification = Notification.by_name("Report Generation Failed") if !csv
|
||||||
attachment = nil
|
attachment = nil
|
||||||
if csv
|
if csv
|
||||||
require 'action_controller'
|
require 'action_controller'
|
||||||
|
|
|
@ -44,6 +44,26 @@ describe Notification do
|
||||||
n.subject.should_not be_nil
|
n.subject.should_not be_nil
|
||||||
n.sms_body.should_not be_nil
|
n.sms_body.should_not be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "by_name" do
|
||||||
|
before do
|
||||||
|
Notification.create(:name => "foo")
|
||||||
|
Notification.create(:name => "bar")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should look up all notifications once and cache them thereafter" do
|
||||||
|
Notification.expects(:all).once.returns{ Notification.find(:all) }
|
||||||
|
Notification.by_name("foo").should eql(Notification.find_by_name("foo"))
|
||||||
|
Notification.by_name("bar").should eql(Notification.find_by_name("bar"))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should give you different object for the same notification" do
|
||||||
|
n1 = Notification.by_name("foo")
|
||||||
|
n2 = Notification.by_name("foo")
|
||||||
|
n1.should eql n2
|
||||||
|
n1.should_not equal n2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "create_message" do
|
context "create_message" do
|
||||||
it "should only send dashboard messages for users with non-validated channels" do
|
it "should only send dashboard messages for users with non-validated channels" do
|
||||||
|
|
|
@ -97,6 +97,7 @@ Spec::Runner.configure do |config|
|
||||||
config.before :all do
|
config.before :all do
|
||||||
# so before(:all)'s don't get confused
|
# so before(:all)'s don't get confused
|
||||||
Account.clear_special_account_cache!
|
Account.clear_special_account_cache!
|
||||||
|
Notification.after_create { Notification.reset_cache! }
|
||||||
end
|
end
|
||||||
|
|
||||||
config.before :each do
|
config.before :each do
|
||||||
|
@ -105,6 +106,7 @@ Spec::Runner.configure do |config|
|
||||||
Account.default.update_attribute(:default_time_zone, 'UTC')
|
Account.default.update_attribute(:default_time_zone, 'UTC')
|
||||||
Setting.reset_cache!
|
Setting.reset_cache!
|
||||||
HostUrl.reset_cache!
|
HostUrl.reset_cache!
|
||||||
|
Notification.reset_cache!
|
||||||
ActiveRecord::Base.reset_any_instantiation!
|
ActiveRecord::Base.reset_any_instantiation!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -126,8 +126,7 @@ module Instructure #:nodoc:
|
||||||
record.messages_failed[self.dispatch] = "Did not meet condition."
|
record.messages_failed[self.dispatch] = "Did not meet condition."
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
notification = record.notifications.find_by_name(self.dispatch) rescue nil
|
notification = Notification.by_name(self.dispatch)
|
||||||
notification ||= Notification.find_by_name(self.dispatch)
|
|
||||||
# logger.warn "Could not find notification for #{record.inspect}" unless notification
|
# logger.warn "Could not find notification for #{record.inspect}" unless notification
|
||||||
unless notification
|
unless notification
|
||||||
record.messages_failed[self.dispatch] = "Could not find notification: #{self.dispatch}."
|
record.messages_failed[self.dispatch] = "Could not find notification: #{self.dispatch}."
|
||||||
|
|
Loading…
Reference in New Issue