Ruby-on-rails Rails:在 Heroku 上启动 Sidekiq

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13770713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:09:04  来源:igfitidea点击:

Rails: Starting Sidekiq on Heroku

ruby-on-railsherokusidekiq

提问by Greg Rogers

I'm having a problem getting Sidekiq up and running on my Heroku deployed Rails app. I have my app working fine in development (and on Heroku without Sidekiq).

我在 Heroku 部署的 Rails 应用程序上启动和运行 Sidekiq 时遇到问题。我的应用程序在开发中运行良好(在没有 Sidekiq 的 Heroku 上)。

I created a Procfile with:

我创建了一个 Procfile:

worker: bundle exec sidekiq

If I run heroku ps, the only process I see is web.1.

如果我运行heroku ps,我看到的唯一进程是web.1.

Should I see one for Sidekiq?

我应该为 Sidekiq 看一个吗?

I do get an error:

我确实收到一个错误:

Redis::CannotConnectError (Error connecting to Redis on localhost:6379)in my Heroku logs.

Redis::CannotConnectError (Error connecting to Redis on localhost:6379)在我的 Heroku 日志中。

UPDATE: Found I probably needed heroku addons:add redistogo. Still not working. I feel I'm missing some basic configuration.

更新:发现我可能需要heroku addons:add redistogo. 还是行不通。我觉得我缺少一些基本配置。

Is there something I need to do to get Redis up and running for my Heroku app?

我需要做些什么来让 Redis 为我的 Heroku 应用程序启动并运行吗?

I've been using Redis/Sidekiq for about a day, so this is new to me.

我已经使用 Redis/Sidekiq 大约一天了,所以这对我来说是新的。

Thanks!

谢谢!

Greg

格雷格

回答by gdurelle

No you do not need any config with Heroku for Sidekiq, just add the RedisToGoplugin and you're on. Do not forget to attribute at least 1 workerto your app in your Heroku config.

不,Sidekiq 的 Heroku 不需要任何配置,只需添加RedisToGo插件即可。不要忘记在您的 Heroku 配置中将至少 1 个工作人员归因于您的应用程序。

Here is my default Procfile:

这是我的默认 Procfile:

web: bundle exec thin start -p $PORT
worker: bundle exec sidekiq -c 5 -v

回答by justingordon

It's worth checking if the sidekiq process is really started with this command:

值得检查 sidekiq 进程是否真的使用以下命令启动:

heroku ps

If there's no worker, then you might need to run this command:

如果没有工作人员,那么您可能需要运行以下命令:

heroku ps:scale worker+1

It turns out that there's a bug in the web UI in that some team members were not allowed to increase the number of workers from 0 to 1, even though the UI seemed to show that!

事实证明,Web UI 中存在一个错误,即不允许某些团队成员将工作人员数量从 0 增加到 1,即使 UI 似乎显示了这一点!

回答by Steve

Starting with sidekiq version 3.0 there is an additional step, run heroku config:set REDIS_PROVIDER=REDISTOGO_URLin the console.

从 sidekiq 3.0 版开始,还有一个额外的步骤,heroku config:set REDIS_PROVIDER=REDISTOGO_URL在控制台中运行。

Here is the process I used for Rails 4:

这是我用于 Rails 4 的过程:

In the console:

在控制台中:

heroku addons:create redistogo
heroku config:set REDIS_PROVIDER=REDISTOGO_URL

In my ProcfileI added:

在我的Procfile我补充说:

worker: bundle exec sidekiq

In my gemfile.rbI added:

在我的gemfile.rb我补充说:

gem 'redis'

I added the following file, config/initializers/redis.rb:

我添加了以下文件config/initializers/redis.rb

uri = ENV["REDISTOGO_URL"] || "redis://localhost:6379/"
REDIS = Redis.new(:url => uri)

Here is the linkto the sidekiq docs.

这是sidekiq 文档的链接

回答by Jose Paez

Complementing gdurelle answer:

补充gdurelle答案:

You do need some config with Heroku for Sidekiq:

你确实需要一些 Heroku 的 Sidekiq 配置:

1) Have the Sidekiq and Redis gems installed (in gemfile and bundled), in my case:

1)安装 Sidekiq 和 Redis gems(在 gemfile 中并捆绑),在我的情况下:

Gemfile

文件

gem 'redis', '~> 3.1'
gem 'sidekiq', '~> 2.7', '>= 2.7.1'

2) Add a worker, if you don't have any workers created locally I suggest you create at least one, just in case, use this:

2)添加一个worker,如果你没有在本地创建任何worker,我建议你至少创建一个,以防万一,使用这个:

rails g sidekiq:worker Hard # will create app/workers/hard_worker.rb

should create this:

应该创建这个:

app/workers/hard_worker.rb

应用程序/工人/hard_worker.rb

class HardWorker
  include Sidekiq::Worker
  def perform(name, count)
    # do something
  end
end

3) Add the Redis add-on (in my case Heroku Redis):

3)添加Redis插件(在我的例子中是Heroku Redis):

heroku addons:create heroku-redis:hobby-dev

4) Add your redis.rb file, in my case:

4)添加你的 redis.rb 文件,在我的例子中:

config/initializers/redis.rb

配置/初始化程序/redis.rb

$redis = Redis.new(url: ENV["REDIS_URL"])

5) Add Procfile or config/sidekiq.yml or both, here are mine:

5) 添加 Procfile 或 config/sidekiq.yml 或两者,这里是我的:

Procfile

配置文件

worker: bundle exec sidekiq -c 1 -q default -q mailers 

which you can create easier by pasting this in your terminal

通过将其粘贴到终端中,您可以更轻松地创建

echo "worker: bundle exec sidekiq -c 1 -q default -q mailers" > Procfile

config/sidekiq.yml

配置/sidekiq.yml

:concurrency: 1
:queues:
  - [mailers, 7]
  - [default, 5]

6) Most important part go here: and turn on the switch for your worker, click on the pencil and then turn on the missing switch. Things should be working fine now, have a great day!

6)最重要的部分在这里为您的工人打开开关,单击铅笔,然后打开缺少的开关。现在一切正常,祝您有美好的一天!

回答by luigi7up

What I found out is that you have to scale process manually like so:

我发现您必须像这样手动扩展流程:

heroku ps:scale worker+1

Makes no sense since my Procfile said:

没有意义,因为我的 Procfile 说:

web: bundle exec....
worker: bundle exec sidekiq

...and one would've expectd Heroku to start the worker automatically. In the end I didn't have to scale the web process...

...人们会期望 Heroku 自动启动工作程序。最后,我不必扩展 Web 流程...

Also, you might have problems with this line: worker: bundle exec sidekiq

此外,您可能对这一行有问题:worker: bundle exec sidekiq

Add flags for concurency:

添加并发标志:

worker: bundle exec sidekiq -c 5 -v

回答by superluminary

You can of course run sidekiq alongside your current jobs queue.

您当然可以在当前作业队列旁边运行 sidekiq。

Our Procfile currently looks like this:

我们的 Procfile 目前看起来像这样:

web: bundle exec thin start -R config.ru -e $RAILS_ENV -p $PORT
worker:  bundle exec rake jobs:work
sidekiq: bundle exec sidekiq -c 5 -v

回答by gef

Sounds like you're almost there. It may be that you just need to set REDISTOGO_URL on the heroku config?

听起来你快到了。可能您只需要在heroku 配置上设置 REDISTOGO_URL ?

heroku config 

should show you the redistogo value?

应该向您展示 redistogo 的价值吗?

If you copy that to REDISTOGO_URL then sidekiq should work?

如果您将其复制到 REDISTOGO_URL 那么 sidekiq 应该可以工作吗?

heroku config:add REDISTOGO_URL=<redistogo value>

Edit:Sidekiq will use any of these currently: https://github.com/mperham/sidekiq/blob/master/lib/sidekiq/redis_connection.rb#L29-L33

编辑:Sidekiq 目前将使用其中任何一个:https://github.com/mperham/sidekiq/blob/master/lib/sidekiq/redis_connection.rb#L29-L33

Edit2:Greg is correct in that you don't need to add the config if you're using RedisToGo. But if you're using OpenRedis, or other Redis providers, then you need to add REDISTOGO_URL for Sidekiq

Edit2:Greg 是正确的,因为如果您使用的是 RedisToGo,则不需要添加配置。但是如果您使用的是 OpenRedis 或其他 Redis 提供商,那么您需要为 Sidekiq 添加 REDISTOGO_URL