简体   繁体   English

将ActiveRecord的数据加载到Capistrano的deploy.rb

[英]Load ActiveRecord's data to Capistrano's deploy.rb

I'am using Capistrano for deployment and Sidekiq for queues. 我正在使用Capistrano进行部署,并使用Sidekiq进行队列。 I need to load user ids to set :sidekiq_queues variable in deploy.rb file to perform sidekiq queues. 我需要加载用户ID才能在deploy.rb文件中设置:sidekiq_queues变量以执行sidekiq队列。 Now I use following code 现在我使用以下代码

set :sidekiq_queues, ::User.all.map {|u| "-q parsing_user_#{u.id}"}.join(" ") + " -q parsing_user_0"

but it throws following error 但它引发以下错误

./config/deploy.rb:29:in `load': uninitialized constant User (NameError)

I tried to require 'rubygems' and 'active_record' into deploy.rb, but it didn't help. 我试图在deploy.rb中要求使用“ ruby​​gems”和“ active_record”,但这没有帮助。 In result I should have sidekiq_queues == "-q parsing_user_1 -q parsing_user_2 -q parsing_user_3 -q parsing_user_4 -q parsing_user_5 -q parsing_user_0". 结果,我应该有sidekiq_queues ==“ -q parsing_user_1 -q parsing_user_2 -q parsing_user_3 -q parsing_user_4 -q parsing_user_5 -q parsing_user_0”。 Hardcoding queue names isn't a solution. 硬编码队列名称不是解决方案。

Capistrano executes deploy.rb locally, and it doesn't load your Rails environment in deploy.rb. Capistrano在本地执行deploy.rb,并且不会将您的Rails环境加载到deploy.rb中。

It might be more trouble than it is worth. 麻烦可能多于其价值。 Especially if you want to execute this on your remote server, you might consider doing a Rake task instead. 特别是如果要在远程服务器上执行此操作,则可以考虑执行Rake任务。 The => :environment in the rake task ensures that your Rails environment is loaded. rake任务中的=> :environment可确保加载Rails环境。

# in deploy.rb
namespace :sidekiq do
  desc "Do something with queues"
  task :queues, :roles => :web  do
    run "cd #{current_path} ; RAILS_ENV=#{rails_env} bundle exec rake sidekiq:queues"
  end
end

# you'll need to decide when to execute this in your deployment process, 
# something like this:

after "deploy:update_code", "sidekiq:queues"



# in lib/tasks/sidekiq.rake
namespace :sidekiq do
  desc "Do something with queues"
  task :queues => :environment do
    queues = (User.scoped.pluck(:id) + [0]).map{|id| "-q parsing_user_#{id}"}.join(" ")
    # do what you need to do
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM