title: How to create a background task for rake task?
body: <p>How do I create a  background task for a rake task. I want it run every 15 minutes.</p>
  
  <p>My statistik.rake in lib/tasks</p>
  
  <pre><code>desc "Importer statistikker"
  namespace :reklamer do
    task :iqmedier =&gt; :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 =&gt; "/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 =&gt; 'Iqmedier', :dato =&gt; @stats[0], :unik_klik =&gt; @stats[1], :klik =&gt; @stats[2], :unik_vis =&gt; @stats[3], :vis =&gt; @stats[4], :leads =&gt; @stats[5], :ordre =&gt; @stats[6], :cpc =&gt; @stats[7], :earn =&gt; @stats[8])
            elsif @existing.dato != Date.today
            Reklamer.create!(:virksomhed =&gt; 'Iqmedier', :dato =&gt; Date.today, :unik_klik =&gt; 0, :klik =&gt; 0, :unik_vis =&gt; 0, :vis =&gt; 0, :leads =&gt; 0, :ordre =&gt; 0, :cpc =&gt; 0, :earn =&gt; 0)
            else
              @existing.update_attributes!(:unik_klik =&gt; @stats[1], :klik =&gt; @stats[2].to_i, :unik_vis =&gt; @stats[3], :vis =&gt; @stats[4], :leads =&gt; @stats[5], :ordre =&gt; @stats[6], :cpc =&gt; @stats[7], :earn =&gt; @stats[8])
            end
    end
  end
  </code></pre>