Permalink
Browse files

fix: moved all pid-related files from shared/pids to /run/lock

The main reason behind this fix is, that sometimes between server restarts,
PID files remain on the file system, causing `start` scripts to crash.
On AWS /run/lock is a special directory, designed for that, usually with tmpfs
mounted in it. This means, that after each restart this directory gets wiped out.

Fixes #92
  • Loading branch information...
ajgon committed Sep 29, 2017
1 parent 1822dd2 commit bc9daf095be6b9827e9162b0ddc58e0b568a5db3
@@ -59,7 +59,7 @@ def manual_action(action)
def add_appserver_config
opts = { deploy_dir: deploy_dir(app), out: out, deploy_env: deploy_env,
webserver: Drivers::Webserver::Factory.build(context, app).adapter,
appserver_config: appserver_config }
appserver_config: appserver_config, app_shortname: app['shortname'] }
context.template File.join(opts[:deploy_dir], File.join('shared', 'config', opts[:appserver_config])) do
owner node['deployer']['user']
@@ -20,7 +20,7 @@ def validate_app_engine; end
def add_worker_monit
opts = { application: app['shortname'], out: out, deploy_to: deploy_dir(app), environment: environment,
adapter: adapter }
adapter: adapter, app_shortname: app['shortname'] }
context.template File.join(node['monit']['basedir'], "#{opts[:adapter]}_#{opts[:application]}.monitrc") do
mode '0640'
@@ -65,7 +65,7 @@ def stop_sidekiq
end
def pid_file(process_number)
"#{deploy_dir(app)}/shared/pids/sidekiq_#{app['shortname']}-#{process_number}.pid"
"/run/lock/#{app['shortname']}/sidekiq_#{app['shortname']}-#{process_number}.pid"
end
def pid_exists?(pid)
View
@@ -62,15 +62,18 @@ def www_group
end
def create_deploy_dir(application, subdir = '/')
dir = File.join(deploy_dir(application), subdir)
directory dir do
create_dir File.join(deploy_dir(application), subdir)
end
def create_dir(path)
directory path do
mode '0755'
recursive true
owner node['deployer']['user'] || 'root'
group www_group
not_if { File.directory?(dir) }
not_if { File.directory?(path) }
end
dir
path
end
def deploy_dir(application)
View
@@ -11,10 +11,13 @@
create_deploy_dir(application, File.join('shared'))
create_deploy_dir(application, File.join('shared', 'config'))
create_deploy_dir(application, File.join('shared', 'log'))
create_deploy_dir(application, File.join('shared', 'pids'))
create_deploy_dir(application, File.join('shared', 'scripts'))
create_deploy_dir(application, File.join('shared', 'sockets'))
create_deploy_dir(application, File.join('shared', 'vendor/bundle'))
create_dir("/run/lock/#{application['shortname']}")
link File.join(deploy_dir(application), 'shared', 'pids') do
to "/run/lock/#{application['shortname']}"
end
databases = []
every_enabled_rds(self, application) do |rds|

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -212,7 +212,7 @@
expect(chef_run).to create_directory('/srv/www/a1/shared')
expect(chef_run).to create_directory('/srv/www/a1/shared/config')
expect(chef_run).to create_directory('/srv/www/a1/shared/log')
expect(chef_run).to create_directory('/srv/www/a1/shared/pids')
expect(chef_run).to create_directory('/run/lock/a1')
expect(chef_run).to create_directory('/srv/www/a1/shared/scripts')
expect(chef_run).to create_directory('/srv/www/a1/shared/sockets')
expect(chef_run).to create_directory('/srv/www/a1/shared/vendor/bundle')
@@ -253,10 +253,10 @@
expect(chef_run).to create_directory('/srv/www/a1/shared')
expect(chef_run).to create_directory('/srv/www/a1/shared/config')
expect(chef_run).to create_directory('/srv/www/a1/shared/log')
expect(chef_run).to create_directory('/srv/www/a1/shared/pids')
expect(chef_run).to create_directory('/srv/www/a1/shared/scripts')
expect(chef_run).to create_directory('/srv/www/a1/shared/sockets')
expect(chef_run).to create_directory('/srv/www/a1/shared/vendor/bundle')
expect(chef_run).to create_directory('/run/lock/a1')
expect(chef_run).to create_template('/srv/www/a1/shared/config/database.yml')
expect(chef_run).to create_template('/srv/www/a1/shared/config/puma.rb')
expect(chef_run).to create_template('/srv/www/a1/shared/scripts/puma.service')
@@ -270,10 +270,10 @@
expect(chef_run).to create_directory('/some/other/path/to/a1/shared')
expect(chef_run).to create_directory('/some/other/path/to/a1/shared/config')
expect(chef_run).to create_directory('/some/other/path/to/a1/shared/log')
expect(chef_run).to create_directory('/some/other/path/to/a1/shared/pids')
expect(chef_run).to create_directory('/some/other/path/to/a1/shared/scripts')
expect(chef_run).to create_directory('/some/other/path/to/a1/shared/sockets')
expect(chef_run).to create_directory('/some/other/path/to/a1/shared/vendor/bundle')
expect(chef_run).to create_directory('/run/lock/a1')
expect(chef_run).to create_template('/some/other/path/to/a1/shared/config/database.yml')
expect(chef_run).to create_template('/some/other/path/to/a1/shared/config/puma.rb')
expect(chef_run).to create_template('/some/other/path/to/a1/shared/scripts/puma.service')
@@ -36,14 +36,14 @@
run_execute(
'/bin/su - deploy -c \'cd /srv/www/dummy_project/current && ENV_VAR1="test" ' \
'ENV_VAR2="some data" RAILS_ENV="staging" HOME="/home/deploy" USER="deploy" ' \
'bundle exec sidekiqctl stop /srv/www/dummy_project/shared/pids/sidekiq_dummy_project-1.pid 8\''
'bundle exec sidekiqctl stop /run/lock/dummy_project/sidekiq_dummy_project-1.pid 8\''
)
)
expect(chef_run).to(
run_execute(
'/bin/su - deploy -c \'cd /srv/www/dummy_project/current && ENV_VAR1="test" ' \
'ENV_VAR2="some data" RAILS_ENV="staging" HOME="/home/deploy" USER="deploy" '\
'bundle exec sidekiqctl stop /srv/www/dummy_project/shared/pids/sidekiq_dummy_project-2.pid 8\''
'bundle exec sidekiqctl stop /run/lock/dummy_project/sidekiq_dummy_project-2.pid 8\''
)
)
end
@@ -9,7 +9,7 @@ ENV['<%= key.to_s %>'] = "<%= value.to_s %>"
ROOT_PATH="<%= @deploy_dir %>"
APP_NAME="<%= @app_shortname %>"
PID_PATH="<%= @deploy_dir %>/shared/pids/<%= @name %>.pid"
PID_PATH="/run/lock/<%= @app_shortname %>/<%= @name %>.pid"
DEPLOY_ENV="<%= @deploy_env %>"
def run_and_print_command(command)
@@ -1,6 +1,6 @@
<% ([@out[:process_count].to_i || 1].max).times do |n| %>
<% identifier = "#{@application}-#{n+1}" %>
<% pid_dir = "#{@deploy_to}/shared/pids/" %>
<% pid_dir = "/run/lock/#{@app_shortname}/" %>
<% syslog = !!@out[:syslog] ? "2>&1 | logger -t delayed_job-#{identifier}" : '' %>
<% queues = @out[:queues].present? ? '--queues=' + Array.wrap(@out[:queues]).join(',') : '' %>
@@ -21,12 +21,12 @@ daemonize true
# Store the pid of the server in the file at "path".
# pidfile '/u/apps/lolcat/tmp/pids/puma.pid'
pidfile "<%= @deploy_dir %>/shared/pids/puma.pid"
pidfile "/run/lock/<%= @app_shortname %>/puma.pid"
# Use "path" as the file to store the server info state. This is
# used by "pumactl" to query and control the server.
# state_path '/u/apps/lolcat/tmp/pids/puma.state'
state_path "<%= @deploy_dir %>/shared/pids/puma.state"
state_path "/run/lock/<%= @app_shortname %>/puma.state"
# Redirect STDOUT and STDERR to files specified. The 3rd parameter
# ("append") specifies whether the output is appended, the default is
@@ -1,6 +1,6 @@
<% ([@out[:process_count].to_i || 1].max).times do |n| %>
<% identifier = "#{@application}-#{n+1}" %>
<% pid_file = "#{@deploy_to}/shared/pids/resque_#{identifier}.pid" %>
<% pid_file = "/run/lock/#{@app_shortname}/resque_#{identifier}.pid" %>
<% syslog = !!@out[:syslog] ? "2>&1 | logger -t resque-#{identifier}" : '' %>
<% workers = [@out[:workers].to_i, 1].max %>
@@ -1,7 +1,7 @@
<% ([@out[:process_count].to_i || 1].max).times do |n| %>
<% identifier = "#{@application}-#{n+1}" %>
<% conf_file = "#{@deploy_to}/shared/config/sidekiq_#{n+1}.yml" %>
<% pid_file = "#{@deploy_to}/shared/pids/sidekiq_#{identifier}.pid" %>
<% pid_file = "/run/lock/#{@app_shortname}/sidekiq_#{identifier}.pid" %>
<% syslog = !!@out[:syslog] ? "2>&1 | logger -t sidekiq-#{identifier}" : '' %>
<% to_require = @out[:require].present? ? " -r #{File.join(@deploy_to, 'current', @out[:require])}" : '' %>
<% timeout = (@out[:config]['timeout'] || 8).to_i %>
@@ -1,6 +1,6 @@
---
user: "<%= node['deployer']['user'] %>"
pid: "<%= @deploy_dir %>/shared/pids/thin.pid"
pid: "/run/lock/<%= @app_shortname %>/thin.pid"
timeout: <%= @out[:timeout] %>
wait: <%= @out[:timeout] %>
log: "<%= @deploy_dir %>/shared/log/thin.log"
@@ -16,7 +16,7 @@ listen "<%= @deploy_dir %>/shared/sockets/unicorn.sock",
timeout <%= @out[:timeout] %>
pid "<%= @deploy_dir %>/shared/pids/unicorn.pid"
pid "/run/lock/<%= @app_shortname %>/unicorn.pid"
stderr_path "<%= @deploy_dir %>/shared/log/unicorn.stderr.log"
stdout_path "<%= @deploy_dir %>/shared/log/unicorn.stdout.log"
@@ -39,7 +39,7 @@ before_fork do |server, worker|
# Before forking, kill the master process that belongs to the .oldbin PID.
# This enables 0 downtime deploys.
old_pid = "<%= @deploy_dir %>/shared/pids/unicorn.pid.oldbin"
old_pid = "/run/lock/<%= @app_shortname %>/unicorn.pid.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
@@ -102,7 +102,7 @@
its(:content) { should include 'ENV[\'RAILS_ENV\'] = "staging"' }
its(:content) { should include 'ENV[\'HOME\'] = "/home/deploy"' }
its(:content) { should include 'ENV[\'USER\'] = "deploy"' }
its(:content) { should include 'PID_PATH="/srv/www/dummy_project/shared/pids/puma.pid"' }
its(:content) { should include 'PID_PATH="/run/lock/dummy_project/puma.pid"' }
its(:content) { should include 'def puma_running?' }
end
end
@@ -88,7 +88,7 @@
its(:content) { should include 'ENV[\'RAILS_ENV\'] = "production"' }
its(:content) { should include 'ENV[\'HOME\'] = "/home/deploy"' }
its(:content) { should include 'ENV[\'USER\'] = "deploy"' }
its(:content) { should include 'PID_PATH="/srv/www/dummy_project/shared/pids/puma.pid"' }
its(:content) { should include 'PID_PATH="/run/lock/dummy_project/puma.pid"' }
its(:content) { should include 'def puma_running?' }
end
end
@@ -159,7 +159,7 @@
its(:content) { should include 'ENV[\'RAILS_ENV\'] = "production"' }
its(:content) { should include 'ENV[\'HOME\'] = "/home/deploy"' }
its(:content) { should include 'ENV[\'USER\'] = "deploy"' }
its(:content) { should include 'PID_PATH="/srv/www/yet_another_project/shared/pids/unicorn.pid"' }
its(:content) { should include 'PID_PATH="/run/lock/yet_another_project/unicorn.pid"' }
its(:content) { should include 'def unicorn_running?' }
end
end
@@ -61,7 +61,7 @@
its(:content) { should include 'ENV[\'RACK_ENV\'] = "production"' }
its(:content) { should include 'ENV[\'HOME\'] = "/home/deploy"' }
its(:content) { should include 'ENV[\'USER\'] = "deploy"' }
its(:content) { should include 'PID_PATH="/srv/www/dummy_project/shared/pids/thin.pid"' }
its(:content) { should include 'PID_PATH="/run/lock/dummy_project/thin.pid"' }
its(:content) { should include 'def thin_running?' }
end
end
@@ -73,14 +73,14 @@
its(:content) do
should include 'RACK_ENV="production" DATABASE_URL="sqlite:///srv/www/dummy_project/shared/db/data.sqlite3" ' \
'HOME="/home/deploy" USER="deploy" bin/delayed_job start ' \
'--pid-dir=/srv/www/dummy_project/shared/pids/ -i 0 --queues=default,mailers'
'--pid-dir=/run/lock/dummy_project/ -i 0 --queues=default,mailers'
end
its(:content) { should include 'logger -t delayed_job-dummy_project-1' }
its(:content) { should include 'check process delayed_job_dummy_project-2' }
its(:content) do
should include 'RACK_ENV="production" DATABASE_URL="sqlite:///srv/www/dummy_project/shared/db/data.sqlite3" ' \
'HOME="/home/deploy" USER="deploy" bin/delayed_job start ' \
'--pid-dir=/srv/www/dummy_project/shared/pids/ -i 1 --queues=default,mailers'
'--pid-dir=/run/lock/dummy_project/ -i 1 --queues=default,mailers'
end
its(:content) { should include 'logger -t delayed_job-dummy_project-2' }
end
@@ -89,7 +89,7 @@
its(:content) { should include 'ENV[\'HANAMI_ENV\'] = "production"' }
its(:content) { should include 'ENV[\'HOME\'] = "/home/deploy"' }
its(:content) { should include 'ENV[\'USER\'] = "deploy"' }
its(:content) { should include 'PID_PATH="/srv/www/dummy_project/shared/pids/unicorn.pid"' }
its(:content) { should include 'PID_PATH="/run/lock/dummy_project/unicorn.pid"' }
its(:content) { should include 'def unicorn_running?' }
end
end
@@ -110,15 +110,15 @@
its(:content) do
should include 'HANAMI_ENV="production" DATABASE_URL="sqlite:///srv/www/dummy_project/shared/db/data.sqlite3"' \
' HOME="/home/deploy" USER="deploy" QUEUE=default,mailers VERBOSE=1 ' \
'PIDFILE=/srv/www/dummy_project/shared/pids/' \
'PIDFILE=/run/lock/dummy_project/' \
'resque_dummy_project-1.pid COUNT=3 bundle exec rake environment resque:work'
end
its(:content) { should include 'logger -t resque-dummy_project-1' }
its(:content) { should include 'check process resque_dummy_project-2' }
its(:content) do
should include 'HANAMI_ENV="production" DATABASE_URL="sqlite:///srv/www/dummy_project/shared/db/data.sqlite3"' \
' HOME="/home/deploy" USER="deploy" QUEUE=default,mailers VERBOSE=1 ' \
'PIDFILE=/srv/www/dummy_project/shared/pids/' \
'PIDFILE=/run/lock/dummy_project/' \
'resque_dummy_project-2.pid COUNT=3 bundle exec rake environment resque:work'
end
its(:content) { should include 'logger -t resque-dummy_project-2' }

0 comments on commit bc9daf0

Please sign in to comment.