How do I create a background task for a rake task. I want it run every 15 minutes.
My statistik.rake in lib/tasks
desc "Importer statistikker"
namespace :reklamer do
task :iqmedier => :environment do
require 'Mechanize'
agent = WWW::Mechanize.new
agent.get("http://www.iqmedier.dk")
form = agent.page.forms.first
form.Username = 'username'
form.Password = 'password'
form.submit
agent.page.link_with(:href => "/Publisher/Stats").click
form = agent.page.forms.first
form.submit
@stats = agent.page.search('//tr')[-2].search('td').map{ |n| n.text }
@existing = Reklamer.where(dato: @stats[0]).first
if @existing.nil?
Reklamer.create!(:virksomhed => 'Iqmedier', :dato => @stats[0], :unik_klik => @stats[1], :klik => @stats[2], :unik_vis => @stats[3], :vis => @stats[4], :leads => @stats[5], :ordre => @stats[6], :cpc => @sta开发者_如何学JAVAts[7], :earn => @stats[8])
elsif @existing.dato != Date.today
Reklamer.create!(:virksomhed => 'Iqmedier', :dato => Date.today, :unik_klik => 0, :klik => 0, :unik_vis => 0, :vis => 0, :leads => 0, :ordre => 0, :cpc => 0, :earn => 0)
else
@existing.update_attributes!(:unik_klik => @stats[1], :klik => @stats[2].to_i, :unik_vis => @stats[3], :vis => @stats[4], :leads => @stats[5], :ordre => @stats[6], :cpc => @stats[7], :earn => @stats[8])
end
end
end
Depending on your architecture, the simplest way in the Linux world is to set up a cron job:
Create a file in /etc/cron.d
and stick this in it:
*/15 * * * * your_app_user cd /your/app/path; rake reklamer:iqmedier RAILS_ENV=production
This will run the job every 15 minutes (due to the */15
).
精彩评论