Skip to content

Commit

Permalink
set up puma & capistrano
Browse files Browse the repository at this point in the history
  • Loading branch information
ParzivalRealm committed Feb 2, 2023
1 parent b21135a commit cd6858b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Capfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set :rvm_type, :user
set :rvm_ruby_version, '3.1.2'


# Load DSL and set up stages
require "capistrano/setup"

Expand Down Expand Up @@ -40,8 +41,8 @@ install_plugin Capistrano::SCM::Git
# require "capistrano/passenger"
require 'capistrano'
require 'capistrano-rails'
require 'capistrano-passenger'
require 'capistrano-rbenv'
require 'capistrano/puma'


require 'capistrano/bundler' # Rails needs Bundler, right?
Expand Down
2 changes: 1 addition & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ default: &default

development:
<<: *default
database: crawler_rebuild_development2
database: crawler_rebuild_development
username: deploy
password: Kingdom1

Expand Down
90 changes: 80 additions & 10 deletions config/deploy.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,84 @@
set :application, "crawler_rebuild"
set :repo_url, "[email protected]:ParzivalRealm/crawler_rebuild.git"
# Change these
server '64.227.97.163', port: 80, roles: [:web, :app, :db], primary: true

# Deploy to the user's home directory
set :deploy_to, "/home/deploy/#{fetch :application}"
set :repo_url, '[email protected]:ParzivalRealm/crawler_rebuild.git'
set :application, 'crawler_rebuild'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 0

append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', '.bundle', 'public/system', 'public/uploads'
# Don't change these unless you know what you're doing
set :pty, true
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

# Only keep the last 5 releases to save disk space
set :keep_releases, 5
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5

# Optionally, you can symlink your database.yml and/or secrets.yml file from the shared directory during deploy
# This is useful if you don't want to use ENV variables
# append :linked_files, 'config/database.yml', 'config/secrets.yml'
## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

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

# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
31 changes: 31 additions & 0 deletions config/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
upstream puma {
server unix:///home/deploy/apps/crawler_rebuild/shared/tmp/sockets/crawler_rebuild-puma.sock;
}

server {
listen 80 default_server deferred;
# server_name example.com;

root /home/deploy/apps/crawler_rebuild/current/public;
access_log /home/deploy/apps/crawler_rebuild/current/log/nginx.access.log;
error_log /home/deploy/apps/crawler_rebuild/current/log/nginx.error.log info;

location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}

try_files $uri/index.html $uri @puma;
location @puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

proxy_pass http://puma;
}

error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}

0 comments on commit cd6858b

Please sign in to comment.