What should I do to load rails models for rake task which is part of my plugin?
Require the :environment rake task to run before it.
task :my_task => :environment do
end
Related
I have rake tasks which installs and starts neo4j.
rake neo4j:install[community-latest, stable]
rake neo4j:start[stable] where `stable` is environment.
Now I want to write another rake task something like rake setup and create dependency on rake neo4j:start[stable] and rake neo4j:install[community-latest, stable]
I have tried,
task :setup_dev_env => [:neo4j:install[community-latest, stable], :neo4j:start[stable]] do
puts "Created Rake task"
end
obviously this doesn't work, because in the above task neo4j is namespace. Then I have changed my task to something like,
task :setup_dev_env => [:'neo4j:install[community-latest, stable]', :'neo4j:start[stable]'] do
puts "Hello rake task working"
end
so, at least this solved my issue with neo4j namespace, but still couldn't solve the problem.
When I run rake setup_dev_env It says
rake aborted!
Don't know how to build task 'neo4j:install[community-latest, stable]' (see --tasks)
You can modify that task as:
desc 'Some description'
# setup_dev_env is dependent on neo4j:start
task :setup_dev_env,[:stable] => :environment do |t, arg|
param = arg[:stable].nil? ? 'stable' : arg[:stable]
Rake::Task['neo4j:start'].invoke(param)
puts "Created Rake task"
end
You can write your task as follow:
desc 'Some description'
# setup_dev_env is dependent on neo4j:start
task :setup_dev_env,[:stable] => "neo4j:start" do
puts "Created Rake task"
end
The above lines of code accept a parameter stable and pass it to neo4j:start. To use stable parameter in neo4j:start, you must have to receive as like:
desc 'Some description'
# it will be inside namespace neo4j
task :start, [:stable] do |t, args|
puts "Created Rake task #{args.inspect}"
end
I deployed my app to vps with capistrano.
Everything works fine but only :environment task.
This is my code
namespace :deploy do
desc 'Clear memcache'
task clear_memcache: :environment do
on roles(:app) , in: :sequence, wait: 2 do
::Rails.cache.clear
CACHE.flush
end
end
after :finishing, :clear_memcache
end
But i always got this error.
#<RuntimeError: Don't know how to build task 'environment' (see --tasks)>
How can i fix this?
Thanks!
I think you are mixing two concepts. A rake task and a capistrano task. Rake tasks do use the :environment subtask, whereas the capistrano ones don't. Capistrano tasks cannot (AFAIK) directly call ruby code in the context of the rails application on the server, this is what rake tasks do.
What you actually probably want is to define both a rake task to clear the cache and a capistrano task that will call the rake task on the deployment server.
Try these:
The rake task to clear the cache:
# put this in Rakefile or any other rake file under lib/tasks
desc 'Clear memcache'
task clear_memcache: :environment do
::Rails.cache.clear
CACHE.flush
end
The capistrano task to call the rake on the server:
# config/deploy/production.rb
namespace :deploy do
desc 'Clear memcache'
task :clear_memcache do
on roles(:app) , in: :sequence, wait: 2 do
within release_path do
with rails_env: fetch(:rails_env) do
execute 'rake', 'clear_memcache'
end
end
end
end
after :finishing, :clear_memcache
end
Hello I'm writing a deploy script(rake task) for my mini project. And I have this part when I invoke the db seed :
Rake::Task['db:seed'].invoke
And also compiling assets :
Rake::Task['assets:precompile'].invoke
So I was wondering is there a way to invoke these tasks in production environment like you would do from a console like this :
RAILS_ENV=production rake db:seed
In Rails, you can do as :
[arup#app (master)]$ rails g task my_namespace my_task1
create lib/tasks/my_namespace.rake
[arup#app (master)]$ cat lib/tasks/my_namespace.rake
namespace :my_namespace do
desc "TODO"
task my_task1: :environment do
end
end
[arup#app (master)]$
Now see the Rakefile is ready for you.
Just open the Rakefile you just created, and define your tasks.
namespace :my_namespace do
task my_task1: :environment do
Rake::Task['db:seed'].invoke
Rake::Task['assets:precompile'].invoke
end
end
Hints is 2.10 Custom Rake Tasks.
Upon following the instructions to create a rake task to clear the cache, when running that rake task:
namespace :cache do
desc "Clears Rails cache"
task :clear do
Rails.cache.clear
end
end
and running that rake task with:
rake cache:clear
I get an error:
undefined method `clear' for nil:NilClass
When running Rails.cache.clear from the rails console, it successfully clears the cache without an error. Why is cache nil on the Rails object in the rake task, but not in the rails console?
Note: I am using dalli and memcache.
To answer Why is => :environment needed?
:environment is a task defined by rails.
When you need to interact with your application models, perform database queries and so on, your custom task should depend on the environment task, as it will load your rails application code.
Rails.cache will return nil if your application is not loaded.
Hence, the error undefined method 'clear' for nil:NilClass
You need to run environment task before your custom clear task.
namespace :cache do
desc "Clears Rails cache"
task :clear => :environment do ## This will run environment task first and then clear task
Rails.cache.clear
end
end
Figured it out. I was missing => :environment after :clear
The below works:
namespace :cache do
desc "Clears Rails cache"
task :clear => :environment do
Rails.cache.clear
end
end
Why is => :environment needed?
I'm using the friendly_id gem.
There's a rake task for deleting old slugs (from the docs):
rake friendly_id:remove_old_slugs MODEL=<model name> DAYS=<days>
It can be run via cron.
Do you know how it should be added to cron.rake (I'm on Heroku)?
Here is my cron.rake:
desc "This task is called by the Heroku cron add-on"
task :cron => :environment do
...
rake friendly_id:remove_old_slugs
end
It produces an error:
"rake aborted! undefined method `friendly_id' for main:Object"
There is no error if I run it from the console (Terminal) like this:
heroku rake friendly_id:remove_old_slugs
Try this:
desc "This task is called by the Heroku cron add-on"
task :cron => :environment do
Rake::Task['friendly_id:remove_old_slugs'].execute
end