RubyDubee

  • Archive
  • RSS

Heroku + Godaddy combination is crazy!

Well, the time for this post cannot be more dramatic; One of my heroku app is down like many others but that is not why i am writing this post and that is not the topic for the post either. You might not have got much from the post title yet, but let me tell you when you read this post you will surely agree with it!

First of all, Heroku is great! Easy deployment, easily scalable; surely one of the best cloud hosting platform for rails application. We have been using Heroku since last one year and it works! Our app was on Heroku’s Bamboo stack for quite some time. A month ago, we had a lot of traffic and decided to serve static homepage, which went well and it served its purpose. Then after couple of weeks we decided to migrate our application to Cedar stack. Again all went well! Now last week, I removed the static index page and got back to rails for serving our homepage. Didn’t work! Heroku was still serving the static page from cache, I tried redeploying the app as heroku says, “Cache will be cleared on every push”. Didn’t work! Somehow i found that we had not updated our CNAME record for “www”, so changing the record from “proxy.heroku.com” to “proxy.herokuapp.com” solved the issue for www subdomain. Awesome!

Now, we use wildcard subdomains(*.example.com) as referral links for our users and we point it to our homepage. Heroku recommends adding CNAME record for wildcard subdomains and Godaddy does not allow it, but it does allow adding A record for wildcards which works fine. Now I have “A” record for wildcard subdomain pointing to one of the IP Addresses from heroku. The problem is, it shows me the old static page! WTF? HOW? I searched and searched! Everything failed! Then i found that the homepage was being served through Varnish which is not available for Cedar stack, that means somehow the page was being served through the old bamboo app’s cache, there you go! I pushed(deployed) the bamboo app and it cleared its cache! Ta-Da everything worked! But How on earth am I supposed to get this? Something is seriously wrong!

Here are some of the takeaways : 

  • If you are using wildcard subdomain, you should always use CNAME records (link)
  • If you have to use A record, you must redeploy your bamboo app to clear Varnish cache for static assets after migrating to Cedar stack.

Hope it will help someone! Cya!

    • #ruby
    • #heroku
    • #rails
  • 7 months ago
  • 1
  • Comments
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

Flash cookies finally found its use!

Ola folks!

We have all used flash cookies in rails and I am sure you liked them. Let me tell you, after reading this, you will like them even more!

Flash is a part of session which will be cleared with every request, so whatever values you have set in the flash will not be available on the next request. That is why most rails developers use it to store error/success messages for that request. Let me show another couple of more useful use cases where you can use flash storage :).

  1. You have an internal groups or communities which user can join. Now a guest user comes to a group page and clicks on join. User will be redirected to the Login page and after login, he will have automatically be joined the group and redirected to the group page again. How will you achieve this? One simple and working approach is to append request parameter like group_id to login request, which doesn’t look good(I don’t find it attractive to have a group_id variable attached to login url). The other option is to set session cookie for group_id. The drawback is, if user changed his mind, he will not login and browse around the site. But let’s say without closing the browser, he logs in after a while! Boom! He is now a part of that Group and he will be redirected to groups page because the session cookie was there and not cleared when he left the login page. So there is your use case, you want a storage which will be cleared if user decides not to login and browse different page instead. So instead of setting session cookie, set a flash!
  2. This use case is for developers using authlogic for user authentication. When user enters wrong credentials on his login page, he is redirected to “/user_sessions” (i.e. POST request to user_sessions_controller) and he will be shown the error messages and login page will be rendered there. All is well, but the URL is still /users_sessions, which it should not be(it should be /login or something). So now I went inside user_sessions_controller and for failure condition, redirected the request to login_path instead of rendering the login form. What bad happened is I lost the @user_session object and thus all the error_messages. So there is your use case. Set the @user_session object in flash and retrieve it on login( “new” method ).

Thanks for reading this far!

    • #Rails
  • 10 months ago
  • Comments
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

Tiny MVC for J2EE

I know, you already know how to write MVC pattern using Servlet/JSP. But I didn’t and I know many of you still don’t know it! People use frameworks even if they don’t want that much complicated setup. For smaller webapps, you don’t need frameworks, they make your app heavy. So mind it and let’s start writing our own MVC pattern using Servlet/JSP. YaY!!

First of all, you need a Controller, yes only one! Let’s call it a FrontController(because some frameworks do). All the requests should come to this controller and it will then delegate these requests to individual Action(again!). Actions take care of processing a request and dispatching it to the JSP, which acts as a view.

Let’s add our FrontController in web.xml

and now lets write our FrontController, Action and a sample AccountAction which will display user’s name through account.jsp.

Here we map a request to its corresponding action, I have done this using requestURI(), you can use different techniques as well, like sending hidden parameter “action”. But this way looks much cooler to me.

AccountAction process your business logic, it will communicate with your Database layer and create a bean of your data(or may be multiple beans). The request will now be dispatched to the jsp file(your View) and along with it goes your data(bean) which is set on the request through setAttribute(..)

Try this! Extending this application is very easy and maintaining it will be even more easier! Happy Coding!

    • #Java
    • #MVC
    • #Design Patterns
  • 1 year ago
  • Comments
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

Observer and Ducks

If you have implemented Observer pattern in any statically typed language, you know that you need an explicit Observer class/interface for the pattern, which will be extended by the actual concrete classes and some ‘update’ kind of method should be overridden in order to propagate the observations.Basically taking advantage of inheritance.

Here comes a duck type language like Ruby, as there are no types to be declared you can pass in any object to the observable and let it call the update method, you don’t need any parent Observer class. If the method is available in the object it will be called and the observation will be accomplished.

Now we can extend this more, The observable object will call any method it wants and ruby’s method_missing will take care of the methods that the individual observers don’t want to define. Cool eh? Check out this simple example:

So I quote this again “If it walks like a duck and quacks like a duck, it must be a duck.”

    • #ruby
    • #design patterns
  • 1 year ago
  • 1
  • Comments
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+
QR Code!
View Separately

QR Code!

  • 1 year ago
  • Comments
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

Game of life in Ruby

Here is the code, I think this needs to be written in more stylish way. If you have any suggestions please comment.

    • #game
    • #algorithm
    • #Ruby
  • 1 year ago
  • 13
  • Comments
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+
Page 1 of 4
← Newer • Older →

About

I am a programmer and I like to build new stuff, usually in Java or Ruby. My interest lies in Web applications, Databases, APIs and things like that! I have a B.Tech degree in Computer Science.
In my spare time, I read(anything ranging from tech-books to mythology), listen to music or play Table Tennis. (And I walk a lot)!

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Mobile
Effector Theme by Pixel Union