tag:blogger.com,1999:blog-37167004176937798552024-03-21T06:17:54.644-07:00Solution @ Ur DeskVigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-3716700417693779855.post-79303797370334859912014-08-31T06:46:00.001-07:002014-08-31T08:24:20.510-07:00Ruby Date & DateTime class extension<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
My recent user story demanded me to extend Date and DateTime classes to support few more methods.<br />
<div>
<br /></div>
<div>
Following is the code and this is done using Ruby's Dynamic Proxy Technique.<br />
<div>
<br /></div>
</div>
</div>
<script src="https://gist.github.com/vigram/47c9f420623dc278cc62.js"></script></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
Copy above date.rb file and keep it under initializer directory of your Rails application.
</div>Vigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com0tag:blogger.com,1999:blog-3716700417693779855.post-58203952763957963852014-03-03T02:39:00.000-08:002014-03-03T03:22:18.203-08:00Ruby Threads + ActiveRecord == "Too Many Connections" ?<div dir="ltr" style="text-align: left;" trbidi="on">
<style type="text/css">
.code_style {
background : #E3E3E3;
border-radius : 10px;
font-family : arial;
padding : 10px;
}
</style>
<br />
<div style="text-align: left;">
I recently encountered Mysql "Too Many Connections" issue in my Rails App, which makes my App down periodically. When I checked Mysql processlist, I could see <b>150+ active DB connections</b> established from Rails App. How could my Rails App can open 150+ connections, though I mentioned AR connection pool size as 30. Baffled me !!!</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Following is my development environment,<br />
<i><br /></i>
<i>Ruby(2.0) + Rails(4.0) + Unicorn + Nginx </i> + <i>SideKiq + MultiTenant</i></div>
<div>
<ul style="text-align: left;">
<li>No.of Unicorn workers: 5</li>
<li>No.of SideKiq processes: 1</li>
<li>AR Connection pool size: 30 </li>
</ul>
</div>
<div>
According to experts, it would establish <b>5 * 1 + 30 = 35</b> maximum connections. Then why is it making 150+ connections in my App? Started Researching !!!<br />
<br />
Found a magical sentence saying that the<b> above calculation is true only for single threaded applications</b>. Oh Yes! In my Rails app, I am spawning application level Threads based on no.of records being processed. So each Thread will try to make a connection to DB. Look at the following example,<br />
<br />
<div class="code_style">
class Article < ActiveRecord::Base<br />
def self.publish_all<br />
threads = []<br />
Article.by_unpublished.find_in_batches(batch_size: BATCH_CONST) do |articles|<br />
threads << Thread.new do<br />
<b>ActiveRecord::Base.connection_pool.with_connection</b> do<br />
articles.each do |article|<br />
article.publish<br />
end<br />
end<br />
end<br />
end<br />
threads.map(&:join)<br />
end<br />
end</div>
<br />
According to Rails Doc '<b>with_connection</b>' method will Check-In the connection back to connection pool once after executing the Block given. But in the above example,<b> it's not releasing the connection rather it holds as active connection. So further requests to server keep creating new connections</b>, this strange behaviour is the root cause of connection leak issue.<br />
<br />
Solution !!!<br />
<br />
Yes you are right, Check-In the connection explicitly. So I created a utility class and wrapped around Thread invocation. For an example,<br />
<br />
<div class="code_style">
class ThreadUtility<br />
def self.with_connection(&block)<br />
begin<br />
yield block<br />
rescue Exception => e<br />
raise e<br />
ensure<br />
# Check the connection back in to the connection pool<br />
<b> ActiveRecord::Base.connection.close</b> if ActiveRecord::Base.connection<br />
end<br />
end<br />
end<br />
<br />
</div>
<br />
<div class="code_style">
class Article < ActiveRecord::Base<br />
def self.publish_all<br />
threads = []<br />
Article.by_unpublished.find_in_batches(batch_size: BATCH_CONST) do |articles|<br />
threads << Thread.new do<br />
<b>ThreadUtility.with_connection</b> do<br />
articles.each do |article|<br />
article.publish<br />
end<br />
end<br />
end<br />
end<br />
threads.map(&:join)<br />
end<br />
end</div>
<br />
Above example completely solved my connection leak issue, <b>No more "Too Many Connections"</b> notice from server.<br />
<br />
Another strange behaviour which I would like to share here from above example is, whenever server spawning new thread, it creates new connection rather than reusing existing connection from pool of connections already available. This is due to<b> the nature of ActiveRecord's Lazy connection pooling technique</b>(connections are opened lazily). Once the no.of established connections in pool reaches the pool size mentioned in config, it grabs and reuses the connections from pool.<br />
<br />
Now tell me guys, how many maximum connections that AR can establish from the above code fix for following configuration,<br />
<ul>
<li>No.of Unicorn workers: 5</li>
<li>AR Connection pool size: 10</li>
</ul>
Is it 10 ? But the truth is 5 * 10 = 50 connections. Bcoz each unicorn process can establish 10 maximum connections in multi threaded application.</div>
<div>
<br />
Your valuable comments are always Welcome !!!<br />
<br />
<br /></div>
</div>Vigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com4tag:blogger.com,1999:blog-3716700417693779855.post-84952229263491560992013-12-13T05:18:00.000-08:002014-12-11T04:45:20.865-08:00Rake task to restart Sidekiq process<script src="https://gist.github.com/vigram/dacea832651b87935f6d.js"></script>Vigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com0tag:blogger.com,1999:blog-3716700417693779855.post-13727830913179305042012-12-20T01:38:00.000-08:002014-03-02T01:06:12.643-08:00Rake task to update build version No. with latest tag name<style type="text/css">
.code_style {
background : #E3E3E3;
border-radius : 10px;
font-family : arial;
padding : 10px;
}
</style>
The following code will help us to automate the process of updating build version No. by considering latest tag name available in git repo.<br />
<br />
Assuming, you are deploying your Rails app using Capistrano.<br />
<br />
1) In your deploy.rb add the following hook,<br />
<br />
<div class="code_style">
<i>after "deploy", "installed_version:update"</i><br />
</div>
<br />
And add a task(<i>installed_version:update)</i><br />
<br />
<div class="code_style">
<i>namespace :installed_version do<br /> task :update, :roles => :app, :except => { :no_release => true } do<br /> run "cd #{current_path} && RAILS_ENV=#{rails_env} rake update:version"<br /> end<br />end </i><br />
</div>
<br />
2) Create a new rake task file in your rails app(<i>lib/tasks/update_build_version.rake</i>)<br />
<br />
Add below lines,<br />
<br />
<div class="code_style">
<i>namespace "update" do<br /> desc "This will update the installed version no. in DB from the latest tag name"<br /> task :version => :environment do<br /> tag = `git rev-list --tags --max-count=1`<br /> tag_name = `git describe --tags #{tag}`.chomp<br /> puts "Latest Tag No.#{tag_name}"<br /> attributes = {:version_no => tag_name, :build_dt => Time.now}<br /> Version.create(attributes) </i><i><i># assuming you have versions tbl</i> </i><br />
<i> puts "Build updated with Version No.\"#{tag_name}\""<br /> end<br />end</i><br />
</div>
<br />
<br />That's all, we are done !!!<br /><br />
<b>Note:</b> Before every deployment you need to tag your code with some version No.(ex: 1.2.1) and then run <i>cap <instance_name> deploy</i>. The above code will read the latest tag name(1.2.1) and update into versions table.<br />
<br />
This post would be really helpful when you want to automate your Build Management process.Vigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com0tag:blogger.com,1999:blog-3716700417693779855.post-4478178074491254662012-12-11T23:54:00.001-08:002012-12-12T00:16:12.607-08:00Rake Task with Arguments<span style="font-family: Verdana,sans-serif;">The following example shows how to pass an argument to Rake Task. Here version No(:no) gets passed to task and the same gets saved into versions table. This example can be incorporated with your build management script to update the current version No. for each release.</span><br />
<br />
<span style="font-family: inherit;"><i>namespace "build" do<br /> desc "This will update the version No."<br /> task :version, [:no] => :environment do |t, args|<br /> raise "Message: Version Number Required to build !!!" if args.<span style="font-family: "Courier New",Courier,monospace;">no</span>.nil?<br /> version = Version.first # assume you have a table named versions(:id, :version_no, :updated_at...)<br /> version.update_attributes(:version_no => args.no)<br /> puts "Build updated with Version No.#{args.no}"<br /> end<br />end</i></span><br />
<br />
<b>Usage:</b> <i>rake "build:version[1.3.2]" </i>Vigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com0tag:blogger.com,1999:blog-3716700417693779855.post-46999761741419507832012-11-27T22:39:00.000-08:002012-12-26T21:51:44.740-08:00Git Tag<b>Tag (people use this functionality to mark release points)</b><br />
<br />
<i>git tag </i> -- Listing Your Tags<br />
<br />
<i>git tag -l ’v1.4.2.*’</i> -- wildcard Tag listing<br />
<br />
<i>git tag v1.4 </i> -- Creates Lightweight Tag<br />
<br />
<i>git tag -a v1.4 -m ’my version 1.4’</i> -- Creates Annotated Tag<br />
<br />
<i>git tag -s v1.5 -m ’my signed 1.5 tag’</i> -- sign your tags with GPG, assuming you have a private key.<br />
<br />
<i>git show v1.4</i> -- Shows the last commit of a Tag<br />
<br />
<i>git tag -a v1.2 9fceb02</i> -- Tagging later (if you forgot to Tag a release point)<br />
<br />
<i>git push origin v1.2</i> -- Sharing Tags (pushing to remote origin)<br />
<br />
<i>git push origin --tags</i> -- Transfer all of your tags to the remote server that are not already there<br />
<br />
<i>git tag -d v1.2</i> -- Delete a Tag<br />
<br />
<i>git checkout -b v1.4-fixes v1.4</i> -- Creates a branch(v1.4-fixes) from Tag(v1.4)<br />
<br />
<i>git diff v1.1 v1.2 --stat</i> -- Shows no of files changed between two tags<br />
<i><br />git log --pretty=oneline v1.1..v1.2 -- </i>Shows no of commits between two tagsVigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com1tag:blogger.com,1999:blog-3716700417693779855.post-20079665709338577162010-09-13T22:35:00.000-07:002010-11-11T01:59:14.683-08:00Desktop to Mobile site switching Apache configRecently i have come across a situation to write Apache config for Desktop to Mobile site switch and vice-versa.<br />
<br />
Below are the Constraints,<br />
<br />
<ol><li>Lets assume we have two sites, one for Desktop(web version 'www.domain.com') and another for Mobile(mobile version 'mobile.domain.com').</li>
<li>If i view my site from Desktop then Desktop version should appear, similarly if i view the same site from any mobile device then it should automatically redirected to Mobile site.</li>
<li>Important constraint here is, user should be allowed to change their preference from desktop to Mobile version and vice-versa by the link provided in both the sites('Mobile version','Web version'). Based on the preference set by user, proper version should appear when user Re-visits the site.<br />
</li>
<li>Moreover, if user is in 'web version' under search page, from here if user clicks 'Mobile version' then in Mobile site same search page with result should appear.</li>
</ol>In order to implement the above scenarios, i have searched in Google many times but couldn't get help, so i started writing by my own (as below),<br />
<br />
<span style="font-weight: bold;">In your Web version site Vhost file</span>:<br />
<br />
<span style="font-size: 85%;">RewriteCond %{REQUEST_URI} ^/switch_to_mobile<br />
RewriteRule ^/switch_to_mobile$ - [co=version:mobile:.domain.com]<br />
RewriteCond %{HTTP_REFERER} <a class="moz-txt-link-freetext" href="http://%5c/%5C/post.incbase.net%5C/%28.*%29">http:\/\/www.domain.com\/(.*)</a><br />
RewriteRule ^/switch_to_mobile$ <a class="moz-txt-link-freetext" href="http://m.post.incbase.net/%1">http://</a><a class="moz-txt-link-freetext" href="http://%5c/%5C/post.incbase.net%5C/%28.*%29">mobile.domain.com</a><a class="moz-txt-link-freetext" href="http://m.post.incbase.net/%1">/%1</a> [R=302,L]<br />
<br />
RewriteCond %{HTTP_COOKIE} version=mobile [NC]<br />
RewriteRule ^(.+)$ <a class="moz-txt-link-freetext" href="http://m.post.incbase.net$1/">http://</a><a class="moz-txt-link-freetext" href="http://%5c/%5C/post.incbase.net%5C/%28.*%29">mobile.domain.com</a><a class="moz-txt-link-freetext" href="http://m.post.incbase.net$1/">$1</a> [L,R=302]<br />
<br />
RewriteCond %{HTTP_USER_AGENT} BlackBerry<br />
RewriteCond %{HTTP_COOKIE} !version=web<br />
RewriteRule ^(.+)$ <a class="moz-txt-link-freetext" href="http://m.post.incbase.net/$1">http://</a><a class="moz-txt-link-freetext" href="http://%5c/%5C/post.incbase.net%5C/%28.*%29">mobile.domain.com</a><a class="moz-txt-link-freetext" href="http://m.post.incbase.net/$1">/$1</a> [R=302,L]</span><br />
<br />
<span style="font-weight: bold;">In your Mobile version site Vhost file</span>:<br />
<br />
<span style="font-size: 85%;">RewriteCond %{REQUEST_URI} ^/switch_to_web<br />
RewriteRule ^/switch_to_web$ - [co=version:web:</span><span style="font-size: 85%;">.domain.com</span><span style="font-size: 85%;">]<br />
RewriteCond %{HTTP_REFERER} <a class="moz-txt-link-freetext" href="http://%5c/%5C/m.post.incbase.net%5C/%28.*%29">http:\/\/</a><a class="moz-txt-link-freetext" href="http://%5c/%5C/post.incbase.net%5C/%28.*%29">mobile.domain.com</a><a class="moz-txt-link-freetext" href="http://%5c/%5C/m.post.incbase.net%5C/%28.*%29">\/(.*)</a><br />
RewriteRule ^/change_to_web$ <a class="moz-txt-link-freetext" href="http://post.incbase.net/%1">http://</a><a class="moz-txt-link-freetext" href="http://%5c/%5C/post.incbase.net%5C/%28.*%29">www.domain.com</a><a class="moz-txt-link-freetext" href="http://post.incbase.net/%1">/%1</a> [L,R=302] <br />
<br />
RewriteCond %{HTTP_COOKIE} version=web [NC]<br />
RewriteRule ^(.+)$ <a class="moz-txt-link-freetext" href="http://post.incbase.net$1/">http://</a><a class="moz-txt-link-freetext" href="http://%5c/%5C/post.incbase.net%5C/%28.*%29">www.domain.com</a><a class="moz-txt-link-freetext" href="http://post.incbase.net$1/">$1</a> [L,R=302]</span><br />
<br />
<span style="font-size: 85%;"> (or) Even simpler you may try the below config</span><br />
<span style="font-size: 85%;"><br />
</span><br />
<span style="font-weight: bold;">Web version:</span><br />
<br />
<span style="font-size: x-small; font-weight: bold;"> </span><span style="font-size: x-small;">RewriteCond %{REQUEST_URI} ^/change_to_mobile<br />
RewriteCond %{HTTP_REFERER} http:\/\/www.domain.com\/(.*)<br />
RewriteRule ^/change_to_mobile$ http://mobile.domain.com/%1 [R=301,L]<br />
<br />
RewriteCond %{HTTP_USER_AGENT} iphone|ipod|alcatel|android|midp|240x320|blackberry|dopod|htc|huwai|lg|midp|nec|netfront|nokia|panasonic|portalmmm|sharp|sie-|sony|sonyericsson|symbian|benq|mda|mot-|motorola|palm|panasonic|philips|sagem|samsung|sanyo|sharp|sda|sgh-|t-mobile|vodafone|xda|pocket\ pc|opera\ mini|windows\ ce [NC]<br />
RewriteCond %{HTTP_COOKIE} !preference=web [NC]<br />
RewriteRule ^(.+)$ http://mobile.domain.com$1 [R=302,L]</span><span style="font-weight: bold;"> </span><br />
<span style="font-weight: bold;"><br />
</span><br />
<span style="font-weight: bold;"> Mobile version:</span><br />
<br />
<span style="font-size: x-small;">RewriteCond %{REQUEST_URI} ^/change_to_web$</span><br />
<span style="font-size: x-small;">RewriteRule ^/change_to_web$ "$0" [co=preference:web:.domain.com]</span><br />
<span style="font-size: x-small;">RewriteCond %{HTTP_REFERER} http://mobile.domain.com/(.*)</span><br />
<span style="font-size: x-small;">RewriteRule ^/change_to_web$ http://www.domain.com/%1 [R=302,L]</span><span style="font-weight: bold;"> </span><br />
<br />
Hope above configuration should help you Guys !!!Vigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com9tag:blogger.com,1999:blog-3716700417693779855.post-76033073531930074662010-04-13T23:54:00.000-07:002010-04-13T23:58:50.752-07:00FIX: extconf.rb:8:in `require’: no such file to load — mkmf (LoadError)When i try to install fastthread in my new system,<br /><br />gem install -v=1.0.7 fastthread<br /><br />I got the following error,<br /><br />/usr/bin/ruby1.8 extconf.rb<br />extconf.rb:8:in `require': no such file to load -- mkmf (LoadError)<br />from extconf.rb:8<br /><br />The solution is to install ruby1.8-dev,<br /><br /><code>$ sudo apt-get install ruby1.8-dev<br /></code><br />Now try.<br /><br />Hope it should work fine for you as well.Vigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com0tag:blogger.com,1999:blog-3716700417693779855.post-55969906602302295972008-07-03T03:33:00.000-07:002010-04-14T00:04:21.752-07:00Apache Rewrite for underscore to hyphenI have faced an url issue in my webapplication.<br /><br />I was asked to rewrite the requests for the html pages which has "_" ,<br /><br />Example :<br /><br /><div style="text-align: center;"><span style="font-size:85%;"><span style="font-style: italic;font-family:verdana;" >http://mydomain.com/test/resource</span><span style="font-weight: bold; font-style: italic;font-family:verdana;" >_</span><span style="font-style: italic;font-family:verdana;" >with</span><span style="font-weight: bold; font-style: italic;font-family:verdana;" >_</span><span style="font-style: italic;font-family:verdana;" >underscore.html</span></span><br /><br /> <span style="font-size:85%;">(to)</span><br /><br /><span style="font-size:85%;"><span style="font-style: italic;">http://mydomain.com/test/resource</span><span style="font-weight: bold; font-style: italic;">-</span><span style="font-style: italic;">with</span><span style="font-weight: bold; font-style: italic;">-</span><span style="font-style: italic;">underscore.html</span></span><br /></div><br /><br />the following rule will work fine for the above scenario,<br /><br /><br /><br />RewriteRule ^/([^_]+)_([^_]+)_([^_]+)$ /$1-$2-$3 [L,R=301] #<span style="font-size:85%;">pages with 2 underscores</span><br />RewriteRule ^/([^_]+)_([^_]+)$ /$1-$2 [L,R=301] #<span style="font-size:85%;">pages with 1 underscore</span><br /><br />Ref:<span style="font-size:85%;"> http://underscorebleach.net/jotsheet/2005/04/perl-301-redirect-script</span>Vigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com0tag:blogger.com,1999:blog-3716700417693779855.post-21184911498775478172008-02-14T21:49:00.000-08:002008-02-14T21:50:53.199-08:00Search for file with a specific name in a set of files (-name)<blockquote> <kbd>find . -name "rc.conf" -print </kbd> </blockquote> <p> This command will search in the current directory and all sub directories for a file named <i>rc.conf</i>. </p> Note: The -print option will print out the path of any file that is found with that name. In general -print wil print out the path of any file that meets the find criteria.Vigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com0tag:blogger.com,1999:blog-3716700417693779855.post-8334248667823545892008-01-23T01:57:00.000-08:002008-01-23T02:05:11.132-08:00How to find Linux Version ?For Ubuntu,<br /> Following file will contain the Linux version<br /><br /> /etc/issueVigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com1tag:blogger.com,1999:blog-3716700417693779855.post-35760230253447355662008-01-08T23:14:00.001-08:002008-01-08T23:20:21.749-08:00uninitialized constant Gem::GemRunner (NameError)I think,this error is due to upgrading your gem version.<br /><br />To reproduce this issue, follow the steps below<br /><br />1> whereis gem<br /><br /> Ex : /usr/bin/ruby/gem ...and so on..<br /><br />2> delete all the listed files & directories (only gem related files..dir)<br /><br />3>Once again install gem-0.9.5<br /><br />4> Now ur problem get solvedVigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com0tag:blogger.com,1999:blog-3716700417693779855.post-59178830563704726592007-12-18T05:19:00.000-08:002010-04-14T00:05:04.958-07:00How to open IIS server from Command promptgoto command prompt -> ctrl+r<br /><br />Then type -> inetmgr<br /><br />This will open IIS server.Vigramhttp://www.blogger.com/profile/03340130273893769022noreply@blogger.com1