I'm using whenever to generate the cronjobs for my Rails application.
Rails 4.2.1
In the schedule.rb I have something like that:
every 13.minutes do
rake "crons:dosomething"
end
This generates a cronjoblike this:
RAILS_ENV=production bundle exec rake crons:dosomething
Problem is that this one only runs cronjobs for production environment ... but I need the cronjob as well in test and development
I tried this:
every 13.minutes do
rake "crons:dosomething", :environment => :development
rake "crons:dosomething", :environment => :test
rake "crons:dosomething", :environment => :production
end
problem here is that the second rake runs before the first one is completely finished is there any solution for this problem?
Related
I have just added the seed-fu gem to my app for seeding my test-database:
group :test do
gem 'seed-fu'
end
I made a custom rake task (in /lib/tasks/db.rake) for seeding only my test-database:
namespace :db do
desc "seed_fu only in test-database"
task seed_fu_test: :environment do
Rails.env = 'test'
puts "Seeding will be made in test-base ONLY!"
Rake::Task["db:seed_fu"].execute
end
end
If I do rake -T | grep seed then my new custom-made task is shown amongst other seed-tasks:
rake db:seed # Load the seed data from db/seeds.rb
rake db:seed_fu # Loads seed data for the current environment
rake db:seed_fu_test # seed_fu only in test-database
Now when I do rake db:seed_fu_test I get
rake aborted!
Don't know how to build task 'db:seed_fu'
But when I do
rake db:seed_fu RAILS_ENV='test'
then seed_fu seeds my test-database well.
Figured it out- the problem was in my Gemfile. Because I added the seed-fu gem into test-group then in development-environment, which was my default for running also the rake db:seed_fu_test task, the seed_fu gem was not seen.
Therefore when moving gem 'seed-fu' line into my :development-group in Gemfile, the problem was solved.
I want to use Heroku's scheduler to reset my database once every day.
It's recommended to use rake tasks for the scheduler. This is what I've tried:
task :reset_database => :environment do
`heroku pg:reset MY_DB:URL`
`heroku run rake db:migrate db:seed`
# some other ruby commands
end
But how would I do this correctly, because putting the heroku commands within backticks, which with bash normally works, doesn't work here:
No such file or directory - heroku
Try this rake task:
namespace :reset_database do
desc "Destroy all table entries."
task :all => :environment do
ActiveRecord::Base.connection.tables.each do |table|
if table != 'schema_migrations'
table.singularize.camelize.constantize.destroy_all
end
# Use this if you want to use the normal seeds:
# Rails.application.load_seed
# Use this if you want to run another rake task:
Rake::Task["foo:bar"].invoke
end
end
end
I have a problem when I do:
namespace :xaaron do
task :get_roles do
roles = Xaaron::Role.all
puts roles
end
task :get_role, [:name] do |t, args|
role = Xaaron::Role.find(args[:name].parameterize)
puts role
end
end
The first task will work fine. I can even add binding.pry and run Xaaron::Role and get information about Roles back. But the second task fails with:
NameError: uninitialized constant Xaaron::Role
I run each task in my main app because these tasks are inside an engine, using:
bin/rake xaaron:get_roles` and `bin/rake xaaron:get_role
I can run bin/rails c in the main application that uses the engine and run Xaaron::Role and get information about Roles table.
Why is the second one failing but the first one is not? Is there scoping with arguments?
I'm not sure why either works, but if this is Rails and those are Rails models, your tasks should depend on the environment:
task :get_roles => [ :environment ] do
By depending on the :environment task, it first loads Rails.
Also see: What's the 'environment' task in Rake?.
You can also run a Rake task as
bundle exec rake environment xaaron:get_role
This will load the Rails environment first.
I kept getting uninitialized constant errors for a Rake task, even after depending on :environment and running with bundle exec.
The issue was that I was making a Rake::TestTask and, even though the Rake task had access to all constants, the test files themselves did not have access to constants.
The solution was to add this line to the top of my test file:
require_relative '../config/environment'
This is the Rake task:
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/test_*.rb"]
end
To add, as of Ruby 1.9 and above, you can use this hash syntax:
namespace :xaaron do
desc "Rake task to get roles"
task get_roles: :environment do
roles = Xaaron::Role.all
puts roles
end
#####
end
And then you can run the command below to run the Rake task:
rake xaaron:get_roles
or
bundle exec rake xaaron:get_roles
I recently added a second database to my development Rails site, and made a custom rake task, 'SysConfig:db:migrate' which can be seen below:
namespace :SysConfig do
task :set_custom_db_config_paths do
ENV['SCHEMA'] = 'db_sysconfig/schema.rb'
Rails.application.config.paths['db'] = ['db_sysconfig']
Rails.application.config.paths['db/migrate'] = ['db_sysconfig/migrate']
Rails.application.config.paths['db/seeds'] = ['db_sysconfig/seeds.rb']
Rails.application.config.paths['config/database'] = ['config/database_sysconfig.yml']
end
namespace :db do
task :migrate => :set_custom_db_config_paths do
Rake::Task["db:migrate"].invoke
end
...
end
end
This takes all the migrations in the db_sysconfig/migrate folder and deploys them to the SysConfig database. However, I am struggling to work out how to set up this task in the deploy.rb file for Capistrano, for when I deploy to staging/production. Does anyone know how I can set the application config paths in capistrano?
Capistrano '2.15.4'
Rails '4.0.2'
Ruby '2.1.0'
I added a new task to the deploy namespace in the deploy.rb file:
namespace :deploy do
...
task :SysConfig, roles: :app do
run "cd #{current_path}; RAILS_ENV=#{rails_env} rake SysConfig:db:migrate"
end
end
after "deploy:migrate", "deploy:SysConfig"
I then set it to run after the deploy:migrate task had been ran, which caused it to successfully migrate to both databases at the same time.
I would like to add a custom zeus command in custom_plan.rb for starting solr/sunspot and make this to be automatically started for test/development environments when zeus starts.
I am currently running solr for both instances using rake tasks:
rake sunspot:solr:start RAILS_ENV=test; rake sunspot:solr:start RAILS_ENV=development
I would like to add this to zeus custom_plan.rb as a command:
require 'zeus/rails'
class CustomPlan < Zeus::Rails
def solr
# something like this?
# Sunspot::Rails::Server.new.start
end
end
Zeus.plan = CustomPlan.new
I found a way to do it using guard-sunspot plugin.
Add gem 'guard-sunspot' to your Gemfile and the add this to Guardfile:
guard 'sunspot', :environment => 'test' do
watch('Gemfile.lock')
watch('config/sunspot.yml')
end