My delayed job is not working. I am trying to create an background task for a rake file using a delayed job that should run every 15 minute. I have copied my rake tasks in m
If I understand correctly what you're trying to do, then you should have the following:
scraper.rb
require 'mechanize'
class Scraper
def perform
[:iqmedier, :euroads, :mikkelsen, :orville].each{|a| send(a)}
end
def iqmedier
# some code
end
def mikkelsen
# some code
end
def orville
# some code
end
end
in your controller:
require 'delayed_job'
class MyController < ApplicationController
def runall
Delayed::Job.enqueue(Scraper.new, :run_at => 5.minutes.from_now)
end
end
For adding jobs from outside of rails, you can write something similar and then run it with rails runner:
require 'scraper'
Delayed::Job.enqueue Scraper.new
Delayed::Job.enqueue Scraper.new, :run_at => 15.minutes.from_now
Delayed::Job.enqueue Scraper.new, :run_at => 30.minutes.from_now
Delayed::Job.enqueue Scraper.new, :run_at => 45.minutes.from_now
If you just want to run code every X minute/hour, than you don't need delayed_job for that. That would be kind of overkill and over engineered. Just use this code here:
until 2 < 1 do
if Time.now.min % 15 == 0
puts "Execute your code here!"
if Time.now.min % 15 == 0
sleep(60)
end
end
end
Put the code into Rakefile, make a task out of it and deploy it on a heroku worker dyno. That will work. Here is a complete tutorial for that: http://robert-reiz.com/2012/06/12/cron-jobs-on-heroku/.
For that solution you even don't need Herokus Cron or Scheduler Add-On. And you don't need any additional GEMs like delayed_job. Pragmatic solution. Keep it slim ;-)