How do i find out if the database exists from within a rake task?
that is, i'd like to do something like:
task :drop_and_create => :environment do
Rails.env = "development"
if (db_exists?)
Rake::Task["db:drop"].invoke
end
Rake::Task["db:create"].invoke
#more stuff...
end
how do i write the db_exists? condition?
How about instead doing a begin/rescue:
task :drop_and_create => :environment do
Rails.env = "development"
if (db_exists?)
begin
Rake::Task["db:drop"].invoke
rescue Exception => e
logger.debug("Error:#{e}")
Rake::Task["db:create"].invoke
#more stuff...
end
task :drop_and_create => :environment do
Rails.env = "development"
Rake::Task["db:reset"].invoke
#more stuff...
end
Related
A set of rake tasks of a .rake file are structured as follows
task :process_data => :environment do
CSV.foreach("scores.tsv", :col_sep => "\t", headers: true) do |row|
begin
[...]
repeated_method_a
ad-hoc_method
repeated_method_b
rescue StandardError => e
end
end
end
How should this rake file be structured to process sub-methods, such as:
def repeated_method_a
do_its_thing
end
You can simply add it under your task in the same file, so you have this:
task :process_data => :environment do
CSV.foreach("scores.tsv", :col_sep => "\t", headers: true) do |row|
begin
[...]
repeated_method_a
ad-hoc_method
repeated_method_b
rescue StandardError => e
end
end
end
def repeated_method_a
do_its_thing
end
I have a rake task like this:
task :update_all => :environment do
codes = get_all_codes
codes.each{ |code| find_or_create_from_my_data(code) }
end
Sometimes the update fails, so I want to know with which code failed.
For that I wrote like this:
task :update_all => :environment do
begin
codes = get_all_codes
#code
codes.each{ |code| #code = code; find_or_create_from_my_data(code) }
rescue
p #code
end
end
It works fine, but I think it's a bit redundant. How can I write more effectively?
the e.message will display for you which code failed and why
task :update_all => :environment do
codes = get_all_codes
codes.each{ |code| find_or_create_from_my_data(code) }
rescue => e
puts "(#{e.message})"
end
How about this:
task :update_all => :environment do
get_all_codes.each do |code|
begin
find_or_create_from_my_data(code)
rescue
p code
end
end
end
This way, even if one code fails, it will print it out and move on to the other ones instead of aborting early.
In a rake task I'm using argument like this:
namespace :foo do
task :bar, [:arg1] do |t, args|
puts args[:arg1]
end
end
And I want to use ActiveRecord model, I add => :environment like this:
namespace :foo do
task :bar => :environment, [:arg1] do |t, args|
puts args[:arg1]
end
end
And when I run rake foo:bar[1], it ends up with error:
rake aborted!
SyntaxError: /home/ironsand/rails_project/lib/tasks/foo.rake:2: syntax error, unexpected keyword_do_block, expecting =>
task :bar => :environment, [:arg1] do |t, args|
What should I to use ActiveRecord model and argument same time in a rake task?
Correct syntax is:
namespace :foo do
task :bar, [:arg1] => :environment do |t, args|
puts args[:arg1]
end
end
I've seen a few solutions that didn't seem to work for me. Suppose I have the following code-
namespace :genie do
task :test => :environment do
test_user = User.find_or_create_by_username('test') do |i|
i.email = 'email#email.com'
end
task :test_reset => :environment do
test_user.update_attributes({
:email => 'test#email.com',
})
end
This code fails when running rake genie:test, rake genie:test_reset because test_user is not defined in the second rake task. How can I call test_user without having to define it each time?
You can encapsulate the definition of the user in a helper function.
namespace :genie do
task :test => :environment do
puts test_user.email
end
task :test_reset => :environment do
test_user.update_attributes({
:email => 'test#email.com',
})
end
end
def test_user
User.find_or_create_by_username('test') do |i|
i.email = email#email.com
end
end
I have a rake task that accepts an argument, :scope (below).
I call the rake task like this:
rake podcast:generate_inventory["new"]
This task used to pass the :scope arg perfectly, however, I noticed today that the arg is no longer being passed. Does anyone have any idea why this is happening?
namespace :podcast do
task :itunes_top_300, [:scope] => :environment do |t,args|
Podcast.podcast_logger.info("BEGIN: #{Time.now}")
if args[:scope] == "new"
Podcast.podcast_logger.info("NEW PODCASTS ONLY")
end
Podcast.itunes_top_rss
end
task :itunes_genres_top_300 => :itunes_top_300 do
Podcast.itunes_genre_rss
end
task :site_and_feed_discovery, [:scope] => :itunes_genres_top_300 do |t,args|
if args[:scope] == "new"
Podcast.site_and_feed_discovery(:new_podcasts_only => true)
else
Podcast.site_and_feed_discovery
end
end
task :social_discovery, [:scope] => :site_and_feed_discovery do |t,args|
if args[:scope] == "new"
Podcast.social_discovery(:new_podcasts_only => true)
else
Podcast.social_discovery
end
end
task :fetch_episodes => :social_discovery do |t,args|
Episode.episode_logger.info("BEGIN: #{Time.now}")
Podcast.fetch_episodes
Episode.episode_logger.info("END: #{Time.now}")
end
task :generate_inventory => :fetch_episodes do |t,args|
Podcast.podcast_logger.info("Successful Rake")
Podcast.podcast_logger.info("END #{Time.now}")
Rake::Task['maintenance:daily'].invoke
end
end
Looks like you're missing the [:scope] bit in your definition of task :generate_inventory. I suspect adding that back in will take care of everything.
Hope that helps!