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
Related
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 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 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?
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'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