Pages

Wednesday 8 May 2013

Pushing Existing Folder/Project To Github

                                      If we need to push an existing project/folder to Github. We can do it with out cloning the empty repository. First we need to create a repository in github. It is good if the name of the repository is same as the parent folder that contains the data to be push. After creating the repository we will get an url of that repository, For eg: https://github.com/user_name/repository_name


After this enter into the project folder and do the following steps.
git init
git remote add origin https://github.com/user_name/repository_name
git add file_names
git commit added_file_names
git push origin master
git pull origin master

After the first pull and and push you can do the git operations as usual. Here in the above command we are using remote add 'origin', besides 'origin' if you are using 'master' rr some other name.It will also work. But when we pull or push,The command will change, instead of origin we need to give the name that we provided at the time of remote add    

Tuesday 7 May 2013

Queue Based Delayed Job

Delayed Job, also known as DJ, makes it easy to add background tasks to your Rails applications. Delayed_job  encapsulates the common pattern of asynchronously executing longer tasks in the background. The main situations we use delayed job are sending mails solr reindexing,uploading  and parsing big files and other background jobs.
                                               Delayed job 3.x only support rails 3.x. The main feature of delayed job 3.x is queue based serving. We can run delayed job with or without queue parameter. The main advantage of running job with queue is, we can run more than one job simultaneously. So we can categorise delayed job to take care specific group of task.
                                               For example in an application we are using delayed job for sending mails it includes the activation mail that we send immediately after registration and other mails. If we are using single job it works on FIFO manner, so the activation mail only go after processing the early added jobs. But if we are running two delayed jobs process with different queue names,eg: "activation"  only for activation mails and "messages" for all other mails. Then the activation job only take care of activation mails and it will send with out any delay.

                                                Here I am using delayed job with active record.

Installation   

gem 'delayed_job'
gem 'delayed_job_active_record'
gem 'daemons'

Then bundle install . The back end requires a delayed job table, we can creat it using the following command
rails generate delayed_job:active_record
rake db:migrate
The first command create a migration for creating the delayed_jobs table. When we calling a delayed job it is added to this table and it is helpfull to kep track of the process and finding the errors.

Upgrading from delayed_job 2.x to 3.x on Active record

First we need to change the gem file. The main difference between2.x and 3.x is the latter introduces a new column to the table. So run the upgrade generator to create migration to add the column.
rails generate delayed_job:upgrade
rake db:migrate
The first command will create a migration to add one column to the table. If it is showing any error while running the best way to upgrade is create a migration manually to add the column.
rails generate migration add_column_to_delayed_job
Edit the migration file to add the column. After editing migration file will look like
def up
  add_column :delayed_jobs,:queue,:string
end

def down
  remove_column :delayed_jobs, :queue
end

Starting Delayed Job with Queue Parameter

The command for staring queue based delayed job is giving below
 RAILS_ENV=(environment:development or staging or production) script/delayed_job -i=(Serial Number:1,2,3....) --queue=(Queue name) start 
We can start number of jobs with different queue name. The main thing we need to take care is the serial number should be different for each job.Otherwise the same job will be replaced to another queue name.

Queueing Jobs

Call .delay.method(params) on any object and it will be processed in the background. For eg:
@user.send_mail(@name)
@user.delay.send_mail(@name)
By doing this the action will execute in background. When we user queue based system and running more than one job the queueing will like below. We need to pass the queue name to understand which job should take care of the corresponding action.
@user.delay(:queue => 'activation').send_activation_mail(@name)
@user.delay(:queue => 'messages').send_reminder_mail(@name)
Here the first action will executed by the job which started with the queue name 'activation' and the second with queue name 'messages'


For detailed reference please follow the link Delayed Job