nginx / unit

NGINX Unit - universal web app server - a lightweight and versatile open source server that simplifies the application stack by natively executing application code across eight different programming language runtimes.
https://unit.nginx.org
Apache License 2.0
5.39k stars 327 forks source link

Ruby: Failed to parse rack script #574

Open kamchatkin opened 3 years ago

kamchatkin commented 3 years ago

Hi!

I can't get the project to run on rails under the unit. First all the steps were done under debian 10.10, the project started, but ubuntu project does not start. Can anyone help?


$ lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:    20.04
Codename:   focal

$ ruby --version

ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]

How I set up the unit:

$ git clone https://github.com/nginx/unit
$ cd unit
$ git checkout 1.25.0-1
$ git status
  HEAD detached at 1.25.0-1
$ ./configure --prefix=/usr --state=/var/lib/unit --no-unix-sockets --pid=/var/run/unit.pid --log=/var/log/unit.log --tmp=/var/tmp --user=unit --group=unit --tests --openssl --modules=/usr/lib/unit/modules --no-ipv6 --control=127.0.0.1:8443
  ...
$ ./configure ruby
  configuring Ruby module
  checking for Ruby library ... not found
  checking for Ruby library in /home/nk/.rvm/rubies/ruby-2.2.3/lib ... found
  checking for Ruby version ... 2.2.3
   + Ruby module: ruby.unit.so
$ make
$ sudo make install
$ sudo make ruby-install

$ unitd --version

unit version: 1.25.0
configured as ./configure --prefix=/usr --state=/var/lib/unit --no-unix-sockets --pid=/var/run/unit.pid --log=/var/log/unit.log --tmp=/var/tmp --user=unit --group=unit --tests --openssl --modules=/usr/lib/unit/modules --no-ipv6 --control=127.0.0.1:8443

$ sudo unitd

2021/08/30 20:15:58 [info] 33821#33821 discovery started
2021/08/30 20:15:58 [notice] 33821#33821 module: ruby 2.2.3 "/usr/lib/unit/modules/ruby.unit.so"
2021/08/30 20:15:58 [info] 33819#33819 controller started
2021/08/30 20:15:58 [notice] 33819#33819 process 33821 exited with code 0
2021/08/30 20:15:58 [info] 33829#33829 router started
2021/08/30 20:15:58 [info] 33829#33829 OpenSSL 1.1.1f  31 Mar 2020, 1010106f

Next, I start the application

$ cat current/config.ru

# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment', __FILE__)
run Rails.application

$ cat unit.json

{
  "type":"ruby",
  "processes":6,
  "script":"config.ru",
  "working_directory":"/opt/rails/current/",
  "user":"nk",
  "group":"nk",
  "environment":{
    "GEM_HOME":"/home/nk/.rvm/gems/ruby-2.2.3@rails",
    "GEM_PATH":"/home/nk/.rvm/gems/ruby-2.2.3@rails:/home/nk/.rvm/gems/ruby-2.2.3@global",
    "RAILS_ENV":"production"
  }
}

$ curl -X PUT --data-binary @unit.json localhost:8443/config/applications/rails

{
    "error": "Failed to apply new configuration."
}

$ tail -f unit.log

2021/08/30 20:16:31 [info] 33847#33847 "rails" application started
2021/08/30 20:16:32 [alert] 33847#33847 [unit] Ruby: Failed to parse rack script
2021/08/30 20:16:32 [alert] 33847#33847 [unit] Ruby: /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/activesupport-4.2.4/lib/active_support/inflector/inflections.rb:106:in `is_a?': class or module required (TypeError)
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/activesupport-4.2.4/lib/active_support/inflector/inflections.rb:106:in `plural'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/activesupport-4.2.4/lib/active_support/inflections.rb:11:in `block in <module:ActiveSupport>'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/activesupport-4.2.4/lib/active_support/inflector/inflections.rb:205:in `inflections'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/activesupport-4.2.4/lib/active_support/inflections.rb:10:in `<module:ActiveSupport>'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/activesupport-4.2.4/lib/active_support/inflections.rb:9:in `<top (required)>'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:3:in `require'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/activesupport-4.2.4/lib/active_support/inflector/methods.rb:3:in `<top (required)>'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/activesupport-4.2.4/lib/active_support/dependencies/autoload.rb:1:in `require'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/activesupport-4.2.4/lib/active_support/dependencies/autoload.rb:1:in `<top (required)>'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/activesupport-4.2.4/lib/active_support.rb:25:in `require'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/activesupport-4.2.4/lib/active_support.rb:25:in `<top (required)>'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/railties-4.2.4/lib/rails.rb:5:in `require'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/railties-4.2.4/lib/rails.rb:5:in `<top (required)>'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/railties-4.2.4/lib/rails/all.rb:1:in `require'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/railties-4.2.4/lib/rails/all.rb:1:in `<top (required)>'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /opt/rails/releases/20210809135846/config/application.rb:3:in `require'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /opt/rails/releases/20210809135846/config/application.rb:3:in `<top (required)>'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /opt/rails/releases/20210809135846/config/environment.rb:2:in `require'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /opt/rails/releases/20210809135846/config/environment.rb:2:in `<top (required)>'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from config.ru:3:in `require'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from config.ru:3:in `block in <main>'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/rack-1.6.13/lib/rack/builder.rb:55:in `instance_eval'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/rack-1.6.13/lib/rack/builder.rb:55:in `initialize'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from config.ru:in `new'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from config.ru:in `<main>'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/rack-1.6.13/lib/rack/builder.rb:49:in `eval'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/rack-1.6.13/lib/rack/builder.rb:49:in `new_from_string'
2021/08/30 20:16:32 [alert] 33847#33847 [unit] from /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/rack-1.6.13/lib/rack/builder.rb:40:in `parse_file'
2021/08/30 20:16:32 [notice] 33819#33819 process 33847 exited with code 1
2021/08/30 20:16:32 [warn] 33829#33829 failed to start application "rails"
2021/08/30 20:16:32 [alert] 33829#33829 failed to apply new conf

$ rails -v

Rails 4.2.4

Any ideas what might be the problem?

VBart commented 3 years ago

Does it work without Unit using rackup?

rackup config.ru
kamchatkin commented 3 years ago

Yes, the application runs successfully.

$ rackup config.ru 
I, [2021-09-06T13:22:12.566046 #110723]  INFO -- sentry: ** [Raven] Raven 2.11.0 ready to catch errors
DEPRECATION WARNING: Sprockets method `register_engine` is deprecated.
Please register a mime type using `register_mime_type` then
use `register_compressor` or `register_transformer`.
https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#supporting-all-versions-of-sprockets-in-processors
 (called from block (2 levels) in <class:Railtie> at /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/sass-rails-5.0.4/lib/sass/rails/railtie.rb:57)
DEPRECATION WARNING: Sprockets method `register_engine` is deprecated.
Please register a mime type using `register_mime_type` then
use `register_compressor` or `register_transformer`.
https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#supporting-all-versions-of-sprockets-in-processors
 (called from block (2 levels) in <class:Railtie> at /home/nk/.rvm/gems/ruby-2.2.3@rails/gems/sass-rails-5.0.4/lib/sass/rails/railtie.rb:58)
[2021-09-06 13:22:13] INFO  WEBrick 1.3.1
[2021-09-06 13:22:13] INFO  ruby 2.2.3 (2015-08-18) [x86_64-linux]
[2021-09-06 13:22:13] INFO  WEBrick::HTTPServer#start: pid=110723 port=9292

It is now successfully run as follows:

$ bundle exec "unicorn -D -c /opt/rails/current/config/unicorn.rb -E production"
VBart commented 3 years ago

Could you provide a minimal app example, that fails? It seems the characters encodings are somehow involved here. At least looking to the relevant lines of Ruby-on-Rails sources alludes to it.

tippexs commented 2 years ago

Any update on this?

dmdin commented 2 years ago

I have the same problem. Trying to run it inside Docker Alpine container. This one works as expected, it finds config.ru

rackup config.ru

Unit: config.json

{
  "listeners": {
    "0.0.0.0:3000": {
      "pass": "routes"
    }
  },
  "routes": [
    {
      "action": {
        "share": "/home/app/public$uri",
        "fallback": {
          "pass": "applications/rails"
        }
      }
    }
  ],
  "applications": {
    "rails": {
      "type": "ruby",
      "working_directory": "/home/app/",
      "script": "config.ru"
    }
  }
}

But configuration using this script throws the error

img