I have the SLEEP_TIME constant written in each task inside the namespace . Each task has its own constant with its own value.

Why when I run task1 , I see these errors?

 /app/lib/tasks/task1.rake:11: warning: already initialized constant SLEEP_TIME /app/lib/tasks/task2.rake:9: warning: previous definition of SLEEP_TIME was here /app/lib/tasks/task3.rake:11: warning: already initialized constant SLEEP_TIME /app/lib/tasks/task1.rake:11: warning: previous definition of SLEEP_TIME was here 

Accordingly, if you use not one, but more constants, then these errors will be more.

Why does one run into another when running a single task? Can I turn it off somehow?

  • "inside namespace " - any? Which one from Rake ? - D-side
  • @ D-side is the most. - Colibri
  • @ D-side just noticed the same errors when deploying, for example, all rake assets . - Colibri

1 answer 1

The heme rake (Ruby make) is used not only in Ruby on Rails but also to perform dependent tasks in pure Ruby. Therefore, rake does not follow the autoload rail conventions; the names of rake tasks do not have to match the file names. To find a task, rake needs to download all the rake files. Since rake tasks are not placed in modules or classes, the SLEEP_TIME constant is in the global scope and is constantly redefined.

Getting rid of this behavior does not make sense, because: rake-tasks often depend on each other

 task foo: :baz do ... end 

In Ruby on Rails, rake tasks may depend on the Rails environment.

 task questions: :environment do ... end 

Those. isolating tasks from each other is impossible in principle - rake is intended to bind them, as for example, this happens during deployment. Pay attention to capflow - a dozen tasks are connected into a single tree, isolating tasks - destroy the deployment procedure.

Instead, it is better to isolate constants in modules with unique names:

 namespace :hello do module Hello SLEEP_TIME = 100 end desc 'hello' task :hello do puts Hello::SLEEP_TIME end end 

And it makes sense to bring modules out of rake-tasks, for example, in the lib folder.