Skip to content

Commit 871ad4c

Browse files
authored
Merge pull request #583 from PecanProject/rails5-unicorn
Rails5 unicorn
2 parents ec7416f + a9b64d5 commit 871ad4c

File tree

11 files changed

+231
-46
lines changed

11 files changed

+231
-46
lines changed

Dockerfile

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
FROM ruby:2.1
1+
FROM ruby:2.3
22
MAINTAINER Max Burnette <[email protected]>
33

44
# Install dependencies
55
RUN apt-get update \
66
&& apt-get install --no-install-recommends -y \
77
curl \
88
git \
9+
libgeos-dev \
910
netcat \
11+
nodejs \
1012
postgresql-client \
1113
&& rm -rf /var/lib/apt/lists/*
1214

@@ -15,15 +17,17 @@ WORKDIR /home/bety
1517

1618
# install gems (allowing for caching)
1719
COPY /Gemfile* /home/bety/
18-
RUN gem install bundler && \
19-
bundle install --without javascript_testing --path vendor/bundle
20+
RUN gem install bundler \
21+
&& bundle install --with docker --without "test development production debug javascript_testing"
22+
2023

2124
# port that is exposed (standard ruby port)
22-
EXPOSE 3000
25+
EXPOSE 8000
2326

2427
# copy rest of the files
2528
COPY / /home/bety
2629
COPY /docker/database.yml /home/bety/config/database.yml
30+
COPY /docker/config.ru /home/bety/config.ru
2731

2832
# download dump.bety and load.bety scripts and configure app
2933
RUN curl -LOs https://raw.githubusercontent.com/PecanProject/pecan/master/scripts/load.bety.sh \
@@ -41,11 +45,18 @@ ARG BETY_GIT_DATE="unknown"
4145
ENV LOCAL_SERVER=99 \
4246
REMOTE_SERVERS="0 1 2 5" \
4347
RAILS_ENV="production" \
48+
RAILS_RELATIVE_URL_ROOT="" \
49+
SECRET_KEY_BASE="ThisIsNotReallySuchAGreatSecret" \
50+
UNICORN_WORKER_PROCESSES="3" \
51+
UNICORN_PORT="8000" \
4452
BETY_GIT_TAGS=${BETY_GIT_TAGS} \
4553
BETY_GIT_BRANCH=${BETY_GIT_BRANCH} \
4654
BETY_GIT_CHECKSUM=${BETY_GIT_CHECKSUM} \
4755
BETY_GIT_DATE=${BETY_GIT_DATE}
4856

57+
# expose public files
58+
VOLUME ["/home/bety/public"]
59+
4960
# default command to run bety web-app
5061
ENTRYPOINT ["/home/bety/docker/entrypoint.sh"]
51-
CMD ["server"]
62+
CMD ["unicorn"]

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ group :production do
9191
gem "passenger", "5.1.12"
9292
end
9393

94+
group :docker do
95+
gem 'unicorn'
96+
end
97+
9498
# API-related Gems:
9599

96100
gem "rspec_api_documentation"

Gemfile.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ GEM
9999
json (2.1.0)
100100
json-schema (2.8.0)
101101
addressable (>= 2.4)
102+
kgio (2.11.2)
102103
loofah (2.2.2)
103104
crass (~> 1.0.2)
104105
nokogiri (>= 1.5.9)
@@ -164,6 +165,7 @@ GEM
164165
method_source
165166
rake (>= 0.8.7)
166167
thor (>= 0.18.1, < 2.0)
168+
raindrops (0.19.0)
167169
rake (12.3.1)
168170
rb-fsevent (0.10.3)
169171
rb-inotify (0.9.10)
@@ -232,6 +234,9 @@ GEM
232234
thread_safe (~> 0.1)
233235
uglifier (4.1.9)
234236
execjs (>= 0.3.0, < 3)
237+
unicorn (5.4.0)
238+
kgio (~> 2.6)
239+
raindrops (~> 0.7)
235240
websocket-driver (0.6.5)
236241
websocket-extensions (>= 0.1.0)
237242
websocket-extensions (0.1.3)
@@ -288,6 +293,10 @@ DEPENDENCIES
288293
trollop
289294
tzinfo
290295
uglifier
296+
unicorn
291297
will_paginate
292298
yajl-ruby (~> 1.3.1)
293299
yard
300+
301+
BUNDLED WITH
302+
1.16.1

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ This is the source code for the [Biofuel Ecophysiological Traits and Yields data
99
The website is primarily written in Ruby-on-Rails, and has a PostgreSQL backend.
1010
BETYdb provides an interface for contributing and accessing data, and is the informatics backend for the [Predictive Ecosystem Analyzer (PEcAn)](http://www.pecanproject.org).
1111

12+
## Running BETY using Docker
13+
14+
To get started with BETY you can use the docker-compose.yml file included. This will start the database (postgresql with postgis version 9.5) as well as the BETY container. If this is the first time you start it you will need to initialize the database, this can be done using the following commands:
15+
16+
```
17+
docker-compose -p bety up -d postgres
18+
docker run --rm --network bety_bety pecan/bety initialize
19+
```
20+
21+
Once bety finishes inializing the database, or to restart BETY, you can bring up the all the containers using:
22+
23+
```
24+
docker-compose -p bety up -d
25+
```
26+
27+
To change the path BETY runs under you can change the path using the environment variable RAILS_RELATIVE_URL_ROOT, for example to just run bety you can use the following command. This will precompile any of the static assets and run BETY.
28+
29+
```
30+
docker run -e RAILS_RELATIVE_URL_ROOT="/bety" pecan/bety
31+
```
32+
1233
## Documentation.
1334

1435
* Technical Documentation: https://pecan.gitbooks.io/betydb-documentation/content/

app/models/experiment.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Experiment < ActiveRecord::Base
2+
attr_protected []
23

34
extend SimpleSearch
45

app/views/layouts/application.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
<li><%= link_to "Citations", citations_path %></li>
179179
<li><%= link_to "Covariates", covariates_path %></li>
180180
<li><%= link_to "Cultivars", cultivars_path %></li>
181+
<li><%= link_to "Experiments", experiments_path %></li>
181182
<li><%= link_to "Managements", managements_path %></li>
182183
<% if current_user.page_access_level <= 2 %>
183184
<li><%= link_to "Methods", methods_path %></li>

config/unicorn.rb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Sample verbose configuration file for Unicorn (not Rack)
2+
#
3+
# This configuration file documents many features of Unicorn
4+
# that may not be needed for some applications. See
5+
# http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
6+
# for a much simpler configuration file.
7+
#
8+
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
9+
# documentation.
10+
11+
# Go with at least 1 per CPU core, a higher amount will usually help for fast
12+
# responses such as reading from a cache.
13+
worker_processes ENV['UNICORN_WORKER_PROCESSES'].to_i
14+
15+
# Help ensure your application will always spawn in the symlinked
16+
# "current" directory that Capistrano sets up.
17+
#working_directory "/home/bety" # available in 0.94.0+
18+
19+
# feel free to point this anywhere accessible on the filesystem
20+
#pid "/home/bety/tmp/pids/unicorn.pid"
21+
22+
# Listen on both a Unix domain socket and a TCP port.
23+
# If you are load-balancing multiple Unicorn masters, lower the backlog
24+
# setting to e.g. 64 for faster failover.
25+
#listen "/home/git/gitlab/tmp/sockets/gitlab.socket", :backlog => 1024
26+
#listen "127.0.0.1:8080", :tcp_nopush => true
27+
listen "0.0.0.0:" + ENV['UNICORN_PORT']
28+
29+
# nuke workers after 30 seconds instead of 60 seconds (the default)
30+
timeout 30
31+
32+
# By default, the Unicorn logger will write to stderr.
33+
# Additionally, some applications/frameworks log to stderr or stdout,
34+
# so prevent them from going to /dev/null when daemonized here:
35+
#stderr_path "/home/bety/log/unicorn.stderr.log"
36+
#stdout_path "/home/bety/log/unicorn.stdout.log"
37+
38+
# combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
39+
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
40+
preload_app true
41+
GC.respond_to?(:copy_on_write_friendly=) and
42+
GC.copy_on_write_friendly = true
43+
44+
# Enable this flag to have unicorn test client connections by writing the
45+
# beginning of the HTTP headers before calling the application. This
46+
# prevents calling the application for connections that have disconnected
47+
# while queued. This is only guaranteed to detect clients on the same
48+
# host unicorn runs on, and unlikely to detect disconnects even on a
49+
# fast LAN.
50+
check_client_connection false
51+
52+
before_fork do |server, worker|
53+
# the following is highly recommended for Rails + "preload_app true"
54+
# as there's no need for the master process to hold a connection
55+
defined?(ActiveRecord::Base) and
56+
ActiveRecord::Base.connection.disconnect!
57+
58+
# The following is only recommended for memory/DB-constrained
59+
# installations. It is not needed if your system can house
60+
# twice as many worker_processes as you have configured.
61+
#
62+
# This allows a new master process to incrementally
63+
# phase out the old master process with SIGTTOU to avoid a
64+
# thundering herd (especially in the "preload_app false" case)
65+
# when doing a transparent upgrade. The last worker spawned
66+
# will then kill off the old master process with a SIGQUIT.
67+
old_pid = "#{server.config[:pid]}.oldbin"
68+
if old_pid != server.pid
69+
begin
70+
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
71+
Process.kill(sig, File.read(old_pid).to_i)
72+
rescue Errno::ENOENT, Errno::ESRCH
73+
end
74+
end
75+
#
76+
# Throttle the master from forking too quickly by sleeping. Due
77+
# to the implementation of standard Unix signal handlers, this
78+
# helps (but does not completely) prevent identical, repeated signals
79+
# from being lost when the receiving process is busy.
80+
# sleep 1
81+
end
82+
83+
after_fork do |server, worker|
84+
# per-process listener ports for debugging/admin/migrations
85+
# addr = "127.0.0.1:#{9293 + worker.nr}"
86+
# server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
87+
88+
# the following is *required* for Rails + "preload_app true",
89+
defined?(ActiveRecord::Base) and
90+
ActiveRecord::Base.establish_connection
91+
92+
# if preload_app is true, then you may also want to check and
93+
# restart any other shared sockets/descriptors such as Memcached,
94+
# and Redis. TokyoCabinet file handles are safe to reuse
95+
# between any number of forked children (assuming your kernel
96+
# correctly implements pread()/pwrite() system calls)
97+
end

docker-compose.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
version: "3"
2+
3+
services:
4+
# BETY rails frontend to the database
5+
bety:
6+
image: pecan/bety:unicorn
7+
networks:
8+
- bety
9+
ports:
10+
- 8000
11+
environment:
12+
- UNICORN_WORKER_PROCESSES=1
13+
- SECRET_KEY_BASE=thisissomereallllllylongsecretkeyandshouldbelongerthanthis
14+
depends_on:
15+
- postgres
16+
restart: unless-stopped
17+
deploy:
18+
mode: replicated
19+
restart_policy:
20+
condition: any
21+
placement:
22+
constraints: [node.role == manager]
23+
24+
# postgresql + postgis to hold all the data
25+
postgres:
26+
image: mdillon/postgis:9.5
27+
networks:
28+
- bety
29+
#ports:
30+
# - 5432
31+
volumes:
32+
- postgres:/var/lib/postgresql/data
33+
restart: unless-stopped
34+
deploy:
35+
mode: replicated
36+
restart_policy:
37+
condition: any
38+
placement:
39+
constraints: [node.role == worker]
40+
41+
networks:
42+
bety:
43+
44+
volumes:
45+
postgres:
46+

docker/config.ru

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file is used by Rack-based servers to start the application.
2+
3+
require ::File.expand_path('../config/environment', __FILE__)
4+
5+
map (ENV['RAILS_RELATIVE_URL_ROOT'] || "") + "/" do
6+
run BetyRails3::Application
7+
end

docker/docker-compose.yml

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

0 commit comments

Comments
 (0)