0

I want to send exception mail to email_ids stored in yb_notifier_email_ids table. Previously, email_id was hardcoded in production.rb file. I want to send mail by using model. But as configurations file are loaded before active_records I am not able to do this.

-----------migration

        class CreateYbNotifierEmailIds < ActiveRecord::Migration
          def change
            create_table :yb_notifier_email_ids do |t|
              t.string :email_id

              t.timestamps null: false
            end
          end
        end

----------------------------required------------

        config.middleware.use ExceptionNotification::Rack,
            :email => {
              :email_prefix => "[YB] ",
              :sender_address => %{"YB Notifier" <noreply@yb.com>},
              :exception_recipients => YbNotifierEmailIds.pluck(:email_id)
            }
        end

-----------------------I have tried this:

        Rails.application.middleware.use ExceptionNotification::Rack,
              :email => {
                :email_prefix => "[YB-QA] ",
                :sender_address => %{"YB Notifier" <noreply@yb.com>},
                :exception_recipients => defined?(YbNotifierEmailId) && YbNotifierEmailId.table_exists? ? YbNotifierEmailIds.pluck(:email_id) : %w{test@ssolution.com}
        
            }

but defined?(YbNotifierEmailId) && YbNotifierEmailId.table_exists? this condition is getting failed.

  • have you tried using `ActiveRecord::Base.connection.present? and YbNotifierEmailId.table_exists?` – Masroor Aug 30 '22 at 17:52
  • Tried but its breaking during deployment. getting below error: ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base – Somebody_ror Sep 02 '22 at 07:08

1 Answers1

0

Have you considered moving this code to an initializer (i.e. a new file under the config/initializers folder) just containing your setup code for the middleware?

You'll have access to the models in code run at this point. You'll probably need the form Rails.application.middleware.use, per your last example ("have tried this:") - that should work, though I haven't explicitly checked this.

Something like a file named config/initializers/exception_notification.rb containing just your middleware setup:

Rails.application.middleware.use ExceptionNotification::Rack,
  :email => {
    :email_prefix => "[YB] ",
    :sender_address => %{"YB Notifier" <noreply@yb.com>},
    :exception_recipients => YbNotifierEmailIds.pluck(:email_id)
  }
ferrisoxide
  • 167
  • 5
  • It works, Thanks. But after changing folder I am facing another problem. As I have different host name for production, staging and development environment, for all these environments I have separate files like **production.rb.qa** , **production.rb.prod** etc....but after moving it to initializers folder and changing file name as exception_notification.rb.qa , email is not generated. – Somebody_ror Sep 06 '22 at 14:29
  • Just use a case statement in the initiatializer, _e.g._ `case Rails.env when "development" then ... development specific code ... when "production" ... end` - or whatever you need to detect and switch the code per environment. If you want to get super funky, use `require "production.rb.qa"` in the environment specific section of your `case` statement - though you might need to change the name of those files to something like `production_qa.rb`. They will also have to be outside of the `initializers` folder otherwise they will get added automatically. Does that help/make sense? – ferrisoxide Sep 07 '22 at 03:40
  • Noticed another issue after moving it to initializer folder, whenever I am adding new email_id to the database table , email has not been received to that user until I deployed. After adding any Email_id I have to do deployement now. – Somebody_ror Sep 08 '22 at 08:45
  • Yep, because the initializer code is only run once - when the app starts. – ferrisoxide Sep 08 '22 at 12:17
  • In that case, it won't be feasible because every time for any new record we can't do deployment. Do you have any another solution? – Somebody_ror Sep 08 '22 at 12:31
  • Nothing easy. It looks like someone has had a go at making the list of recipients a proc, but I've no idea how well this works (in terms of lazy evaluation): https://github.com/smartinez87/exception_notification/pull/330 – ferrisoxide Sep 09 '22 at 01:17
  • this won't do as we need to get the email ids from the database table which maybe updated while the application runs. – Somebody_ror Oct 17 '22 at 11:46
  • The proc based solution should work, as it appears to evaluate each time in the `compose_email` method within the `EmailNotifier` class. If not, you may just have to monkey-patch that method or the `maybe_call` method (the wrapper around the proc) to return the list of recipients dynamically. See https://github.com/smartinez87/exception_notification/blob/master/lib/exception_notifier/email_notifier.rb#L151 – ferrisoxide Oct 20 '22 at 22:52