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!
Related
namespace :mails do
desc "Send emails"
task :send_emails => :environment do
User.not_deleted.find_each do |user|
Notifier.confirm_about_paid_access(user.id).deliver
end
end
desc "Free subscription for users registered in 2011"
task :free_subscription_for => :environment, :year do |t, args|
year = args[:year]
users = User::registered_in(year)
end
end
displaying error: lib/tasks/mails.rake:10: syntax error, unexpected keyword_do_block, expecting =>
task :free_subscription_for => :environment, :year do |t, args|
What is incorrect in syntax code?
Try this,
namespace :mails do
desc "Send emails"
task :send_emails => :environment do
User.not_deleted.find_each do |user|
Notifier.confirm_about_paid_access(user.id).deliver
end
end
desc "Free subscription for users registered in 2011"
task :free_subscription_for, [:year] => :environment do |t, args|
year = args[:year]
users = User::registered_in(year)
end
end
Notice the syntax for :free_subscription_for task.
And while executing the task the syntax would be rake mails:free_subscription_for["2016"]
Hope this helps!
I have setup a Task that check for all the followups that are outstanding by a date. I have now send up a task that will run and check for outstanding follow ups for the day and send out an email reminder. All working i think but i can't get the values to show in the Email itself it keep giving me a NilClass error.
rake aborted!
undefined method `company_name' for nil:NilClass
This task i am running through rake at the moment as it will be running through Cron (Whenever gem) which all is working.
Thanks in Advance Code is Below
lib/tasks/daily.rake
namespace :notifications do
desc "Sends notifications"
task :send => :environment do
Followup.where(:closed => false, :quotefdate => (8640.hours.ago..Time.now)).each do |u|
FollowupMailer.followup_confirmation(#followup).deliver
end
end
end
followup_mailer.rb
class FollowupMailer < ActionMailer::Base
default :from => "from#email.com"
def followup_confirmation(followup)
#followup = followup
mail(:to => 'my#email.com', :subject => "Follow up Required")
end
end
followup_confirmation.text.erb
Good Day
Please action this follow up.
<%= #followup.company_name %>
Kind Regards
Mangement
The error source is located in this rake task:
namespace :notifications do
desc "Sends notifications"
task :send => :environment do
Followup.where(:closed => false, :quotefdate => (8640.hours.ago..Time.now)).each do |u|
FollowupMailer.followup_confirmation(#followup).deliver
end
end
end
You're trying to use #followup instance variable, which is unset. Instead, you should use u passed into block:
namespace :notifications do
desc "Sends notifications"
task :send => :environment do
Followup.where(:closed => false, :quotefdate => (8640.hours.ago..Time.now)).each do |u|
FollowupMailer.followup_confirmation(u).deliver # use u variable here
end
end
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.
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 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