Skip to content

Commit 3fdc337

Browse files
authored
Merge pull request #1 from ntamvl/deploy
Add mina deployment
2 parents 432fd61 + 1498ba9 commit 3fdc337

File tree

3 files changed

+80
-198
lines changed

3 files changed

+80
-198
lines changed

Gemfile.lock

Lines changed: 0 additions & 186 deletions
This file was deleted.

config/deploy.rb

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require 'mina/git'
44
require 'mina/rbenv' # for rbenv support. (https://rbenv.org)
55
# require 'mina/rvm' # for rvm support. (https://rvm.io)
6-
require 'mina/puma'
76

87
# Basic settings:
98
# domain - The hostname to SSH to.
@@ -14,7 +13,8 @@
1413
set :domain, 'xdev-server'
1514
set :deploy_to, '/home/tamnguyen/apps/rails_5_api'
1615
set :repository, 'git@github.com:ntamvl/rails_5_api_tutorial.git'
17-
set :branch, 'master'
16+
set :branch, 'deploy'
17+
set :rails_env, 'production'
1818

1919
# Optional settings:
2020
# set :user, 'foobar' # Username in the server to SSH to.
@@ -27,8 +27,8 @@
2727
# They will be linked in the 'deploy:link_shared_paths' step.
2828
# set :shared_dirs, fetch(:shared_dirs, []).push('config')
2929
# set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'config/secrets.yml')
30-
set :shared_dirs, fetch(:shared_dirs, []).push('config', 'tmp/sockets', 'tmp/pids')
31-
set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'Gemfile.lock')
30+
set :shared_dirs, fetch(:shared_dirs, []).push('tmp/sockets', 'tmp/pids')
31+
set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'Gemfile.lock', 'config/puma.rb')
3232

3333
# This task is the environment that is loaded all remote run commands, such as
3434
# `mina deploy` or `mina rake`.
@@ -74,11 +74,15 @@
7474
# Put things that will set up an empty directory into a fully set-up
7575
# instance of your project.
7676
invoke :'git:clone'
77-
invoke :'deploy:link_shared_paths'
77+
# invoke :'deploy:link_shared_paths'
78+
invoke :'my_link_paths'
7879
invoke :'bundle:install'
79-
invoke :'rails:db_migrate'
80-
invoke :'rails:assets_precompile'
80+
# invoke :'rails:db_migrate'
81+
# invoke :'rails:assets_precompile'
82+
invoke :'my_db_migrate'
83+
invoke :'my_assets_clean'
8184
invoke :'deploy:cleanup'
85+
invoke :'my_restart_puma'
8286

8387
on :launch do
8488
in_path(fetch(:current_path)) do
@@ -92,6 +96,57 @@
9296
# run :local { say 'done' }
9397
end
9498

99+
task :my_link_paths do
100+
comment %{Symlinking my shared paths}
101+
102+
fetch(:shared_dirs, []).each do |linked_dir|
103+
command %{mkdir -p #{File.dirname("./#{linked_dir}")}}
104+
command %{rm -rf "./#{linked_dir}"}
105+
command %{ln -s "#{fetch(:shared_path)}/#{linked_dir}" "./#{linked_dir}"}
106+
end
107+
108+
fetch(:shared_files, []).each do |linked_path|
109+
command %{ln -sf "#{fetch(:shared_path)}/#{linked_path}" "./#{linked_path}"}
110+
end
111+
end
112+
113+
task :my_db_migrate do
114+
in_path(fetch(:current_path)) do
115+
comment %{Running command rails db:migrate for Rails 5+...}
116+
command %{rails db:migrate RAILS_ENV=production}
117+
end
118+
end
119+
120+
task :my_assets_clean do
121+
in_path(fetch(:current_path)) do
122+
comment %{Running command rails tmp:clear for Rails 5+...}
123+
command %{rails tmp:clear RAILS_ENV=production}
124+
end
125+
end
126+
127+
task :my_start_puma do
128+
in_path(fetch(:current_path)) do
129+
comment %{Puma is starting...}
130+
command %{bundler exec puma -C config/puma.rb -e production -d}
131+
comment %{Puma started!}
132+
end
133+
end
134+
135+
task :my_stop_puma do
136+
in_path(fetch(:current_path)) do
137+
comment %{Puma is stopping...}
138+
command %{kill -9 `cat tmp/pids/puma.pid`}
139+
comment %{Puma stopped!}
140+
end
141+
end
142+
143+
task :my_restart_puma do
144+
in_path(fetch(:current_path)) do
145+
invoke :'my_stop_puma'
146+
invoke :'my_start_puma'
147+
end
148+
end
149+
95150
# For help in making your deploy script, see the Mina documentation:
96151
#
97152
# - https://github.com/mina-deploy/mina/docs

config/puma.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@
44
# the maximum value specified for Puma. Default is set to 5 threads for minimum
55
# and maximum, this matches the default thread size of Active Record.
66
#
7+
root = File.expand_path("../..", __FILE__)
8+
79
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
810
threads threads_count, threads_count
911

1012
# Specifies the `port` that Puma will listen on to receive requests, default is 3000.
1113
#
1214
port ENV.fetch("PORT") { 3000 }
1315

16+
# Set up socket location
17+
bind "unix://#{root}/tmp/sockets/puma.sock"
18+
19+
# Logging
20+
# stdout_redirect "#{root}/log/puma.stdout.log", "#{root}/log/puma.stderr.log", true
21+
22+
# Set master PID and state locations
23+
pidfile "#{root}/tmp/pids/puma.pid"
24+
state_path "#{root}/tmp/pids/puma.state"
25+
activate_control_app
26+
1427
# Specifies the `environment` that Puma will run in.
1528
#
1629
environment ENV.fetch("RAILS_ENV") { "development" }
@@ -21,7 +34,7 @@
2134
# Workers do not work on JRuby or Windows (both of which do not support
2235
# processes).
2336
#
24-
workers ENV.fetch("WEB_CONCURRENCY") { 2 }
37+
workers ENV.fetch("WEB_CONCURRENCY") { 5 }
2538

2639
# Use the `preload_app!` method when specifying a `workers` number.
2740
# This directive tells Puma to first boot the application and load code
@@ -38,10 +51,10 @@
3851
# option you will want to use this block to reconnect to any threads
3952
# or connections that may have been created at application boot, Ruby
4053
# cannot share connections between processes.
41-
#
42-
# on_worker_boot do
43-
# ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
44-
# end
54+
55+
on_worker_boot do
56+
ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
57+
end
4558

4659
# Allow puma to be restarted by `rails restart` command.
4760
plugin :tmp_restart

0 commit comments

Comments
 (0)