How to use rescue in rake task - ruby-on-rails

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.

Related

Using method within rake file tasks

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

Write rspec for rake task

i have task /lib/crawler.rake like that:
namespace :crawler do
area_names = Dir[Rails.root.join("lib", "crawler", "*.rb")].map do |file_name|
File.basename(file_name, ".rb")
end
area_names.each do |area_name|
task area_name.to_sym => :environment do
logger = Logger.new("log/crawl_#{area_name}.log")
# do something
parallel_results = crawler.crawl
mutex = Mutex.new
Parallel.each(parallel_results, in_threads: [parallel_results.count, CRAWL_CONFIG["building_thread_max"]].min) do |pages|
begin
# do something
rescue => e
# do something
raise e
end
end
Availability.update_by_grounds_and_time
end
end
end
Logic here, if everything's ok after parallel, we'll call update_by_grounds_and_time method to update Availability; if get error, we'll stop action and raise error.
So i want to write rspec to test for these cases, i want to mock/stub output of task here (pass or raise error) and check did we call update_by_grounds_and_time method?
Can we don't need invoke really task? can we use Rspec Mock?
Can you help me!
Thank
What I usually do in these cases is I extract the meat into a separate class/service-object/whatever, which is much easier to test. The rake task then becomes just an invoker of that object and, as such, doesn't need to be tested.
If it is defined in Rakefile, try this:
require 'rake'
RSpec.describe "Rake Tasks" do
before do
file, path = Rake.application.find_rakefile_location
Rake.load_rakefile("#{path}/#{file}")
end
it "should invoke some tasks" do
expect(Availability).to receive(:update_by_grounds_and_time)
Rake.application["crawler:#{area_name}"].invoke
end
end
If it is defined in foo.rake, then try this one:
require 'rake'
RSpec.describe "Rake Tasks" do
before do
Rake.application.rake_require('/path/to/lib/tasks/foo')
end
it "should invoke some tasks" do
expect(Availability).to receive(:update_by_grounds_and_time)
Rake.application["crawler:#{area_name}"].invoke
end
end
UPDATE (error case)
For example
# foo.rake
Parallel.each(parallel_results, in_threads: [parallel_results.count, CRAWL_CONFIG["building_thread_max"]].min) do |pages|
begin
foo = Foo.new
foo.bar
# do something else
rescue => e
# do something
raise e
end
end
# foo_spec.rb
require 'rake'
RSpec.describe "Rake Tasks" do
before do
Rake.application.rake_require('/path/to/lib/tasks/foo')
end
it "should not call Availability#update_by_grounds_and_time if error raised" do
allow_any_instance_of(Foo).to receive(:bar).and_raise(StandardError)
expect(Availability).to_not receive(:update_by_grounds_and_time)
expect { Rake.application["crawler:#{area_name}"].invoke }.to raise_error(StandardError)
end
end

How to access variables across multiple rake tasks with Ruby on Rails

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

How to know if DB exists from within Rake task

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

Args not being passed through to Rake tasks

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!

Resources