0

After I configure to use postmark service to post emails I'm facing some troubles with development and staging envs.

Short explanation: portmark is working nice in the below situations with 'ok', and when there is DeserializationError it seems that the active job is failing because it is looking to see if the user id exists on production database.

Development env:

devise mailing -> ActiveJob::DeserializationError
deliver_later -> ActiveJob::DeserializationError
delover_now  -> ok 

Staging env

devise mailing -> ActiveJob::DeserializationError
deliver_later -> ActiveJob::DeserializationError
delover_now  -> ok 

production env

devise mailing -> ok
deliver_later -> ok
deliver_now -> ok

Long explanation:

When I'm on these two envs and requests with devise the confirmation instructions, it sends a link to this email with a production url with the token attached to it which points to wrong page.

The most intriguing thing is that after I send an email from development or staging envs console, and I opened up sidekiq dashboard and I found out that when there is no user id on production associated with the id that this email was requested to send (from the other envs), this mailing fails with this message:

Console:

> Rails.env
"development"
> @user.id
22
> UserMailer.welcome(@user).deliver_later

Sidekiq error:

ActiveJob::DeserializationError: Error while trying to deserialize arguments: Couldn't find User with 'id'=22

Console:

> Rails.env
"staging"
> @user.id
15
> UserMailer.welcome(@user).deliver_later

Sidekiq error:

ActiveJob::DeserializationError: Error while trying to deserialize arguments: Couldn't find User with 'id'=15


Some configs:

Rails 5.0.1
postmark (1.14.0)
postmark-rails (0.18.0)

production.rb

...
  config.action_mailer.delivery_method = :postmark
  config.action_mailer.postmark_settings = { :api_token => ENV["POSTMARK_API_KEY"] }
  config.action_mailer.default_url_options = { host: "www.mywebsite.com" }
...

development.rb

...
  config.action_mailer.delivery_method = :postmark
  config.action_mailer.postmark_settings = { :api_token => ENV["POSTMARK_API_KEY"] }
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
...

staging.rb

...
  config.action_mailer.delivery_method = :postmark
  config.action_mailer.postmark_settings = { :api_token => ENV["POSTMARK_API_KEY"] }
  config.action_mailer.default_url_options = { host: "website.herokuapp.com" }
...

sidekiq.yml

:concurrency: 3
:timeout: 60
:verbose: true
:queues:  
  - default
  - mailers

Obs: all the three environments uses different postmark api key.

UPDATE 1

Here is the development:

development.rb

Rails.application.configure do

  config.action_mailer.delivery_method = :postmark
  config.action_mailer.postmark_settings = { :api_token => ENV["POSTMARK_API_KEY"] }
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

  config.cache_classes = false

  config.eager_load = false

  # Show full error reports.
  config.consider_all_requests_local = true

  # Enable/disable caching. By default caching is disabled.
  if Rails.root.join('tmp/caching-dev.txt').exist?
    config.action_controller.perform_caching = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => 'public, max-age=172800'
    }
  else
    config.action_controller.perform_caching = false

    config.cache_store = :null_store
  end

  config.assets.precompile += %w( '.svg' ) 

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.perform_caching = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Suppress logger output for asset requests.
  config.assets.quiet = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  # Use an evented file watcher to asynchronously detect changes in source code,
  # routes, locales, etc. This feature depends on the listen gem.
  config.file_watcher = ActiveSupport::EventedFileUpdateChecker

end

I changed to the old configuration with gmail and the problem persists...

development.rb

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.

  config.action_mailer.delivery_method = :letter_opener
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports.
  config.consider_all_requests_local = true

  # Enable/disable caching. By default caching is disabled.
  if Rails.root.join('tmp/caching-dev.txt').exist?
    config.action_controller.perform_caching = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => 'public, max-age=172800'
    }
  else
    config.action_controller.perform_caching = false

    config.cache_store = :null_store
  end

  config.assets.precompile += %w( '.svg' ) 

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.perform_caching = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Suppress logger output for asset requests.
  config.assets.quiet = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  # Use an evented file watcher to asynchronously detect changes in source code,
  # routes, locales, etc. This feature depends on the listen gem.
  config.file_watcher = ActiveSupport::EventedFileUpdateChecker


  config.action_mailer.delivery_method = :smtp

  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: 'gmail.com',
    user_name: ENV['GMAIL_ADDRESS'],
    password: ENV['GMAIL_APP_PASSWORD'],
    authentication: "plain",
    enable_starttls_auto: true
  }

  config.action_mailer.default_url_options = {host: "localhost:3000"}

end

UPDATE 2:

I also tried to go to past branches which the mailing was working and nothing (I restarted the server)...

Fillype Farias
  • 622
  • 1
  • 8
  • 20
  • I'm not entirely sure what you're asking here. You may also need to add more of your code from mailers, controllers, models, etc. that regard this problem. – Jake Feb 13 '19 at 14:36
  • Hi @Jake, what is going on is that it seems that when I have the 'DeserializationError' is when the environement is "hacking" to use the production, and is having access to production database too...I don't know how it can be possible and I think that maybe it is related with the postmark DNS calling which is programmed internally to the production url and not staging or localhost. – Fillype Farias Feb 13 '19 at 16:24
  • @Jake Here it is – Fillype Farias Feb 13 '19 at 17:01
  • It didn't work and the confusing part is that the staging.rb is pretty the same as production.rb file and it has the issue... – Fillype Farias Feb 13 '19 at 18:03
  • This might just be an issue in your controller/model and how you're sending mail. – Jake Feb 13 '19 at 18:07
  • Check out this answer [here](https://stackoverflow.com/questions/39677612/deliver-later-not-working-in-the-test-environment-in-rails-5), it might fix this. – Jake Feb 13 '19 at 19:21

1 Answers1

0

This is a partial solution to make possible to use mailing on test envs in my case.

On this rails mailing documentation on topic 10.2 Deserialization says:

If a passed record is deleted after the job is enqueued but before the #perform method is called Active Job will raise an ActiveJob::DeserializationError exception.

So I realized that somehow the record has being deleted from cache before the sending method (deliver_later) takes action. If this is going only on development and staging environments, then I attached on:

production.rb

config.active_job.queue_adapter = :sidekiq

development.rb / staging.rb

  config.active_job.queue_adapter = :inline
Fillype Farias
  • 622
  • 1
  • 8
  • 20