Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup code for use with rackup gem (may be used with rack 3) #3061

Merged
merged 4 commits into from Feb 12, 2023

Conversation

MSP-Greg
Copy link
Member

@MSP-Greg MSP-Greg commented Jan 20, 2023

Description

Rack v2 included the files needed to use rackup from the command line. With Rack v3, it is now a separate gem, and the namespace has changed.

Re tests, when Rack v3 was released, we (ok, I) blocked test/test_rack_handler from running with Rack v3, so tests do exist. This PR unblocks the tests.

The changes to lib/rack/handler/puma.rb look excessive. This PR is taking the previous code, and turning it into a HEREDOC, so it can be used in module_eval. This is the work-around to allow it to be placed in either the Rack::Handler or the Rackup::Handler namespace.

Adjust code to work with both.

Note: labeled as a 'bug', not sure...

Closes #3057

Your checklist for this pull request

  • I have reviewed the guidelines for contributing to this repository.
  • I have added (or updated) appropriate tests if this PR fixes a bug or adds a feature.
  • My pull request is 100 lines added/removed or less so that it can be easily reviewed.
  • If this PR doesn't need tests (docs change), I added [ci skip] to the title of the PR.
  • If this closes any issues, I have added "Closes #issue" to the PR description or my commit messages.
  • I have updated the documentation accordingly.
  • All new and existing tests passed, including Rubocop.

module Rack::Handler
def self.default(options = {})
Rack::Handler::Puma
if Object.const_defined? :Rackup

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this works, I think testing for Rack version 3 is more meaningful, because this tells the reader that this was implemented in response to a change in the new Rack version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I thought I responded. The rackup namespace needs to be defined. The Rack v3 gem could be loaded, but unless the rackup gem is also loaded, this will fail. I added a comment.

else
require 'rack/handler'
::Rack::Handler
end.module_eval <<'HEREDOC'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get rid of this module_eval we could create a new lib/rackup/handler.rb file that is required here. The file could either duplicate the code, or we are putting the code inside a module and are including this new module inside the new rackup/handler and rack/handler 🤔

Copy link
Member Author

@MSP-Greg MSP-Greg Jan 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cleanest way is with module_eval, but that also 'hides' the code from RuboCop, etc. I changed the code to use include. See the commit 'Update puma.rb, use include instead of module_eval'.

RubyGems had a somewhat similar issue, they extracted the code to a Ruby file that wasn't required (but was picked up by doc systems), and loaded a string with the file contents, then eval'd the string...

@MSP-Greg
Copy link
Member Author

A note for those who haven't looked at rack/rackup code, as of the current version of rackup (2.0.0), the code by default tries require 'rack/handler/puma' first, so the file path is fixed. If Puma has been activated, that require statement succeeds...

@nateberkopec nateberkopec added the waiting-for-changes Waiting on changes from the requestor label Jan 23, 2023
end
end
else
raise "Rack 3 must be used with the Rackup gem"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message could be something like you must install the rackup gem., i.e. tell the user what to do exactly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, see commit 'Changes per comments'


register :puma, Puma
# rackup was removed in Rack 3, it is now a separate gem
if Object.const_defined? :Rackup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just combine this with what's in rack_default? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. As mentioned above, Rackup currently will look for several servers, and Puma is the first one. It determines if Puma is available by requiring rack/handler/puma.

For info on rack_default, see #194.

require "rack/handler/puma"
module TestRackUp
if Rack::RELEASE < '3'
require "rack/handler/puma"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be moved out of the if statement because you use it in both branches

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, see commit 'Changes per comments'

else
require "rackup"
require "rack/handler/puma"
RACK_HANDLER_MOD = ::Rackup::Handler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could just test Puma::RackHandler directly for most of the tests instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two methods are called in RACK_HANDLER_MOD, register and get. If we want to verify that things work with Rack/Rackup, it would be good to continue doing so...

@MSP-Greg
Copy link
Member Author

Rebased

@nateberkopec nateberkopec merged commit 8092bf8 into puma:master Feb 12, 2023
@MSP-Greg MSP-Greg deleted the 00-rackup-2 branch February 12, 2023 05:20
@ioquatix
Copy link
Contributor

Just FYI, I expect the rackup gem to evolve more rapidly than it has before. While Rack v2 and Rackup gems are currently aligned, I imagine the handler implementation will diverge in the future (or be extended I compatible ways which are not part of the Rack 2 implementation).

severin added a commit to severin/puma that referenced this pull request May 15, 2023
The constant Rack::RELEASE was introduced only in rack version 2
(specifically: rack/rack@a2fe30a)
Earlier versions provided the class method Rack.release which still
exists.

puma#3061 has introduced usage of
Rack::RELEASE and therefore broke compatibility with rack 1.6.X.
@severin severin mentioned this pull request May 15, 2023
7 tasks
nateberkopec pushed a commit that referenced this pull request May 16, 2023
* Use Rack.release over Rack::RELEASE

The constant Rack::RELEASE was introduced only in rack version 2
(specifically: rack/rack@a2fe30a)
Earlier versions provided the class method Rack.release which still
exists.

#3061 has introduced usage of
Rack::RELEASE and therefore broke compatibility with rack 1.6.X.

* test.yaml, Gemfile - allow Rack 1.6.x CI

* lib/rack/handler/puma.rb - allow Rack 1.6.x

* test_rack_server.rb - allow testing with Rack 1.6.x

* test_puma_server_hijack.rb - allow testing with Rack 1.6.x

---------

Co-authored-by: MSP-Greg <Greg.mpls@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug waiting-for-changes Waiting on changes from the requestor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using rack 3. Receive deprecation warning.
4 participants