Friday, 21 November 2014

Adding a crontab in ubuntu by capistrano task using Rails Application

How ever in practice sometimes we required to add a cronjob while deployment of the rails application.here an example of replace an exiting or create a new crontab by capistrano task.have a look:

there are following steps to reached to the required result.these are:

1. Create a recipe named 'cron.rb' inside the 'config/recipes' folder.
2. Add the following code to that 'cron.rb'

namespace :crontab do
  desc "Install cronjobs"
  task :install, roles: :desired_role do
    template "crontab.erb", "/tmp/cronjob"
    run('crontab /tmp/cronjob')
  end
  after "deploy:cold", "crontab:install"
end

// if you are dealing with multiple servers and want to add crontab on only one server then add an extra role to that server/machine in 'config/environment.rb' like

server 'ip_of_the_server', :web, :app, :db, :role_for_cron, primary: true 
example:

server 'ip_of_the_server', :web, :app, :db, :cron, primary: true

modify 'cron.rb' like this:


namespace :crontab do
  desc "Install cronjobs"
  task :install, roles: :cron do
    template "crontab.erb", "/tmp/cronjob"
    run('crontab /tmp/cronjob')
  end
  after "deploy:cold", "crontab:install"
end


3. Create a template named 'crontab.erb' inside 'config/recipes/templates'.
// dont forget to require 'crontab.erb'  into your deploy.rb file.

4. Add cronjob into 'crontab.erb'.for this add the following code in 'crontab.erb'

* * * * * /bin/bash -l -c "cd <%= current_path %> && bundle exec rake_path task_namesapce:task_name RAILS_ENV=rails_env" >

example,

* * * * * /bin/bash -l -c "cd <%= current_path %> && bundle exec /home/<%= user %>/.rvm/rubies/ruby-2.1.2/bin/rake test_task:test RAILS_ENV=<%= rails_env %>" >> <%= current_path %>/crontab.txt

// this above cronjob will execute every minute every hour and when it execute add an entry to 'crontab.txt' file.

5. Customizing you cronjob execution delay:

you can customize your cronjob execute delay.as example:

*/5 * * * * // before cronjob will execute job every five minute 
delay.

 





Friday, 7 November 2014

Sending emails in Rails using sendgrid

In most of the application you need to send bulk or single emails on a periodic time.like newsletter or digest or any other thing.for sending emails in your rails application you use SMTP servers to send emails.here is an example of sending emails by using sendgrid:

1). First of all you have to create a sendgrid account and make it provisioned.for sign-up in send grid you have to give a website(domain name) for the provision.if your website doesn't have enough content then your account can't be provisioned.
you will not able to send emails from sendgrid if your account is not provisioned.  

2). Goto https://sendgrid.com/developer after you logged in to your sendgrid account and copy SMTP settings.

3). In your environment.rb write the following settings:

 # Action mailer settings
  config.action_mailer.default_url_options = {:host => 'your_host_name(domain_name)'}

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
      :user_name => 'user_name',
      :password => 'your_password',
      :address => 'smtp.sendgrid.net',
      :port => 587,
      :authentication => :plain,
      :enable_starttls_auto => true
  }

now you emails sent from your application will be delivered via sendgrid.com

Handling Email Activity: 

you can login to your sendgrid account and see email activities.

1). Handling Bounce Emails: if you want to handle bounced emails then there are two ways to do:
a). Forword emails to a perticular email_id: if you want to send the bounced emails to another email-id then follow these steps:

click Email Reports ->Bounces -> settings

in the setting page click the check box and give the email-id on which you want to receive the bounced emails.

2). Handle post request from sendgrid: The other way to enable Event Notification and click on settings. on the settings page give an url and select events. when any of the selected event occur sendgrid send a post request on the given url with all possible parameters(ex. bounced,drop email information's). 

the url which you give above have to manage the post request.


Saturday, 1 November 2014

Immutable Objects with Object.freeze

One of the more common techniques in JavaScript is the use of an object to hold configuration values. The object might be accessed as a global or passed around as an argument. For example:

var artist = {
    name: "Johnny Cash",
    latestAlbum: "American V"
};

function announce (artist) {
    if (artist.name == "Johnny Cash") {
        console.log("Hello Johnny");
    } else {
        console.log(artist.name);
    }
}
announce(artist);
// Outputs: "Hello Johnny"

console.log(artist);
// Outputs: {
//     name: "Johnny Cash",
//     latestAlbum: "American V"
// }

But in either sort of situation, there is a problem. Functions that have access to the configuration object can modify the object, whether intentionally or accidentally. Suppose that you had a coworker modify the announce function above to highlight Elvis rather than Cash, but they mistyped the comparison.

var artist = {
    name: "Johnny Cash",
    latestAlbum: "American V"
};

function announce (artist) {
    // Whoops! Assigning the name rather than testing equality!
    if (artist.name = "Elvis Presley") {
        console.log("The King");
    } else {
        console.log(artist.name);
    }
}

announce(artist);
// Outputs: "The King"

console.log(artist);
// Outputs: {
//     name: "Elvis Presley",
//     latestAlbum: "American V"
// }

The Object.freeze method takes an object and renders it effectively immutable. Its existing properties may not be modified and new properties may not be added. In the example above this means that even though the logical error is still there, our artist object remains safe from modification and available for later use.
example:

var artist = {
    name: "Johnny Cash",
    latestAlbum: "American V"
};

Object.freeze(artist);

function announce (artist) {
    // Whoops! Assigning the name rather than testing equality!
    if (artist.name = "Elvis Presley") {
        console.log("The King");
    } else {
        console.log(artist.name);
    }
}

announce(artist);
// Outputs: "The King"

console.log(artist);
// Outputs: {
//     name: "Johnny Cash",
//     latestAlbum: "American V"
// }