【问题标题】:Using Redis in Heroku for a React and Rails API在 Heroku 中使用 Redis 实现 React 和 Rails API
【发布时间】:2020-09-13 21:02:59
【问题描述】:

我在 heroku 上部署了一个 Rails API,它用于 React.js 静态页面。两者都部署在 heroku 上,并通过 API 链接进行通信。使用 Redis 和 Sidekiq 时,我的挣扎即将来临。

在我的 Rails API 上,我的 RedisToGo 链接配置没有问题,但是当我转到我的 React 应用程序并尝试发送电子邮件邀请时,我收到了这条消息 Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)。 我想如果我在后端配置了它,那么它将适用于我的反应静态页面应用程序。

Sidekiq.yml

---
:verbose: false
:concurrency: 3
staging:
  :concurrency: 1
production:
  :concurrency: 5
:queues:
  - [mailers,2]
  - slack_notifications
  - mixpanel
  - invoices
  - default
  - rollbar

Redis.rb

uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:url => uri)

sidekiq.rb

Sidekiq::Extensions.enable_delay!

unless Rails.env == 'development' || Rails.env == 'test'
  Sidekiq.configure_server do |config|
    config.redis = {
      url: Rails.application.credentials.redis_url,
      password: Rails.application.credentials.redis_password
    }
  end

  Sidekiq.configure_client do |config|
    config.redis = {
      url: Rails.application.credentials.redis_url,
      password: Rails.application.credentials.redis_password
    }
  end
end

# Turn off backtrace if at all memory issues are popping up as
# backtrace occupies to much memory on redis
# number of lines of backtrace and number of re-tries
Sidekiq.default_worker_options = { backtrace: false, retry: 3 }

Sidekiq.configure_server do |config|
  # runs after your app has finished initializing
  # but before any jobs are dispatched.
  config.on(:startup) do
    puts 'Sidekiq is starting...'
    # make_some_singleton
  end
  config.on(:quiet) do
    puts 'Got USR1, stopping further job processing...'
  end
  config.on(:shutdown) do
    puts 'Got TERM, shutting down process...'
    # stop_the_world
  end
end

所以我的问题是下一个: 如果我已经在我的 Rails 应用程序上配置了 REDISTOGO_LINK,我的 react config vars 是否需要同样的配置?

在 Heroku 中使用 react 作为前端在 Rails API 上配置 sidekiq 和 Redis 的最佳方法是什么?我还没有在互联网上看到有关此内容的内容。

非常感谢您的帮助! ;)

【问题讨论】:

    标签: ruby-on-rails heroku redis sidekiq


    【解决方案1】:

    您需要运行 heroku config:set REDIS_PROVIDER=REDISTOGO_URL 来告诉 Sidekiq 使用 REDISTOGO_URL 连接到 Redis。

    https://github.com/mperham/sidekiq/wiki/Using-Redis#using-an-env-variable

    【讨论】:

    • 嗨迈克!感谢您的友好回复。因此,如果我已经在 heroku 中的 rails 应用程序上安装了 redis_provider。我的 react 静态应用程序不需要它,对吧?
    • 我认为你是对的。 Sidekiq 在后端使用 Rails。
    猜你喜欢
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 2015-04-23
    • 2014-10-16
    • 2012-05-12
    • 2018-07-06
    • 2021-05-17
    相关资源
    最近更新 更多