Posted by Dhaval Parikh on Jun 21, 2009
Do you have a process which takes a long amount of time before you display the result to the user? First of all make sure that your code is correct and you are following Rails standards. If thats alrite then you should do that process in background. Doing a process in background means that you start a asynchronous process as a separate process thread which keeps on running in background thus not interfering in user process. (Hope this clarifies what i mean by background process)
Now the question is how to do it and how its beneficial ??
Well by doing a background process you don’t keep a user waiting on your site. this helps the user to visit other parts of your site and since he gets faster responses he gets attached to your site and thats what you want being a website owner.
Now how to do it is the main thing. Well there are couple of plugins which claims to do this type of tasks.
BackgrounDRb
BackgroundFu
Bj
But My Favourite is SPAWN plugin availabe on http://github.com/tra/spawn/tree/master
Although the contents below are mentioned on the above link I m putting it here for your reference.
Spawn
=====
This plugin provides a 'spawn' method to easily fork OR thread
long-running sections of code so that your application can return
results to your users more quickly.This plugin works by creating new
database connections in ActiveRecord::Base for the spawned block.
The plugin also patches ActiveRecord::Base to handle some known
bugs when using threads (see lib/patches.rb).
Usage
-----
Here's a simple example of how to demonstrate the spawn plugin.
In one of your controllers, insert this code
(after installing the plugin of course):
spawn do
logger.info("I feel sleepy...")
sleep 11
logger.info("Time to wake up!")
end
If everything is working correctly, your controller should finish quickly
then you'll see the last log message several seconds later.
If you need to wait for the spawned processes/threads, then pass the
objects returned by spawn to Spawn::wait(), like this:
N.times do |i|
# spawn N blocks of code
spawn_ids[i] = spawn do
something(i)
end
end
# wait for all N blocks of code to finish running
wait(spawn_ids)
If you want your forked child to run at a lower priority than
the parent process, pass in the :nice option like this:
spawn(:nice => 7) do
do_something_nicely
end
By default, spawn will use the fork to spawn child processes.
You can configure it to do threading either by telling the spawn
method when you call it or by configuring your environment.
For example, this is how you can tell spawn to use threading on the call,
spawn(:method => :thread) do
something
end
When using the :thread setting,
spawn will check to make sure that you have set
allow_concurrency=true in your configuration.
If you want this setting then
put this line in one of your environment config files:
config.active_record.allow_concurrency = true
If it is not set, then spawn will raise an exception.
To (optionally) configure the spawn method in your configuration,
add a line to your configuration file(s) like this:
Spawn::method :thread
If you don't set any configuration, the :method will default to :fork. To
specify different values for different environments, pass the environment as
the 2nd argument:
Spawn::method :fork, 'production'
Spawn::method :yield, 'test'
This allows you to set your production and development
environments to use different methods according to your needs.
Forking vs. Threading
---------------------
There are several tradeoffs for using threading vs. forking.
Forking was chosen as the default primarily because it requires
no configuration to get it working out of the box.
Forking advantages:
- more reliable? - the ActiveRecord code is generally not
deemed to be thread-safe.
Even though spawn attempts to patch known problems with the
threaded implementation, there are no guarantees.
Forking is heavier but should be fairly reliable.
- keep running - this could also be a disadvantage,
but you may find you want to fork
off a process that could have a life longer than its parent.
For example, maybe you
want to restart your server without killing the spawned processes.
We don't necessarily condone this (i.e. haven't tried it)
but it's technically possible.
- easier - forking works out of the box with spawn,
threading requires you set allow_concurrency=true.
Also, beware of automatic reloading of classes in
development mode (config.cache_classes = false).
Threading advantages:
- less filling - threads take less resources...
how much less? it depends.
Some flavors of Unix are pretty efficient at forking
so the threading advantage may not be as big as you think...
but then again, maybe it's more than you think.
- debugging - you can set breakpoints in your threads
I guess now its more clear to you what is Threading,Forking & bg procee
Tags: background process in rails, forking, threading
Posted by Dhaval Parikh on Jun 10, 2009
There are many articles posted under this topic but none of them have all information or a Step by Step guide of how to integrate OAuth for use with Twitter Apps and Rails.
So thought of posting one here. Here are few steps which you should follow. The code below is same as provided by the twitter wiki but i have made couple of changes and organized it to make it simple to understand
Step 1 : = You need to get your consumer key and consumer secret # from the twitter OAuth site under this link twitteroauth.
Step 2 := Install OAuth Gem with “gem install oauth” command on ur terminal
Step 3 := Generate a Scaffold using the following command and then migrate the table called “users” to ur database
ruby script/generate scaffold user screen_name:string token:string secret:string
rake db:migrate
Step 4 := Add the following code to your User Controller. Replace the key and secret code with yours
def self.consumer
# The readkey and readsecret below are the values you get during registration
OAuth::Consumer.new(“OmwO7wsjtYHjquu6bd6C4w”, “j1kZ6yzsqChkeQtToErUx2LnPQMsSPkXMkiy4F82sPA”,{ :site=>”http://twitter.com” })
end
def create
@request_token = UsersController.consumer.get_request_token
session[:request_token] = @request_token.token
session[:request_token_secret] = @request_token.secret
# Send to twitter.com to authorize
redirect_to @request_token.authorize_url
return
end
Now this is an important part where you need to set the call back url Which u will define while you register your app at twitter OAuth site. Again add this to the UserController
def callback
@request_token = OAuth::RequestToken.new(UsersController.consumer,
session[:request_token],
session[:request_token_secret])
# Exchange the request token for an access token.
@access_token = @request_token.get_access_token
@response = UsersController.consumer.request(:get, ‘/account/verify_credentials.json’,
@access_token, { :scheme => :query_string })
case @response
when Net::HTTPSuccess
user_info = JSON.parse(@response.body)
unless user_info['screen_name']
flash[:notice] = “Authentication failed”
redirect_to :action => :index
return
end
# We have an authorized user, save the information to the database.
@user = User.new({ :screen_name => user_info['screen_name'],:token => @access_token.token,:secret => @access_token.secret })
@user.save!
# Redirect to the show page
redirect_to(@user)
else
RAILS_DEFAULT_LOGGER.error “Failed to get user info via OAuth”
# The user might have rejected this application. Or there was some other error during the request.
flash[:notice] = “Authentication failed”
redirect_to :action => :index
return
end
end
end
As you can see from the comments this callback action exchanges the request token for an access token. Since the user is going to need to type in the username and password at Twitter I avoid prompting for it and instead we fetch that using a call to verify_credentials. This saves duplicate entry and makes it easier on the user. This also prevents users with multiple accounts from giving you one username and then using another when they login to twitter. The access token and secret are what is needed to act on behalf of a user, so those are saved to the database. The show action then uses this information to display some data like so:
def show
@user = User.find(params[:id])
# Get this users favorites via OAuth
@access_token = OAuth::AccessToken.new(UsersController.consumer, @user.token, @user.secret)
RAILS_DEFAULT_LOGGER.error “Making OAuth request for #{@user.inspect} with #{@access_token.inspect}”
@response = UsersController.consumer.request(:get, ‘/favorites.json’, @access_token,
{ :scheme => :query_string })
case @response
when Net::HTTPSuccess
@favorites = JSON.parse(@response.body)
respond_to do |format|
format.html # show.html.erb
end
else
RAILS_DEFAULT_LOGGER.error “Failed to get favorites via OAuth for #{@user}”
# The user might have rejected this application. Or there was some other error during the request.
flash[:notice] = “Authentication failed”
redirect_to :action => :index
return
end
Finally the views
<p>
<b>Screen name:</b>
<%=h @user.screen_name %>
</p>
<ul>
<% @favorites.each do |fav| %>
<li><b><%= fav['user']['screen_name'] %></b>: <%=h fav['text'] %></li>
<% end %>
</ul>
and
the new.html.erb
<h1>New user</h1>
<% form_for(@user) do |f| %>
<%= f.error_messages %>
<p>
We’ll be sending you to Twitter in a moment to login and grant us access.
Once you allow us in we’ll give you the super-cool feature
you’bve heard about.
</p>
<p>
<%= f.submit “Grant Access” %>
</p>
<% end %>
<%= link_to ‘Back’, users_path %>
Also Add validates_presence_of to the User model for screen_name, token and secret.
And in the routes.rb set the following route
map.connect ‘/callback’, :controller => ‘users’, :action => ‘callback’
Thats it you are ready for the twitter Login. Start the server and open localhost:3000/users
and you will see the magic.
These are all the steps. Post me a comment if you still face any isssues.
Tags: OAuth + twitter + rails, OAuth integration with rails, rails OAuth and Twitter, ruby on rails
Posted by Dhaval Parikh on Jun 5, 2009
I just found a gr8 tool to measure page loading speed which is an important aspect for any website. initially google used to use this plugin for the internal measurement but now its open to all and i think every web developer should use it.
I just installed it and found that its similiar to Yslow. Here is the detail as per the google page speed site
|
What is Page Speed?
Page Speed is an open-source Firefox/Firebug Add-on. Webmasters and web developers can use Page Speed to evaluate the performance of their web pages and to get suggestions on how to improve them. |
|
How does Page Speed work?
Page Speed performs several tests on a site’s web server configuration and front-end code. These tests are based on a set of best practices known to enhance web page performance. Webmasters who run Page Speed on their pages get a set of scores for each page, as well as helpful suggestions on how to improve its performance. |
|
Why should you use Page Speed?
By using Page Speed, you can:
- Make your site faster.
- Keep Internet users engaged with your site.
- Reduce your bandwidth and hosting costs.
- Improve the web!
|
Looks a gr8 and a promising tool. Inorder to try it visit http://code.google.com/speed/page-speed/download.html and njoy
Tags: Firebug addon, google page speed, Technology News
Posted by Dhaval Parikh on May 20, 2009
Hey all
After a long time I am posting something about stock markets. The reason is everyone was bearish and no one was ready to listen to ne thing negative so I stopped posting. And thats 100% true. Now I am seeing people coming back (especially my friends who were trading aggressively in the last bull run) and asking me about their holdings and talking about stock markets especially after the 2 upper circuits on back of election results.
Just before few weeks most of the analyst described every rally as a bear market rally. No one was expecting the NIFTY to cross 3800 levels. Everyone seemed to be bearish on market. Everyone was backing their views by talking about bad economic conditions, negative global cues blah blah.. Now with the election results things seem to have changed totally. People are no longer talking about global cues, economic condition and it seems that bulls have returned into business. Now suddenly India seems to have decoupled from global markets. and with the same govt coming into power people have again started talking about 8% GDP and so on.
What has changed in these few days that people have forgotten every thing and are just buying blindly? Brokerages are talking about 20k levels on sensex http://in.news.yahoo.com/48/20090520/1237/tbs-market-watchers-predict-new-high-by.html. FII’s suddenly overweight on India and you will see every thing turned Bullish from Bearish. (Especially those who were bearish just 2 weeks back
This sounds really funny to me but most importantly its very confusing for those who are stuck with their holding which they bought when markets was @ 18k-20k and are thinking to hold or sell.
According to me the current movement in the market is because of over excitement and is overdone. People are putting in money blindly since there is a left out feeling. Every thing is moving up.. Personally I don’t feel that this rally should sustain. People already holding stocks should book out profits and stay with cash. “CASH IS KING” and no one will argue that. Profit booking to my mind is never a bad thing to do. If your investments have doubled book out capital and hold on to the profits. I just noticed that even companies which have posted net loss are moving up 10% – 20% every day. Reminds me of the last leg of the bull rally when each and every single stock was moving.
Ne ways having said that I think one must book profits and we could see nifty coming back to 3800 levels or even less going forward. And as and when economic conditions improve one should again start investing or buy stocks which are available at dirt cheap price going forward.
Though I cannot term this move as a bear market rally or a bull market but I am sure about one thing that the valuations doesn’t seem to be cheap any more and economic environment has not changed. So profit booking will be the ideal strategy.
Hope you have liked this post. If you have any questions pls post a comment and I shall reply.
Thanks
Tags: bear market rally, new bull run, nifty views, Stock Markets
Posted by Dhaval Parikh on May 20, 2009
Most of the new websites are having the feature of localization or internationalization on their site. With introduction of rails 2.3 this feature is provided by default But if you are still working in rails 2.0 or 2.1 then GlobalLite is the solution for you.. http://code.google.com/p/globalite/
How ever while implement it in one of my projects I noticed that the localized date time module used in the plugin has a problem when you have time_select tag in your form.
So when you use a time_select tag in your form it will generate an error message “Can’t convert nil into string”. The problem is in the file called localized_action_view.rb which you will find in plugins/globalite/lib/rails folder.
Apply the code below to get it fixed
def select_month(date, options = {}, html_options = {})
if options[:locale]
@original_locale = Locale.code
Locale.code = options[:locale]
end
val = date ? (date.kind_of?(Fixnum) ? date : date.month) : ”
if options[:use_hidden]
hidden_html(options[:field_name] || ‘month’, val, options)
else
month_options = []
monthnames = :date_helper_month_names.l
abbr_monthnames = :date_helper_abbr_month_names.l
month_names = options[:use_month_names] || (options[:use_short_month] ? abbr_monthnames : monthnames)
month_names.unshift(nil) if month_names.size < 13
1.upto(12) do |month_number|
month_name = if options[:use_month_numbers]
month_number
elsif options[:add_month_numbers]
month_number.to_s + ‘ – ‘ + month_names[month_number]
else
month_names[month_number]
end
month_options << ((val == month_number) ?
%(<option value=”#{month_number}” selected=”selected”>#{month_name}</option>\n) :
%(<option value=”#{month_number}”>#{month_name}</option>\n)
)
end
@selector = select_html(options[:field_name] || ‘month’, month_options, options)
Locale.code = @original_locale if options[:locale]
return @selector
end
# Locale.code = @original_locale if options[:locale]
# return @selector
end
You may even Comment the date module in the code plugins/globalite/lib/rails/localized_action_view.rb
this will make use of default rails date time. so nothing will get affected if you do this. But the solution mentioned about is a better one.
Njoi the power of Internationalization and make sure site global.
Tags: globalite plugin, globalite plugin error, internationalization in rails, localization in rails, ruby on rails, time_select tag error