I have a rake file that is being called by a job scheduler. The file outputs the desc but I am not able to log anything else to the console. What am I missing?
inbox.rake
namespace :inbox do
desc 'Check inbox for new app builds'
task process_inbox: :environment do
puts "my task is working"
end
end
Similar to Heroku logs, you need STDOUT to see the outputs. Could be as simple as
my_logger = Logger.new(STDOUT)
my_logger.info "work or die"
puts sends the text to STDOUT, which is different when you run rake from the terminal versus invoking from another ruby process.
Where do you expect to see this text?
Try manually printing to console.
namespace :inbox do
desc 'Check inbox for new app builds'
task process_inbox: :environment do
Rails.logger.info "my task is working"
end
end
You're not using --quiet / --silent are you?
Related
I'm running a large update on my models (generating a random public_id) and it's taking forever on heroku.
Running the command:
Episode.where(public_id: nil).find_each do |e|
e.set_public_id
e.save
end
Is taking forever and it's a black box. Running it locally I could see the progress through database commits.
Is there a way to see what's going on? Is there a different way I should be running this command?
Make a rake task & use the logger to inform whats happening
#lib/tasks/episode.rake
require 'rake'
namespace :episode do
desc "your description"
task :update => :environment do
Episode.where(public_id: nil).find_each do |e|
Rails.logger.info "updating #{e} ..."
e.set_public_id
e.save
end
end
end
Then run it
heroku run rake episode:update
and you can tail the logs in another terminal window with
heroku logs -t
Somebody has asked a similar question here:
https://github.com/jimweirich/rake/issues/257
The answer from the maintainer was:
I am going to reject this since it allows you to use tasks in non-rake-like ways.
So what are the correct way of using rake if a task depends of other tasks.
task 'succeed' => ['db:drop','stats'] do something end
displays results of stats even if Postgres threw an error and db:drop failded because of active connections.
If rake is not suitable for system maintenace, what tools should I use?
I need to be able to run a backup of a database, then do some tests, then drop the database and finally restore from backup.
to hel you understand my problem look at folowing fragment
namespace :experiment do
desc "TODO"
task 'succeed' => ['stopme', 'stats'] do
puts 'this and stats task should not run'
end
desc "TODO"
task stopme: :environment do
Rake::Task['db:drop'].invoke
end
end
You can invoke tasks manually like that:
task :stats => :environment do
Rake::Task['db:drop'].invoke rescue nil
# do something
end
I'm trying to user rake and rufus, both of which I am new to. I want to have Rufus call my rake task but I am getting the following error. Don't know how to build task 'inbox:process_inbox'
lib/tasks/inbox_tasks.rb
namespace :inbox do
task :process_inbox do
logger = Logger.new(Rails.root.to_s + "/log/scheduler.log")
logger.info "Rufus Here!"
end
end
rufus_scheduler.rb
require 'rufus-scheduler'
require 'rake'
scheduler = Rufus::Scheduler.new
scheduler.every '10s', :first_at => Time.now + 3 do
Rake::Task["inbox:process_inbox"]
end
As #jmettraux (the creator of rufus-scheduler!) has already answered, the problem is that the rake task is defined in a .rb file instead of .rake file.
Adding some more details to help in the future.
While creating a new rake task, you could get the rails generator to automatically create the file with appropriate structure.
Example: Running
> rails g task inbox process_inbox
create lib/tasks/inbox.rake
will create a file named lib/tasks/inbox.rake with content:
namespace :inbox do
desc "TODO"
task process_inbox: :environment do
end
end
Having a DESC in the task definition is important; that allows for verifying that the rake task is defined and available, by running either rake -T inbox or rake -T | grep inbox
> rake -T inbox
rake inbox:process_inbox # TODO
Could this one help?
How to build task 'db:populate' (renaming inbox_tasks.rb to inbox_tasks.rake)
(did a simple https://www.google.com/?#q=rails+don%27t+know+how+to+build+task ...)
I'm trying to help a non-technical user run a specific rails rake task and be able to see the results of the rake task in their browser.
Below is my rake task code:
namespace :partner do
task :report => :environment do
csv_output = Partner.generate_report
csv_output.split("\n").each {|row| puts row}
end
task :sample_report => :environment do
csv_output = Partner.generate_report(30)
csv_output.split("\n").each {|row| puts row}
end
end
Currently I just run rake partner:report and a bunch of data shows up in terminal but I would like for them to be able to press a button on a View page, run the rake task, and then see the results directly in the View.
Any ideas or suggestions would be MUCH APPRECIATED.
You maybe want to look to a background task gem for that purpose.
Have a look into these:
https://github.com/defunkt/resque
http://mperham.github.com/sidekiq/
https://github.com/collectiveidea/delayed_job
I have two questions:
How can I add a heroku worker just before running a delayed job and remove it after it finishes?
Is my cron.rake ok?
cron.rake:
desc "This task is called by the Heroku cron add-on"
task :cron => :environment do
puts "requesting homepage to refresh cache"
uri = URI.parse('http://something.com')
Net::HTTP.get(uri)
puts "end requesting homepage"
puts "start sending daily mail"
User.notified_today.each do |user|
Delayed::Job.enqueue UserMailer.daily_mail(user).deliver
end
puts "end sending daily mail"
end
I use collectiveidea delayed_job.
I've had good success with HireFire.
Easy setup:
Add gem 'hirefire' to your Gemfile
Create Rails.root/config/initializers/hirefire.rb with the config information.
To add remove/remove workers, hook into your ORM's after :create / after :destroy
With DataMapper on Heroku, I did it like this (You must set the ENV vars yourself)
MAX_CONCURRENT_WORKERS = 5
if ENV["HEROKU_APP"]
Delayed::Job.after :create do
workers_needed = [Delayed::Job.count, MAX_CONCURRENT_WORKERS].min
client = Heroku::Client.new(ENV['HEROKU_USERNAME'], ENV['HEROKU_PASSWORD'])
client.set_workers(ENV['HEROKU_APP'], workers_needed)
puts "- Initialized Heroku workers for ZipDecoder"
end
Delayed::Job.after :destroy do
workers_needed = [Delayed::Job.count, MAX_CONCURRENT_WORKERS].min
client = Heroku::Client.new(ENV['HEROKU_USERNAME'], ENV['HEROKU_PASSWORD'])
client.set_workers(ENV['HEROKU_APP'], workers_needed)
puts "- Cleaned Up a Delayed Job for ZipDecoder ---------------------------------"
end
end
You maybe can use an "autoscale" plugin like workless or heroku-autoscale.
About the cron I don't see any problem on it...