My bundler command is failing to load in my Sinatra app deployed in Heroku and thus causes it to crash

 ̄綄美尐妖づ 提交于 2021-01-28 22:20:09

问题


I am trying to successfully deploy an extremely rudimentary Sinatra app to Heroku. I am able to run this app locally. The ruby code itself is incredibly simple:

require 'sinatra'

get '/' do
  'Hello World!'
end

I added a proper Gemfile:

source 'https://rubygems.org'
gem 'sinatra'
gem 'rack'

As well as a configuration file:

require './hello_app'
run SinatraApp

This code builds successfully on Heroku:

-----> Ruby app detected
-----> Installing bundler 2.1.4
-----> Removing BUNDLED WITH version in the Gemfile.lock
-----> Compiling Ruby/Rack
-----> Using Ruby version: ruby-2.6.6
-----> Installing dependencies using bundler 2.1.4
       Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
       Using bundler 2.1.4
       Using ruby2_keywords 0.0.2
       Using mustermann 1.1.1
       Using rack 2.2.3
       Using rack-protection 2.1.0
       Using tilt 2.0.10
       Using sinatra 2.1.0
       Bundle complete! 2 Gemfile dependencies, 7 gems now installed.
       Gems in the groups development and test were not installed.
       Bundled gems are installed into `./vendor/bundle`
       Bundle completed (0.49s)
       Cleaning up the bundler cache.
-----> Detecting rake tasks
###### WARNING:
       You have not declared a Ruby version in your Gemfile.
       
       To declare a Ruby version add this line to your Gemfile:
       
       ```
       ruby "2.6.6"
       ```
       
       For more information see:
         https://devcenter.heroku.com/articles/ruby-versions
###### WARNING:
       No Procfile detected, using the default web server.
       We recommend explicitly declaring how to boot your server process via a Procfile.
       https://devcenter.heroku.com/articles/ruby-default-web-server
-----> Discovering process types
       Procfile declares types     -> (none)
       Default types for buildpack -> console, rake, web
-----> Compressing...
       Done: 13.5M
-----> Launching...
       Released v6
       https://arcane-depths-40341.herokuapp.com/ deployed to Heroku

However when I deploy it and actually visit the site it crashes and the following is displayed in the log:

2020-12-22T14:04:23.100758+00:00 heroku[web.1]: State changed from crashed to starting
2020-12-22T14:04:24.857234+00:00 heroku[web.1]: Starting process with command `bundle exec rackup config.ru -p ${PORT:-5000}`
2020-12-22T14:04:28.910617+00:00 app[web.1]: bundler: failed to load command: rackup (/app/vendor/bundle/ruby/2.6.0/bin/rackup)
2020-12-22T14:04:28.910647+00:00 app[web.1]: Gem::Exception: can't find executable rackup for gem rack. rack is not currently included in the bundle, perhaps you meant to add it to your Gemfile?
2020-12-22T14:04:28.910648+00:00 app[web.1]: /app/vendor/bundle/ruby/2.6.0/gems/bundler-2.1.4/lib/bundler/rubygems_integration.rb:374:in `block in replace_bin_path'
2020-12-22T14:04:28.910648+00:00 app[web.1]: /app/vendor/bundle/ruby/2.6.0/gems/bundler-2.1.4/lib/bundler/rubygems_integration.rb:402:in `block in replace_bin_path'
2020-12-22T14:04:28.910648+00:00 app[web.1]: /app/vendor/bundle/ruby/2.6.0/bin/rackup:23:in `<top (required)>'
2020-12-22T14:04:29.001978+00:00 heroku[web.1]: Process exited with status 1
2020-12-22T14:04:29.106041+00:00 heroku[web.1]: State changed from starting to crashed
2020-12-22T18:56:39.441582+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=arcane-depths-40341.herokuapp.com request_id=875e2357-d1f9-4457-ab68-3d6a76bea281 fwd="98.13.129.57" dyno= connect= service= status=503 bytes= protocol=https

My 2 questions are:

  1. Why is this happening?
  2. How do it fix it such that the app successfully loads on Heroku?

回答1:


Identified Problems

You have a number of issues, but the largest is that you're attempting to run SinatraApp rather than run Sinatra::Application. This is most likely what's causing the app to crash, and the correct invocation is in both the Heroku and Sinatra documentation.

Furthermore, the Sinatra README recommends using the thin web server. Heroku recommends using a Procfile that explicitly defines the web server invocation for most Ruby-based apps. Specifically, it says:

Regardless of the webserver you choose, production apps should always specify the webserver explicitly in the Procfile.

Below, I provide my own suggested configuration for Sinatra apps that's (very slightly) less minimalist than the one provided in the Heroku docs. Start there, then tune it to suit.

Use a Foreman Procfile to Start Sinatra on Heroku

First, make sure your application's Heroku stack includes the heroku/ruby buildpack. Then, use a foreman Procfile to start your Sinatra app using the thin web server. For example:

# Gemfile

ruby '2.6.6'
source 'https://rubygems.org'

gem 'sinatra'
gem 'thin'
gem 'foreman'
# config.ru
require './hello_app'
run Sinatra::Application
# Procfile

dev: bundle exec rackup
web: APP_ENV=production bundle exec rackup -p "$PORT"

You can probably get it working with other configurations, including the minimalist one suggested by Heroku, but this setup works reliably for a broad number of Sinatra applications on Heroku. Your mileage (and number of errors) with other configurations may vary.



来源:https://stackoverflow.com/questions/65414701/my-bundler-command-is-failing-to-load-in-my-sinatra-app-deployed-in-heroku-and-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!