I need to deploy mailman with daemon and capistrano for my rails app. I came across this article and just followed the steps that he wrote.
But when I tried to deploy it, my local machined gave me this error:
The deploy has failed with an error: undefined local variable or method `mailman'.
I know the reason why this failed is because I am using the latest version capistrano (version 3.4.0) while the code is for the older version of it (probably version 2+)
Below is the code:
# Mailman configuration
namespace :mailman do
desc "Mailman::Start"
task :start, :roles => [:app] do
run "cd #{current_path};RAILS_ENV=#{rails_env} script/mailman_daemon.rb start"
end
desc "Mailman::Stop"
task :stop, :roles => [:app] do
run "cd #{current_path};RAILS_ENV=#{rails_env} script/mailman_daemon.rb stop"
end
desc "Mailman::Restart"
task :restart, :roles => [:app] do
mailman.stop mailman.start
end
end
before "deploy:cleanup", "mailman:restart"
So, may I know how could I rewrite the above code to follow the latest version of capistrano?
Thanks!
############################## UPDATE ########################
Below is my deploy.rb
file:
server '178.62.16.69', port: 22, roles: [:web, :app, :db], primary: true
set :repo_url, 'git@github.com:ryzalyusoff/xxxx.git'
set :application, 'xxxx'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 0
set :pty, false
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
set :whenever_identifier, ->{ "myapp_#{fetch(:stage)}" }
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
# Mailman configuration
namespace :mailman do
desc "Mailman::Start"
task :start do
on roles(:app) do
within release_path do
with default_env: fetch(:default_env) do
execute "script/mailman_daemon.rb", "start"
end
end
end
end
desc "Mailman::Stop"
task :stop do
on roles(:app) do
within release_path do
with default_env: fetch(:default_env) do
execute "script/mailman_daemon.rb", "stop"
end
end
end
end
desc "Mailman::Restart"
task :restart do
on roles(:app) do
invoke("mailman:stop")
invoke("mailman:start")
end
end
end
before "deploy:cleanup", "mailman:restart"
Capistrano 3 syntax changed quite a lot, try this:
# Mailman configuration
namespace :mailman do
desc "Mailman::Start"
task :start do
on roles(:app) do
within release_path do
with rails_env: fetch(:rails_env) do
execute "script/mailman_daemon.rb", "start"
end
end
end
end
desc "Mailman::Stop"
task :stop do
on roles(:app) do
within release_path do
with rails_env: fetch(:rails_env) do
execute "script/mailman_daemon.rb", "stop"
end
end
end
end
desc "Mailman::Restart"
task :restart do
on roles(:app) do
invoke("mailman:stop")
invoke("mailman:start")
end
end
end
before "deploy:cleanup", "mailman:restart"
See the Capistrano upgrade guide for more options.