Cron Job

Cron is a time based job scheduler. Cron job is a job scheduled to run at particular time. In rails you may need to create cron jobs in many situations. For example send account expiration mail, check account balance and inform the user, etc. There is a gem 'Whenever', which allows you to create a cron job.

Whenever

Using whenever you can easily create cron jobs. First you have to install the gem:
gem install whenever

Then from your application path type:
whenevrize .
This will create the file 'config/schedule.rb'.

In scedule.rb file you can write your cron job:
every 1.day, :at => '12:00 AM' do
   runner "Model.method"
end 
this will run the method inside the Model everyday at 12:00 AM.


For more details visit GitHub

Read More...

Delayed Job

Delayed Job is a plugin used to execute long running jobs in background. For example, downloads, uploads, external interactions etc. And you can also specify when to start a delayed job.

To install delayed_job plugin:

script/plugin install git://github.com/tobi/delayed_job.git

After installation you need to create a table for the delayed job to save the jobs.

script/generate migration create_table_for_delayed_job

And add the following script to the migration file.

class CreateTableForDelayedJob < ActiveRecord::Migration
    def self.up
       create_table :delayed_jobs, :force => true do |t|
          t.integer :priority, :default => 0
          t.integer :attempts, :default => 0
          t.text :handler
          t.text :last_error
          t.datetime :run_at
          t.datetime :locked_at
          t.datetime :failed_at
          t.string :locked_by
          t.timestamps
       end
    end

    def self.down
       drop_table :delayed_jobs
    end
end



Now run the migration:

rake db:migrate


To add a job to delayed_job you can use the enqueue method of Delayed Job. First you have to create a Job Class, for example we need to send a notification mail using delayed job. Then we create a lib file with the content:

Class DelayedNotificationMail
   attr_accessor :id


   def initialize(id)
      self.id = id
   end


   def perform
     UserMailer.deliver_notification_mail (User.find(id))
   end
end

Now we can add job to the delayed_job using:

Delayed::Job.enqueue DelayedNotificationMail.new(@user_id), 0, 1.hour.from_now


To run the jobs in background you have to execute the rake task:


rake jobs:work


This will start a process which will run the jobs in the queue, and will run the job according to the delay specified in the job.

To clear all the jobs in queue you can use the rake task:

rake jobs:clear

For more details you can go to the GitHub

Read More...

dependent

'dependent' is an option used with association in rails.

Association
An association is the relationship between models. There are associations like one-one, one-many, many-many. To represent association in rails, it uses has_many, belongs_to, has_one and has_and_belongs_to_many.

For Eg: Account model contains many Members. It is represented as:

In Account Model:
has_many :members

In Member Model:
belongs_to :account

dependent
In the above example if we delete one record from the accounts table it will affect the members table. The members table will contain the invalid account_id. In these situations we can use 'dependent'.

In Account Model:
has_many :members, :dependent => :destroy

It will automatically deletes all the dependent data from the models.

Read More...