我的捆绑命令无法加载到部署在 Heroku 中的 Sinatra 应用程序,因此导致它崩溃
My bundler command is failing to load in my Sinatra app deployed in Heroku and thus causes it to crash
我正在尝试将一个非常基本的 Sinatra 应用程序成功部署到 Heroku。我可以在本地 运行 这个应用程序。 ruby 代码本身非常简单:
require 'sinatra'
get '/' do
'Hello World!'
end
我添加了一个正确的 Gemfile:
source 'https://rubygems.org'
gem 'sinatra'
gem 'rack'
还有一个配置文件:
require './hello_app'
run SinatraApp
此代码在 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
然而,当我部署它并实际访问该站点时,它崩溃了,并在日志中显示以下内容:
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
我的 2 个问题是:
- 为什么会这样?
- 如何修复它以便应用程序在 Heroku 上成功加载?
发现的问题
您有很多问题,但最大的问题是您试图 run SinatraApp
而不是 run Sinatra::Application
。这很可能是导致应用程序崩溃的原因,正确的调用在 Heroku 和 Sinatra 文档中。
此外,Sinatra README recommends using the thin web server. Heroku recommends using a Procfile 明确定义了大多数基于 Ruby 的应用程序的 Web 服务器调用。具体来说,它说:
Regardless of the webserver you choose, production apps should always specify the webserver explicitly in the Procfile.
下面,我为 Sinatra 应用程序提供了我自己的建议配置,与 Heroku 文档中提供的配置相比,它(稍微)没有那么简约。从那里开始,然后调整它以适应。
使用 Foreman Procfile 在 Heroku 上启动 Sinatra
首先,确保您的应用程序的 Heroku 堆栈包含 heroku/ruby
buildpack。然后,使用瘦网络服务器使用 foreman Procfile 启动您的 Sinatra 应用程序。例如:
# 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"
您可能可以让它与其他配置一起使用,包括 minimalist one suggested by Heroku,但此设置对于 Heroku 上的大量 Sinatra 应用程序可靠地工作。您使用其他配置的里程(和错误数量)可能会有所不同。
我正在尝试将一个非常基本的 Sinatra 应用程序成功部署到 Heroku。我可以在本地 运行 这个应用程序。 ruby 代码本身非常简单:
require 'sinatra'
get '/' do
'Hello World!'
end
我添加了一个正确的 Gemfile:
source 'https://rubygems.org'
gem 'sinatra'
gem 'rack'
还有一个配置文件:
require './hello_app'
run SinatraApp
此代码在 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
然而,当我部署它并实际访问该站点时,它崩溃了,并在日志中显示以下内容:
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
我的 2 个问题是:
- 为什么会这样?
- 如何修复它以便应用程序在 Heroku 上成功加载?
发现的问题
您有很多问题,但最大的问题是您试图 run SinatraApp
而不是 run Sinatra::Application
。这很可能是导致应用程序崩溃的原因,正确的调用在 Heroku 和 Sinatra 文档中。
此外,Sinatra README recommends using the thin web server. Heroku recommends using a Procfile 明确定义了大多数基于 Ruby 的应用程序的 Web 服务器调用。具体来说,它说:
Regardless of the webserver you choose, production apps should always specify the webserver explicitly in the Procfile.
下面,我为 Sinatra 应用程序提供了我自己的建议配置,与 Heroku 文档中提供的配置相比,它(稍微)没有那么简约。从那里开始,然后调整它以适应。
使用 Foreman Procfile 在 Heroku 上启动 Sinatra
首先,确保您的应用程序的 Heroku 堆栈包含 heroku/ruby
buildpack。然后,使用瘦网络服务器使用 foreman Procfile 启动您的 Sinatra 应用程序。例如:
# 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"
您可能可以让它与其他配置一起使用,包括 minimalist one suggested by Heroku,但此设置对于 Heroku 上的大量 Sinatra 应用程序可靠地工作。您使用其他配置的里程(和错误数量)可能会有所不同。