Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahajanabhij/52734b568f85750a0c23 to your computer and use it in GitHub Desktop.
Save mahajanabhij/52734b568f85750a0c23 to your computer and use it in GitHub Desktop.
ActionController::Live puma + nginx in 2 minutes
Start Puma
RAILS_ENV=development bundle exec puma -C ./config/puma.rb
curl -i http://localhost:3001/events/141/check
output:
data: {"time":"2014-05-25 16:41:39"}
data: {"time":"2014-05-25 16:41:40"}
data: {"time":"2014-05-25 16:41:41"}
data: {"time":"2014-05-25 16:41:42"}
data: {"time":"2014-05-25 16:41:43"}
done!!
#model/concerns/reloader/comment_posted.rb
require 'json'
module Reloader
class CommentPosted
def initialize io
@io = io
end
def write object, options = {}
options.each do |k,v|
@io.write "#{k}: #{v}\n"
end
@io.write "data: #{JSON.dump(object)}\n\n"
end
def close
@io.close
end
end
end
include Reloader
include ActionController::Live
def check
# SSE expects the `text/event-stream` content type
response.headers['Content-Type'] = 'text/event-stream'
sse = Reloader::CommentPosted.new(response.stream)
begin
loop do
sse.write({ :time => Time.now })
sleep 1
end
rescue IOError
# When the client disconnects, we'll get an IOError on write
ensure
sse.close
end
end
#user nobody;
worker_processes 2;
worker_rlimit_nofile 8192;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
client_max_body_size 10M;
passenger_root /home/usr/.rvm/gems/ruby-2.1.1/gems/passenger-4.0.37;
passenger_ruby /home/usr/.rvm/gems/ruby-2.1.1/wrappers/ruby;
passenger_app_env development;
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
upstream appname {
server unix:///home/usr/sites/site/tmp/puma/appname-puma.sock;
}
server {
listen 80;
listen 3001 default_server;
server_name usr.site.com;
passenger_ruby /home/usr/.rvm/gems/ruby-2.1.1/wrappers/ruby;
location / {
root /home/usr/sites/sites/public;
passenger_enabled on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
#config/puma.rb
#!/usr/bin/env puma
# start puma with:
# RAILS_ENV=production bundle exec puma -C ./config/puma.rb
require 'pathname'
APP_PATH = Pathname.new('/home/dir/sites/')
CURRENT = APP_PATH.join('app_dir')
SHARED = APP_PATH.join('app_dir')
socket = "unix://" + CURRENT.join('tmp/sockets/puma.sock').to_s
# See Puma::Configuration::DSL
bind socket
pidfile SHARED.join("pids/puma.pid")
threads 1, 16
state_path SHARED.join('pids/puma.state')
directory CURRENT
rackup "config.ru"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment