I have a build task in rake defined with the following dependencies:
desc 'Builds the App'
task :rebuild_dev => ["solr:start", "db:drop", "db:create", "db:migrate", "spec", "solr:stop"]
The first task "solr:start" starts the Solr Indexing server. Now, if the build fails (may be in spec tests fail), the "solr:stop" task is not executed. And the server is not stopped.
Is there any way to specify a clean-up task or a task that always runs even if one of the dependent tasks fail? In my case, to always ensure that "solr:stop" executes...
You just need use the ensure system of Ruby
desc "Builds the App"
task :rebuild_dev do
begin
["solr:start", "db:drop", "db:create", "db:migrate", "spec"].each do |t|
Rake::Task[t].execute
end
ensure
Rake::Task["solr:stop"].execute
end
end
Related
// This is a question for Ruby on Rails
I have a Rails Tasks that should take arguments from command line.
Looks like this :
task update: :environment do
if (ARGV[1] == "DEBUG")
DEBUG = true
else
DEBUG = false
end
Now I can run the command line :
rails call:update DEBUG
and it works !
But after the task was finished I got also the message :
rails aborted!
Don't know how to build task 'DEBUG' (See the list of available tasks with `rails --tasks`)
I looking already around the community, but all what I found was quite old and seems not to be compatible with Rails 6 ! So thats the reason why I asking here.
I try with the code you shared above I got the same error of rake abort.
its Comming because they way you calling the rake task with the argument it assume the DEBUG is another task so it try to find and give this error.
Here is proper way of doing it
I did some modification in the code to get rid of the error use the following code it will work perfectly
task :update, [:value] => [:environment] do |t, args|
if (args[:value] == "DEBUG")
DEBUG = true
p "Value is True"
else
DEBUG = false
p "Value is False"
end
end
it works perfectly
You can also put this task in the namespace and then call it from there as well
namespace :debug_task do
# task code here what I mentioned above
end
and then call it to form the terminal like this
rake debug_task:update["DEBUG"]
I'm trying to do enqueue a simple job using Resque 1.26.0 (and Redis-rb 3.3.1). The job doesn't seem to be processing the perform function because resque-web is processing each job and shows 0 failures. The jobs also are being processed instantly.
The jobs are enqueued from a controller action with
Resque.enqueue(TestJob, url)
The job itself looks like
class TestJob < ApplicationJob
#queue = :tags_queue
Logger.new("log/resque_worker_QUEUE.log").fatal("thing")
def self.perform(url)
Logger.new("log/resque_worker_QUEUE.log").fatal("other thing")
logger.fatal("more errors please")
myDivideByZeroVar= 1/0
raise "error"
Logger.new("log/resque_worker_QUEUE.log").fatal("other thing")
logger.fatal("more errors please")
end
end
A rake task is also set up:
require 'resque/tasks'
task "resque:setup" => :environment
The redis-server is running.
The worker is started with rake resque:work QUEUE=*. Using verbose logging doesn't show anything useful.
The log file only shows the first fatal error string "thing". None of the other errors are logged that are inside perform.
What am I doing wrong here?
Solved this. The job needed to be called using ActiveJob instead of using Resque.enqueue. TestJob.perform_later(url) worked just fine.
I need to make a custom Rails 4 rake task to delete all records in database using ip:
task :delete_records, [:ip] => :environment do |t, args|
User.destroy_all(ip: args.ip)
end
I try to execute it using the following command:
bundle exec rake delete_records["127.0.0.1"] but I've the error:
no matches found: delete_records["127.0.0.1"]
How can I fix it? Thank in advance!
I'm guessing you used this answer for help - although we've done rake tasks before, we've never used one with arguments like this
--
Test
We've created custom rake tasks before - they reside in lib/tasks
I would test out yours by doing the following:
#lib/tasks/your_task.rake
desc "Remove all records for particular IP"
task :delete_records, [:id] => :environment do
args.with_defaults(:ip => "127.0.0.1")
User.destroy_all ip: args.ip
end
If this does not work, it will mean there's something wrong with the definition of your task (it's not in the correct folder or something)
The above should determine if your argument definition is the issue; if it isn't you should let me know so we can work to fix it
I have three Rake tasks invoked from another Rake task. The first Rake task requires that an environmental variable is set before it is executed.
The following works, however it means that I lose all the output from the task which is critical:
namespace :deploy do
task :staging => :environment do
`EXAMPLE=something rake db:rebuild`
Rake::Task["rake envs:push:staging"].invoke
Rake::Task["rake app:push:staging"].invoke
end
end
How can I invoke the first task with the environmental variable AND display its output to the terminal?
ENV['EXAMPLE'] = 'something'
Rake::Task['db:rebuild'].invoke
Use system instead of back-ticks:
system("EXAMPLE=something rake db:rebuild")
What is the syntax for calling rake tasks from clockwork? I've tried all kinds of syntax, and nothing seems to work. (I'm specifically interested in clockwork because Heroku's supporting it.)
Here's my clock.rb, using the same syntax that the whenever gem uses:
module Clockwork
puts "testing clockwork!"
every(30.seconds, 'Send Messages') {
rake 'scheduler:send_messages'
}
end
And here's my rake task in scheduler.rake:
task :send_messages => :environment do
puts "rake task run successfully!"
end
And here's what happens when I start a clockwork process:
$ clockwork lib/clock.rb
testing clockwork!
I, [2012-07-16T14:42:58.107245 #46427] INFO -- : Starting clock for 1 events: [ Send Messages ]
I, [2012-07-16T14:42:58.107364 #46427] INFO -- : Triggering 'Send Messages'
attempting to run rake task!
E, [2012-07-16T14:42:58.107437 #46427] ERROR -- : undefined method `rake' for Clockwork:Module (NoMethodError)
This runs every 30 seconds. As you can see, the clock.rb is executed successfully. But I can't for the life of me figure out the syntax to run a rake task. The clockwork readme is no help, unfortunately:
https://github.com/tomykaira/clockwork
rake is not a method, so you can't invoke it like that here.
You can either shell out and invoke it, something like
every(30.seconds, 'Send Messages') {
`rake scheduler:send_messages`
}
or rather invoke a new detached process using the heroku API. This is my preferred method right now:
Heroku::API.new.post_ps('your-app', 'rake scheduler:send_messages')
Heroku::API is available from heroku.rb: https://github.com/heroku/heroku.rb
You can add the following method to your clock.rb file:
def execute_rake(file,task)
require 'rake'
rake = Rake::Application.new
Rake.application = rake
Rake::Task.define_task(:environment)
load "#{Rails.root}/lib/tasks/#{file}"
rake[task].invoke
end
and then call
execute_rake("your_rake_file.rake","your:rake:task")
in your handler
You can pass in a block to every that executes your rake task:
every(1.day, "namespace:task") do
ApplicationName::Application.load_tasks
Rake::Task['namespace:task'].invoke
end
Invoking the task using Rake::Task['...'].invoke works well the first time, but the task need to be reenabled to be invoked again later.
ApplicationName::Application.load_tasks
module Clockwork do
every(10.minutes, "namespace:task") do
Rake::Task['namespace:task'].invoke
Rake::Task['namespace:task'].reenable
end
end
Otherwise the task will be invoked the first time, but no more after that until the clockwork process is restarted.