Monday, July 24, 2017

The transfer from development to production using Heroku

The first attempt was failed when I tried to push all the development codes from all 14 chapters to Heroku. When I digged into Heroku logs file, it did not specific the error. In Heroku log file, it stated "Heroku deployment error H10 App crashed" and it did not explain the details.

In order to make the development version could be applied to Heroku, I had to revert back the project version to chapter 3: "Finish static pages" and I replaced the original Gemfile that was regenerated automatically by Ruby on Rails new project command with Michael Hartl's customized Gemfile for Heroku that listed on Listing 3.2: A Gemfile for the sample app.

The production version had completed the first 10 chapters of Michael Hartl's book. As of time of writing, these files had to be changed in order to make it to deploy to Heroku production environment successfully.

sample_app_heroku_2/Gemfile
source 'https://rubygems.org'

gem 'rails',        '5.1.2'
gem 'bcrypt',         '3.1.11'
gem 'faker',          '1.7.3'
gem 'will_paginate',           '3.1.5'
gem 'bootstrap-will_paginate', '1.0.0'
gem 'bootstrap-sass', '3.3.7'
gem 'puma',         '3.9.1'
gem 'sass-rails',   '5.0.6'
gem 'uglifier',     '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks',   '5.0.1'
gem 'jbuilder',     '2.7.0'

group :development, :test do
  gem 'sqlite3', '1.3.13'
  gem 'byebug',  '9.0.6', platform: :mri
end

group :development do
  gem 'web-console',           '3.5.1'
  gem 'listen',                '3.0.8'
  gem 'spring',                '2.0.2'
  gem 'spring-watcher-listen', '2.0.1'
end

group :test do
  gem 'rails-controller-testing', '1.0.2'
  gem 'minitest-reporters',       '1.1.14'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
end

group :production do
  gem 'pg', '0.18.4'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

sample_app_heroku_2/config/environments/production.rb
  config.force_ssl = true

sample_app_heroku_2/config/puma.rb
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  # Worker specific setup for Rails 4.1+
  # See: https://devcenter.heroku.com/articles/
  # deploying-rails-applications-with-the-puma-web-server#on-worker-boot
  ActiveRecord::Base.establish_connection
end

sample_app_heroku_2/Procfile
web: bundle exec puma -C config/puma.rb
 
On the other hand, I made small changes to the View files and Database Seed file for personalization.

sample_app_heroku_2/app/views/static_pages/home.html.erb
<div class="center jumbotron">
  <h1>Welcome to the Jimmy App</h1>

  <h2>
    This is the home page for the
    <a href="http://www.railstutorial.org/">Ruby on Rails Tutorial</a>
    sample application.
  </h2>

  <%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
</div>

<%= link_to image_tag("rails.png", alt: "Rails logo"),
            'http://rubyonrails.org/' %>

sample_app_heroku_2/app/views/layouts/_header.html.erb
<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="container">
    <%= link_to "jimmy app", root_path, id: "logo" %>
    <nav>
      <ul class="nav navbar-nav navbar-right">
        <li><%= link_to "Home",   root_path %></li>
        <li><%= link_to "Help",   help_path %></li>       
        <% if logged_in? %>         
          <li><%= link_to "Users", users_path %></li>                   
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
              Account <b class="caret"></b>
            </a>
            <ul class="dropdown-menu">
              <li><%= link_to "Profile", current_user %></li>
              <li><%= link_to "Settings", edit_user_path(current_user) %></li>
              <li class="divider"></li>
              <li>
                <%= link_to "Log out", logout_path, method: :delete %>
              </li>
            </ul>
          </li>
        <% else %>
          <li><%= link_to "Log in", login_path %></li>
        <% end %>
         
      </ul>
    </nav>
  </div>
</header>

sample_app_heroku_2/db/seeds.rb
User.create!(name:  "Example User",
             email: "example@railstutorial.org",
             password:              "secret",
             password_confirmation: "secret",
             admin: true)

99.times do |n|
  name  = Faker::Name.name
  email = "example-#{n+1}@railstutorial.org"
  password = "password"
  User.create!(name:  name,
               email: email,
               password:              password,
               password_confirmation: password)
end

* The Github repository for the Heroku production is https://github.com/jimmy2046/sample_app_heroku_2

* The production website in Heroku is https://warm-ravine-91766.herokuapp.com/

No comments:

Post a Comment

How to kill an abandoned process in Linux/Unix

I remembered it, then I forgot, then I remembered it, and then I forgot again. In case of a Linux/Unit process hang, I have to figure out ...