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

Add on_booted event #2709

Merged
merged 2 commits into from Feb 20, 2023
Merged

Add on_booted event #2709

merged 2 commits into from Feb 20, 2023

Conversation

rodzyn
Copy link
Contributor

@rodzyn rodzyn commented Sep 22, 2021

Description

Closes #2635

I'm not sure if exactly that is what was intended. But I'm happy to get a feedback :)

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.

lib/puma/dsl.rb Outdated Show resolved Hide resolved
lib/puma/single.rb Outdated Show resolved Hide resolved
@nateberkopec
Copy link
Member

Thank you so much for your first contrib to Puma! Welcome to the team!

@rodzyn
Copy link
Contributor Author

rodzyn commented Sep 22, 2021

Thank you so much for your first contrib to Puma! Welcome to the team!

Thanks for a warm welcome!

@rodzyn
Copy link
Contributor Author

rodzyn commented Sep 23, 2021

@nateberkopec One simple way that it could be achieved is to give a possibility for DSLs to have access to the events object. I did that in the latest commit

@nateberkopec nateberkopec added feature waiting-for-review Waiting on review from anyone labels Sep 23, 2021
Copy link
Contributor

@jacobherrington jacobherrington left a comment

Choose a reason for hiding this comment

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

I don't have much useful guidance on the implementation, but I was able to make the test you added pass with a change that might help you find the right path for this feature.

lib/puma/dsl.rb Outdated Show resolved Hide resolved
test/test_config.rb Outdated Show resolved Hide resolved
@nateberkopec nateberkopec added waiting-for-changes Waiting on changes from the requestor and removed waiting-for-review Waiting on review from anyone labels Dec 12, 2021
@nateberkopec
Copy link
Member

Needs a test and I think it's good to go?

lib/puma/dsl.rb Outdated
@@ -588,6 +592,17 @@ def after_worker_fork(&block)
@options[:after_worker_fork] << block
end

# Code to run after puma is booted in a single mode
#
# @note Single mode only.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this would be Single mode only? Cluster mode also calls fire(:on_booted)

Copy link
Member

Choose a reason for hiding this comment

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

Johnny is correct:

@launcher.events.fire_on_booted!
@events.fire_on_booted!

I guess the docs was written like this because the request (#2635, #1230 (comment)) came from a user using single mode where after_worker_boot (aliased to after_worker_fork) doesn't exist.

@launcher.config.run_hooks :after_worker_fork, idx, @launcher.events

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this, correct it runs for both, clustered and single mode, I've changed the description. The question is, what would be the expected behavior? At the end the name of the event is on_booted so I think it makes sense to be run on both: single and clutered. Any thoughts @nateberkopec ?

lib/puma/dsl.rb Outdated
@@ -251,6 +251,10 @@ def bind_to_activated_sockets(bind=true)
@options[:bind_to_activated_sockets] = bind
end

def events(events)
@options[:events] = events
end
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to add events to the DSL? Should we document it? Looks like some kind of hack to me? :) Should it be a public thing in the DSL?

Copy link
Member

Choose a reason for hiding this comment

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

Should the other DSL objects also have access to @events?

@user_dsl = DSL.new(@options.user_options, self)
@file_dsl = DSL.new(@options.file_options, self)
@default_dsl = DSL.new(@options.default_options, self)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was long time ago, but I think it was result of this discussion: #2709 (comment)

It was made as a temporary measure, to be refactored later I guess. I'm not sure if that is still right approach

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed the behavior, so now there is no need for this method and the Puma::Events instance is passed early to Puma::Configuration so it can be used by any type of DSL if needed

@johnnyshields
Copy link
Contributor

We just merged #2798 which affects events, so please pull latest master to this branch.

@axos88
Copy link

axos88 commented Jul 19, 2022

Can this be merged? I also need this functionality.

@rodzyn
Copy link
Contributor Author

rodzyn commented Jul 30, 2022

Sorry for abandoning this work for a long time. I had a very busy personal life in the past few months. I'm back with more time so I did a rebase. I will also try to look once again into a solution.

@NickLarsenNZ
Copy link

Any update on this?

@indirect
Copy link

indirect commented Nov 8, 2022

If you don't have multiple processes, can't you just... run your code directly in puma.rb? For example, it seems like I have embedded Sidekiq working in a single non-worker Puma process like this:

# [the default rails puma.rb file goes here]

require "sidekiq"
require_relative "initializers/sidekiq"

sidekiq = Sidekiq.configure_embed do |config|
  # config.logger.level = Logger::DEBUG
  config.queues = %w[critical default low]
  config.concurrency = 2
end
sidekiq.run

at_exit do
  sidekiq&.stop
end

@axos88
Copy link

axos88 commented Jan 12, 2023

Not necessarily. My use case involves running both single process and multi process based on environment. And I need the on booted event to fire correctly in both cases.

@rodzyn rodzyn requested a review from dentarg January 23, 2023 04:53
@nateberkopec
Copy link
Member

Looks like you've done a git-whoopsie and deleted your test.

Copy link
Member

@nateberkopec nateberkopec left a comment

Choose a reason for hiding this comment

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

Fix tests

@rodzyn rodzyn changed the title Add on_booted event for single mode Add on_booted event Jan 26, 2023
@rodzyn rodzyn requested review from nateberkopec and removed request for dentarg January 26, 2023 04:01
@nateberkopec nateberkopec added waiting-for-review Waiting on review from anyone and removed waiting-for-changes Waiting on changes from the requestor labels Jan 29, 2023
@rodzyn
Copy link
Contributor Author

rodzyn commented Feb 7, 2023

@nateberkopec @dentarg - feel free to take a look if you have time whether these changes make sense

@nateberkopec
Copy link
Member

nateberkopec commented Feb 20, 2023

Merged in 7c8cb02

@nateberkopec
Copy link
Member

Crud, I screwed up the merge. @MSP-Greg can you take a look and try? the changes to rack/handler meant that my merge didn't work.

@MSP-Greg
Copy link
Member

Looking at it now.

@MSP-Greg MSP-Greg reopened this Feb 20, 2023
@MSP-Greg
Copy link
Member

Test are running. I think the Windows mswin job will fail, it may be a recent change in Ruby master, re an error message. All the Actions master builds are done at different times, so they're not all the same commit in ruby/ruby...

I think the older msg is:

undefined method `to_i' for an instance of Array

newer:

undefined method `to_i' for [0, 1]:Array

@MSP-Greg MSP-Greg merged commit a61b078 into puma:master Feb 20, 2023
@MSP-Greg
Copy link
Member

Done...

@nateberkopec
Copy link
Member

🙇 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature waiting-for-review Waiting on review from anyone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow configuration of on_booted event hooks
9 participants