Create Recurring job using delayed job

July 29, 2010 § 4 Comments

basically delayed job is using for background job. but we can use it as a recurring job.
here’s the step

first, install delayed_ojb gem. you can see how to install it at
http://github.com/collectiveidea/delayed_job

then add a new library to your rails apps directory ( /Libs ) e.g: jobs.rb

module Jobs
  module ScheduledJob
    def self.included(base)
      puts "WORK !!!"
      base.extend(ClassMethods)
    end

    def perform_with_schedule
      Delayed::Job.enqueue self, 0, self.class.schedule.from_now.getutc
      perform_without_schedule
    end

    module ClassMethods
      def method_added(name)
        if name.to_s == "perform" && !@redefined
          @redefined = true
          alias_method_chain :perform, :schedule
        end
      end

      def schedule
        @schedule
      end

      def run_every(time)
        @schedule = time
      end
    end

  end
end

after that, you can include your job class to your delayed_job script :

class recurring_job
include ScheduledJob

run_every 1.hours

  def perform
    puts "perform job"
  end
end

then run the job in the console :
RAILS_ENV=production script/delayed_job start
rake jobs:work

stop the job :
RAILS_ENV=production script/delayed_job stop

Advertisements

Tagged: , , , ,

§ 4 Responses to Create Recurring job using delayed job

  • Idris says:

    Hi, I’m new with ruby on rails.

    I have problem with delayed job in my application.
    I add delayed job when i send email. but gmail have daily quota, how can i get error, and delayed again that job tomorrow?!?

    Any suggest?!?

    thx

    • kubid says:

      Hi Idris,

      sorry for replyin your comment very late.. the notifications goes to my old email addres..
      I thought you have the answer for your question.. pelase share with me if you don’t mind 🙂

      thanks

  • daudi says:

    Thanks for the ideas .. a suggestion: comments and code indentation would help to understand what you’re trying to do in the code and also help the reader detect any typo errors.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Create Recurring job using delayed job at Kubid on rails blog.

meta

%d bloggers like this: