rake task has to run a controller method every 2 minutes
now its working for me....
i moved method implementation to "downloader.rake" file which added at lib/tasks.
the code in downloader.rake is
namespace :downloader do
desc "download a file"
task:downloading => :environment do
Rails.logger.info("message from task")
.......method implementation........
end
end
i scheduled the above implementation in "schedule.rb" in config file
every 2.minutes do
rake "downloader:downloading"
end
here "downloader" is the name of the rake file and "downloading" is the task name
How to call a rake task inside controller?
Rake::Task['task_name'].invoke(args)
But, I'd recommend against this; it's bad practice. It's better to either use Cron if you need that type of functionality, or you can use delayed_job with a custom job object specific to your needs. I'd personally recommend the latter as it causes less pain when moving servers. But delayed_job is not built to run rake tasks, it's meant to queue work items that you create.
Calling a controller action from a rake task
You're approaching it from the wrong end. Instead of trying to hit http endpoint from within the app, just perform the logic from that action directly.
You are in the app environment already and have access to all of the code! You don't need the http interface.
If you insist on using http interface, though, you can do this the same way you'd get an external api endpoint. Using Net::HTTP, for example.
Rails how to run rake task
You can run Rake tasks from your shell by running:
rake task_name
To run from from Ruby (e.g., in the Rails console or another Rake task):
Rake::Task['task_name'].invoke
To run multiple tasks in the same namespace with a single task, create the following new task in your namespace:
task :runall => [:iqmedier, :euroads, :mikkelsen, :orville] do
# This will run after all those tasks have run
end
How do I run rake tasks within my rails application
If you wish this rake code to run during the request cycle then you should avoid running rake via system
or any of the exec family (including backticks) as this will start a new ruby interpreter and reload the rails environment each time it is called.
Instead you can call the Rake commands directly as follows :-
require 'rake'
class SomeModel <ActiveRecord::Base
def self.run_rake(task_name)
load File.join(RAILS_ROOT, 'lib', 'tasks', 'custom_task.rake')
Rake::Task[task_name].invoke
end
end
Note: in Rails 4+, you'll use Rails.root
instead of RAILS_ROOT
.
And then just use SomeModel.run_rake("ts:reindex")
The key parts here are to require rake
and make sure you load the file containing the task definitions.
Most information obtained from http://railsblogger.blogspot.com/2009/03/in-queue_15.html
Related Topics
How to Sum Array of Numbers in Ruby
How to Convert a String Object into a Hash Object
How to Get the Current Absolute Url in Ruby on Rails
How to Reload the Current Page in Ruby on Rails
Ruby: How to Post a File Via Http as Multipart/Form-Data
What Does the (Unary) * Operator Do in This Ruby Code
Why Doesn't Ruby Support I++ or I-- (Increment/Decrement Operators)
Is Ruby Pass by Reference or by Value
Why Do Ruby Setters Need "Self." Qualification Within the Class
How to Generate a Random String in Ruby
Understanding the Rails Authenticity Token
How to Understand Nil Vs. Empty Vs. Blank in Ruby