Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-4rake

A General Method to be invoked before every rake execution


In my rails project (Rails 3.1, Ruby 1.9.3) there are around 40 rake tasks defined. The requirement is that I should be able to create an entry (the rake details) in a database table right when we start each rake. The details I need are the rake name, arguments, start time and end time. For this purpose, I don't want rake files to be updated with the code. Is it possible to do this outside the scope of rake files.

Any help would be greatly appreciated!!


Solution

  • This seems to be a bit awkward, But it may help others.

    Rake.application.top_level_tasks
    

    will return an array of information including Rake name and its arguments. Reference attached below.

    pry(main)> a = Rake.application.top_level_tasks
    => ["import_data[client1,", "data.txt]"]