【问题标题】:Postmark mailing on localhost and staging problem本地主机上的邮戳邮件和暂存问题
【发布时间】:2019-07-07 08:18:38
【问题描述】:

在我配置为使用 postmark 服务发布电子邮件后,我在使用 developmentstaging 环境时遇到了一些问题。

简短说明:portmark 在以下情况下运行良好,“ok”,当有 DeserializationError 时,似乎活动作业失败,因为它正在查看用户是否id 存在于生产数据库中。

开发环境:

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

暂存环境

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

生产环境

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

详细解释

当我在这两个环境中并使用devise 确认说明请求时,它会发送一个指向此电子邮件的链接,其中包含一个production url,并附有指向错误页面的令牌。

最有趣的是,在我从开发或登台 envs 控制台发送电子邮件后,我打开了 sidekiq 仪表板,我发现当生产中没有与请求此电子邮件的 ID 相关联的用户 ID 时发送(从其他环境),此邮件失败并显示此消息:

控制台:

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

Sidekiq 错误:

ActiveJob::DeserializationError: 尝试反序列化时出错 参数:找不到 'id'=22 的用户

控制台:

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

Sidekiq 错误:

ActiveJob::DeserializationError: 尝试反序列化时出错 参数:找不到 'id'=15 的用户


一些配置:

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:三种环境都使用不同的邮戳api key。

更新 1

这里是发展:

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

我用gmail改成旧配置,问题依旧……

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

更新 2:

我也试图去过去的邮件正在工作的分支,但什么也没有(我重新启动了服务器)......

【问题讨论】:

  • 我不完全确定你在这里问什么。您可能还需要从邮件程序、控制器、模型等中添加更多与此问题相关的代码。
  • 嗨@Jake,发生的事情似乎是当我有“DeserializationError”时,环境正在“黑客”使用生产,并且也可以访问生产数据库。 ..我不知道这怎么可能,我认为这可能与邮戳 DNS 调用有关,该调用在内部编程到生产 url 而不是暂存或 localhost。
  • @Jake 在这里
  • 它没有用,令人困惑的部分是 staging.rb 与 production.rb 文件非常相似,并且存在问题......
  • 这可能只是您的控制器/模型以及您如何发送邮件的问题。

标签: ruby-on-rails sidekiq postmark


【解决方案1】:

这是一个部分解决方案,可以在我的情况下在测试环境中使用邮件。

关于主题 10.2 反序列化的 rails mailing documentation 说:

如果在作业入队之后但在调用#perform 方法之前删除了传递的记录,Active Job 将引发 ActiveJob::DeserializationError 异常。

所以我意识到,在发送方法 (deliver_later) 采取行动之前,不知何故,该记录已从缓存中删除。如果这仅适用于 developmentstaging 环境,那么我附上:

production.rb

config.active_job.queue_adapter = :sidekiq

development.rb / staging.rb

  config.active_job.queue_adapter = :inline

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-19
    • 2018-05-02
    • 2010-11-22
    • 2014-09-12
    • 2017-01-09
    • 1970-01-01
    • 2021-11-20
    • 2018-08-14
    相关资源
    最近更新 更多