如何在两台服务器之间分离rake任务

时间:2017-06-25 08:11:32

标签: ruby-on-rails ruby rake puma rake-task

我的Rails应用程序有2个不同的远程服务器。要使用capistrano部署im。第一次 - 分期,第二次制作。要运行puma服务器,每个服务器都使用自己的命令,所以在本地机器上使用下一个:

STAGING:

run("bundle exec puma -C /var/www/snapship/shared/config/puma.rb --daemon")

PROD:

sudo :systemctl, "start puma.target"

当我部署我的应用程序时,我写道:cap staging deploy或cap production deploy。我怎样才能让Rails应用程序知道应该使用哪个美洲狮?因为我无法检查本地机器上的Rails.env(它总是开发)

UPDATE ::(Capfile)

require "capistrano/setup"
require "capistrano/deploy"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
require "capistrano/rbenv"
require "capistrano/bundler"
require "capistrano/puma"
require "capistrano/puma/nginx"
require "capistrano/puma/jungle"
require "sshkit/sudo"
require "whenever/capistrano"

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

Deploy.rb

lock '3.5.0'

set :application, 'snapship'
set :pty, true
set :repo_url, 'git@gitlab.com:snapship/snapship-backend.git'
set :user, ENV['USER'] || 'deploy'
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml', 'config/puma.rb', '.env', '.rbenv-vars')
set :linked_dirs, %w(tmp/pids tmp/sockets log public/uploads)

# rbenv plugin setup
set :rbenv_type, :user
set :rbenv_ruby, File.read('.ruby-version').strip
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, fetch(:rbenv_map_bins, []).push('foreman')

# bundle plugin setup
set :bundle_bins, fetch(:bundle_bins, []).push('foreman')

# puma plugin setup
set :puma_preload_app, true
set :puma_init_active_record, true
set :puma_conf, "#{shared_path}/config/puma.rb"


before :deploy, "deploy:run_tests"
after "deploy:publishing", "foreman:export"
after "deploy:publishing", "systemd:update"
after "deploy:publishing", "systemd:enable"

set :migration_servers, -> { release_roles(fetch(:migration_role)) }

每个部署还有2个文件暂存和生产。

1 个答案:

答案 0 :(得分:0)

对我有用的是什么:

# in deploy.rb
# define stages
set :stages, %w(staging production)
# then get the current stage
set :my_var_env, Proc.new { fetch :stage }

然后运行cap staging deploy

我还使用了gem 'capistrano3-puma',它提供了像

这样的任务
cap staging|production puma:[start|stop|restart|status]