Backgroundprocessing for Rails
Backgroundprocessing for Rails
- script/runner
- rake
- cron
- daemon
- run_later plugin
- spawn plugin
script/runner
- In Your Rails App root:
- script/runner “Worker.process”
rake
- In RAILS_ROOT/lib/tasks/dev.rake
rake dev:process
namespace :dev do
task :process do
#...
end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
cron
- Cron is a time-based job scheduler in Unixlike computer operating systems.
- crontab -e
daemon_controller
- A library for robust daemon management
- Make daemon-dependent applications Just Work without having to start the daemons manually.
off-load task via system command
# mailings_controller.rb
def deliver
call_rake :send_mailing, :mailing_id => params[:id].to_i
flash[:notice] = "Delivering mailing"
redirect_to mailings_url
end
# controllers/application.rb
def call_rake(task, options = {})
options[:rails_env] ||= Rails.env
args = options.map { |n, v| "#{n.to_s.upcase}='#{v}'" }
system "/usr/bin/rake #{task} #{args.join(' ')} --trace 2>&1 >> #{Rails.root}/log/rake.log &"
end
# lib/tasks/mailer.rake
desc "Send mailing"
task :send_mailing => :environment do
mailing = Mailing.find(ENV["MAILING_ID"])
mailing.deliver
end
# models/mailing.rb
def deliver
sleep 10 # placeholder for sending email
update_attribute(:delivered_at, Time.now)
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Simple Thread
after_filter do
Thread.new do
AccountMailer.deliver_signup(@user)
end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
run_later plugin
- Borrowed from Merb
- Uses worker thread and a queue
- Simple solution for simple tasks
run_later do
AccountMailer.deliver_signup(@user)
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
spawn plugin
spawn do
logger.info("I feel sleepy...")
sleep 11
logger.info("Time to wake up!")
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- By default, spawn will use the fork to spawn child processes. You can configure it to do threading.
- Works by creating new database connections in ActiveRecord::Base for thespawned block.
- Fock need copy Rails every time