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

Avoid loading/rendering in Rails if SKIP_CONSULT is set #40

Merged
merged 1 commit into from
Apr 4, 2023

Conversation

aharpervc
Copy link
Contributor

@aharpervc aharpervc commented Mar 13, 2023

In certain situations, Consult might be loaded by Rails but you don't want it to process templates. For example, from a Rake task that doesn't load the Rails environment. To support those scenarios, you can run the task (etc) with SKIP_CONSULT=true and it won't do anything.

I don't think we can do an automated test in this repo for this feature, since otherwise we'd need a dummy Rails app. Adding that might be a good idea for the future, though.


Question: Is there a better way to do this?

@aharpervc aharpervc self-assigned this Mar 13, 2023
@@ -6,6 +6,8 @@ module Rails
# on app boot.
class Railtie < ::Rails::Railtie
config.before_configuration do
next if %w[1 true].include? ENV['SKIP_CONSULT'].to_s.downcase
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's a Rails-ism to do .present? (eg), but that seems worse than checking for a truthy value 🤔

@aharpervc aharpervc requested review from bvogelzang, sixfeetover and a team March 22, 2023 21:07
@aharpervc aharpervc marked this pull request as ready for review March 22, 2023 21:07
@sixfeetover
Copy link
Member

There's no functional difference, but I wonder about blocking the auto-load vs short circuiting the method. in other words, doing this:

require 'consult/rails/engine' if defined?(Rails) && ENV['SKIP_CONSULT'].blank?

https://github.com/veracross/consult/blob/master/lib/consult.rb#L103

@aharpervc
Copy link
Contributor Author

Good idea. Something I was considering is if we'll ever want to get between load and render. I was almost going to do SKIP_CONSULT_RENDER, but in my particular case I also want to skip loading since that'd access vault/consul in a way that isn't useful for my project. So the "skip" instruction ended up one line earlier and one word fewer.

Here's a test question to decide between the current approach and your suggestion... can we imagine any scenario where loading the Railtie integration is useful, but we also don't want to load/render? If yes, then we should keep my approach. If no, then I think yours is better. (I currently can't think of any reason we'd want to load the Railtie if we aren't going to do anything).

@bvogelzang
Copy link
Member

bvogelzang commented Mar 22, 2023

I don't necessarily think this would be better and this seems handy enough in other situations but here's an alternative for your acute problem:

You don't necessarily have to load Rails at all to run your rake task. Call the rake tasks with bundle exec rake --rakefile lib/tasks/build.rake namespace:task_name.... This will avoid the need for extra environment variables and reduce the time it takes for the task(s) to run.

You are checking the rails environment but that can be passed into the rake task as a separate argument or could be read from the environment instead.

@aharpervc
Copy link
Contributor Author

I'd probably still need something from Consult here for things like bundle exec rails dev:cache though right?

@sixfeetover
Copy link
Member

I also want to skip loading since that'd access vault/consul in a way that isn't useful for my project

I could be wrong but I think access is JIT, so if templates aren't rendered no Vault/Consul access will be attempted.

can we imagine any scenario where loading the Railtie integration is useful, but we also don't want to load/render

Not that I can think of off the top of my head.

@aharpervc
Copy link
Contributor Author

I also want to skip loading since that'd access vault/consul in a way that isn't useful for my project

I could be wrong but I think access is JIT, so if templates aren't rendered no Vault/Consul access will be attempted.

The load method will also call these "configure" methods, which I assume are allowed to contact their respective services:

consult/lib/consult.rb

Lines 42 to 43 in 4a90ccf

configure_consul
configure_vault

can we imagine any scenario where loading the Railtie integration is useful, but we also don't want to load/render

Not that I can think of off the top of my head.

Great, I'll redo this PR with your suggestion instead.

Copy link

@ronnietaylor ronnietaylor left a comment

Choose a reason for hiding this comment

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

Seems reasonable. LGTM

@aharpervc aharpervc merged commit 1d31855 into master Apr 4, 2023
@aharpervc aharpervc deleted the skip-consult branch April 4, 2023 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants