Access Rake Task Description from within Task
task
must be defined as a parameter for the task-block.
desc "Populate DB"
task :populate do |task|
puts task.comment # "Populate DB"
puts task.full_comment # "Populate DB"
puts task.name # "populate "
end
Edit: This solution works with rake 0.8.7. At least rake 0.9.2.2 need an additional
Rake::TaskManager.record_task_metadata = true
(I checked only this two versions).A stand alone ruby-script with adaption:
gem 'rake' #'= 0.9.2.2'
require 'rake'
#Needed for rake/gem '= 0.9.2.2'
Rake::TaskManager.record_task_metadata = true
desc "Populate DB"
task :populate do |task|
p task.comment # "Populate DB"
p task.full_comment # "Populate DB"
p task.name # "populate "
end
if $0 == __FILE__
Rake.application['populate'].invoke() #all tasks
end
Reason: in rake/task_manager.rb
line 30 (rake 0.9.2.2) is a check if Rake::TaskManager.record_task_metadata
add_location(task)
task.add_description(get_description(task))
end
The default false
is set in line 305. How do I programmatically extract a rake task's description?
Use the methods comment
or full_comment
for that. More docs on the Rake::Task class here.
How to access namespace from within a rake task?
task.name
includes the namespace. Use this tip to get task.name
to print under rake 0.9.2.2.
Can a Rake task be documented with a multiline usage accessible from command line?
I came here with the same problem: I added a parameter with a default value to task, and wanted to document that in enough detail to make sure that folks knew they could pass their own value.
I found that if I made the string longer, it would print the entire string, wrapping it to the next line, which looks terrible. But if I passed a multiline string, the -T
output would only print the string up to the end of the first line.
It turns out, this behavior is intentional:
https://ruby.github.io/rake/Rake/DSL.html#method-i-desc
Descriptions are shown with rake -T (up to the first sentence)Given your example above,
and rake -D (the entire description).
rake -D
(or rake --describe
) should do the trick:$ rake -T
rake idle[option,token] # Do nothing, even when arguments are provided
$ rake -D
rake idle[option,token]
Do nothing, even when arguments are provided.
Usage:
rake 'users:idle["something", "anotherthing"]'
rake 'users:idle[, "anotherthing"]' # something is ignored anyway
rake users:idle # do nothing tersely
Accessing rake task variables in controller and Scheduling rake tasks
First thing, calling rake task from controller is a bad practice. Ryan published that video at 2008 since that many better solution have came up. You shouldn't ignore it.
I suggest you to use delayed_job
, it serves your needs in a great way. Since, if you want to invoke task dynamically, there should be some checker which will continuously check the desire field every second. Delayed job keep checking its database every time, you can use that.
Anyway,You can use something like this
def self.run_when
Scheduler.all.each do |s|
if d.dynamically_assigned_field < 1.second.ago
d.run_my_job!
d.status = "finished"
d.save
end
end
end
And, in model you can do something like this def run_my_job!
self.status = "processing"
self.save
long_running_task
end
One thing also you should keep in mind that if too many workers/batch/cron job starts at run at same it will fight for resources and may enter into deadlock state. As per your server capacity, you should limit the running jobs. Sidekiq is also a good option you can consider. Personally, i like sidekiq because it doesn't hit my database everytime , scales very effectively. It uses redis but it is expensive.
Rails Rake Task - Access to model class
You're close :)
#lib/tasks/defaultuser.rake
require 'rake'
namespace :defaultuser do
task :adduser => :environment do
...
end
Note the use of :environment, which sets up the necessary Rails environment prior to calling the rake task. After that, your User object will be in scope.
Related Topics
Ruby: How to Convert an Array of Data to Hash and to JSON Format
How to Use C# Style Enumerations in Ruby
Rails3 Scope for Count of Children in Has_Many Relationship
Ruby: Start Reading at Arbitrary Point in Large File
Belongs_To Presence in Rails 5 Not Working
Best/Most Elegant Way to Share Objects Between a Stack of Rack Mounted Apps/Middlewares
Ubuntu 10 Ruby 1.9 Rails 3: No Such File or Directory
Sinatra Not Persisting Session with Redirect on Chrome
How to Deploy a Test App on Dreamhost Rails 3.0.4
When Is a Block or Object That Is Passed to Hash.New Created or Run
How to "Break" Out of a Case...While in Ruby
Nomethoderror: Undefined Method 'On' for Main:Object
Rake Cucumber and Rake Spec Always Use "Develop" Environment
How to Get Records Created at The Current Month
Using Google Search Rest API in Ruby
Easiest Way to Convert "A/B/C" to ["A/B/C", "A/B", "A"]
Rails/Postgres, 'Foreign Keys' Stored in Array to Create 1-Many Association