Skip to content
This repository was archived by the owner on Mar 7, 2018. It is now read-only.

Commit ee64d7d

Browse files
committed
Switch server to puma, provide puma config and adapt command line
- configure puma using config/puma.rb config file - add default puma.rb config file for new projects - adapt dashing cli to use the config file and correctly handle -d (daemonize) arg. - adapt cli_test.rb to the new setup - remove tmp dir from gitignore as we want to generate it in the project template - add tmp/pids/ dir in project template (for saving puma pid / state files.
1 parent 34ec439 commit ee64d7d

8 files changed

Lines changed: 72 additions & 26 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
*.gem
44
coverage/
55
log/
6-
tmp/
76
.ruby-version
87
history.yml

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ rvm:
33
- 2.3.0
44
- 2.2.4
55
- 2.1.8
6-
6+
- jruby-19mode
7+
- jruby-9.0.4.0
78
script: "rake test"

dashing.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
2121
s.add_dependency('execjs', '~> 2.0.2')
2222
s.add_dependency('sinatra', '~> 1.4.4')
2323
s.add_dependency('sinatra-contrib', '~> 1.4.2')
24-
s.add_dependency('thin', '~> 1.6.1')
24+
s.add_dependency('puma', '~> 2.16.0')
2525
s.add_dependency('rufus-scheduler', '~> 2.0.24')
2626
s.add_dependency('thor', '> 0.18.1')
2727
s.add_dependency('sprockets', '~> 2.10.1')

lib/dashing/app.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
require 'sinatra/streaming'
88
require 'sprockets'
99
require 'yaml'
10-
require 'thin'
1110

1211
SCHEDULER = Rufus::Scheduler.new
1312

@@ -134,16 +133,6 @@ def authenticated?(token)
134133
end
135134
end
136135

137-
Thin::Server.class_eval do
138-
def stop_with_connection_closing
139-
Sinatra::Application.settings.connections.dup.each(&:close)
140-
stop_without_connection_closing
141-
end
142-
143-
alias_method :stop_without_connection_closing, :stop
144-
alias_method :stop, :stop_with_connection_closing
145-
end
146-
147136
def send_event(id, body, target=nil)
148137
body[:id] = id
149138
body[:updatedAt] ||= Time.now.to_i

lib/dashing/cli.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,20 @@ def install(gist_id, *args)
5757
desc "start", "Starts the server in style!"
5858
method_option :job_path, :desc => "Specify the directory where jobs are stored"
5959
def start(*args)
60-
port_option = args.include?('-p') ? '' : ' -p 3030'
60+
daemonize = args.include?('-d')
6161
args = args.join(' ')
62-
command = "bundle exec thin -R config.ru start#{port_option} #{args}"
62+
command = "bundle exec puma #{args}"
6363
command.prepend "export JOB_PATH=#{options[:job_path]}; " if options[:job_path]
64+
command.prepend "export DAEMONIZE=true; " if daemonize
6465
run_command(command)
6566
end
6667

67-
desc "stop", "Stops the thin server"
68-
def stop
69-
command = "bundle exec thin stop"
68+
desc "stop", "Stops the puma server (daemon mode only)"
69+
def stop(*args)
70+
args = args.join(' ')
71+
# TODO correctly handle pidfile location change in puma config
72+
daemon_pidfile = !args.include?('--pidfile') ? '--pidfile ./tmp/pids/puma.pid' : args
73+
command = "bundle exec pumactl #{daemon_pidfile} stop"
7074
run_command(command)
7175
end
7276

templates/project/config/puma.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# For a complete list of puma configuration parameters, please see
2+
# https://github.com/puma/puma
3+
4+
# Puma can serve each request in a thread from an internal thread pool.
5+
# The `threads` method setting takes two numbers a minimum and maximum.
6+
# Any libraries that use thread pools should be configured to match
7+
# the maximum value specified for Puma. Default is set to 5 threads for minimum
8+
# and maximum.
9+
#
10+
threads_count = ENV.fetch("PUMA_MAX_THREADS") { 5 }.to_i
11+
threads threads_count, threads_count
12+
13+
# Specifies the `port` that Puma will listen on to receive requests, default is 2020.
14+
#
15+
port ENV.fetch("DASHING_PORT") { 3030 }
16+
17+
# Specifies the `environment` that Puma will run in.
18+
#
19+
environment ENV.fetch("RACK_ENV") { "production" }
20+
21+
# Daemonize the server into the background. Highly suggest that
22+
# this be combined with "pidfile" and "stdout_redirect".
23+
#
24+
# The default is "false".
25+
#
26+
daemonize ENV.fetch("DAEMONIZE") { false }
27+
28+
# Store the pid of the server in the file at "path".
29+
#
30+
pidfile './tmp/pids/puma.pid'
31+
32+
# Use "path" as the file to store the server info state. This is
33+
# used by "pumactl" to query and control the server.
34+
#
35+
state_path './tmp/pids/puma.state'
36+
37+
# Redirect STDOUT and STDERR to files specified. The 3rd parameter
38+
# ("append") specifies whether the output is appended, the default is
39+
# "false".
40+
#
41+
# stdout_redirect '/u/apps/lolcat/log/stdout', '/u/apps/lolcat/log/stderr'
42+
# stdout_redirect '/u/apps/lolcat/log/stdout', '/u/apps/lolcat/log/stderr', true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.empty_directory

test/cli_test.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,41 @@ def test_install_task_warns_when_gist_not_found
101101
assert_includes output, 'Could not find gist at '
102102
end
103103

104-
def test_start_task_starts_thin_with_default_port
105-
command = 'bundle exec thin -R config.ru start -p 3030 '
104+
def test_start_task_starts_puma_with_default_port
105+
command = 'bundle exec puma '
106106
@cli.stubs(:run_command).with(command).once
107107
@cli.start
108108
end
109109

110-
def test_start_task_starts_thin_with_specified_port
111-
command = 'bundle exec thin -R config.ru start -p 2020'
110+
def test_start_task_starts_puma_in_daemon_mode
111+
commands = [
112+
'export DAEMONIZE=true; ',
113+
'bundle exec puma -d'
114+
]
115+
116+
@cli.stubs(:run_command).with(commands.join('')).once
117+
@cli.start('-d')
118+
end
119+
120+
def test_start_task_starts_puma_with_specified_port
121+
command = 'bundle exec puma -p 2020'
112122
@cli.stubs(:run_command).with(command).once
113123
@cli.start('-p', '2020')
114124
end
115125

116126
def test_start_task_supports_job_path_option
117127
commands = [
118128
'export JOB_PATH=other_spot; ',
119-
'bundle exec thin -R config.ru start -p 3030 '
129+
'bundle exec puma '
120130
]
121131

122132
@cli.stubs(:options).returns(job_path: 'other_spot')
123133
@cli.stubs(:run_command).with(commands.join('')).once
124134
@cli.start
125135
end
126136

127-
def test_stop_task_stops_thin_server
128-
@cli.stubs(:run_command).with('bundle exec thin stop')
137+
def test_stop_task_stops_puma_server
138+
@cli.stubs(:run_command).with('bundle exec pumactl --pidfile ./tmp/pids/puma.pid stop')
129139
@cli.stop
130140
end
131141

0 commit comments

Comments
 (0)