How to get the name of the current rake task in my Rails model?

I have some problems with one of the stones supporting ActiveModel caching. When I use the observer for the cached model, during application initialization, it tries to describe the table in order to get all the field names.

The same thing is done when performing the rake task, including db: migration. In this case, there is some circular control error. I would like to discover the current rake task in order to skip pearl initialization, but I do not know how to find out if the code was called using the rake function. How to check it?

+6
source share
3 answers

If you run your task using rake task or bundle exec rake task , you can check it in your initializer, simply:

 if $0.end_with?('rake') # rake stuff else # non-rake stuff end 

You can use $PROGRAM_NAME instead of $0 if you want.

+1
source

I am not getting exactly what you are trying to do, but here is an example of getting the task name.

  task :testing do |task_name| puts task_name end 
+11
source

This question was asked in several places, and I did not think that any of the answers was very good ... I think the answer is to check Rake.application.top_level_tasks , which is a list of tasks that will be performed. Rake does not necessarily run just one task.

+8
source

Source: https://habr.com/ru/post/916991/


All Articles