This morning, I needed to find a way to sort some Data on the front-end or back-end, depending on the solution I came up with. The day before, I had written this whole super-cool implementation for the sorting which worked nicely with PDOs (PHP Data Objects) and my model objects. Thing is, as far as sorting the data is concerned, there should be a easier and faster way which reduces the load on the server (both database and page serving) and lets the client do all the work.
I had a explored a few of the lovely jQuery solutions and despite my love for jQuery, I thought they were overkill. I just found a solution that takes about 5 minutes to implement and customize and that works brilliantly.
Here's the link: http://yoast.com/articles/sortable-table/
There is only one catch though: I am lucky with this, the amount of data I am pushing back and forth and very small and will remain small; however, if you will be doing some sort of pagination, this script is not a good idea as it only sorts the data on the current page. Meaning that the data on the further pages are not sorted. So, if you had a "Z" somewhere in your records, because the sorting is not happening on the database end, it will not come up as top or first record. Just something to keep in mind.
Cheers!
10 March 2009
Super-Easy Sortable Table
Posted by
Jean-Paul
at
8:22:00 AM
0
comments
Links to this post
15 November 2008
mod_rails (Passenger) setup issues with Apache2 on OS X
mod_rails not finding Apache2 APR
When installing mod_rails with a custom Apache2 setup, you may get the following while running sudo passenger-install-apache2-module:
Compiling and installing Apache 2 module...
cd /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3
rake clean apache2
(in /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3)
rake aborted!
Could not find Apache Portable Runtime (APR).
/usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3/rakefile:37
(See full trace by running task with --trace)
The main reason for this is that mod_rails cannot find apr-1-config which should have been installed within (as per my system) /usr/local/apache2/bin. In order to solve the problem, you just need to add the Apache2 custom install bin directory to your user path (as per my system):
export PATH=":/usr/local/apache2/bin:$PATH"
That should do it.
mod_rails and "MACOSX_DEPLOYMENT_TARGET environment variable"
The error you are most likely to get would be close to the following:
g++ -flat_namespace -bundle -undefined dynamic_lookup Utils.o Bucket.o Logging.o System.o Configuration.o Hooks.o mod_passenger.o -fPIC -o mod_passenger.so -lstdc++ -lpthread ../boost/src/libboost_thread.a -L/usr/local/apache2/lib -lapr-1
/usr/bin/ld: flag: -undefined dynamic_lookup can't be used with MACOSX_DEPLOYMENT_TARGET environment variable set to: 10.1
collect2: ld returned 1 exit status
rake aborted!
Command failed with status (1): [g++ -flat_namespace -bundle -undefined dyn...]
/usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.3/rakefile:142
(See full trace by running task with --trace)
In this case, you will have to add the following to your environment variables
export MACOSX_DEPLOYMENT_TARGET=10.4
Posted by
Jean-Paul
at
9:23:00 AM
0
comments
Links to this post
Labels: apache 2, apr-1-config, mod_rack, mod_rails, OS X, passenger, Ruby on Rails
08 May 2008
Guido Sohne
Have you ever thought that death would come through email? I just heard the saddest and most shocking news today: a very good friend of mine, Guido Sohne passed away. I hadn't seen him in years since I left Ghana, but every time he came to SA, he'd give me a call and we'd talk about various issues on the phone. He's always told me that I had to learn how to relax and take life as it comes. He had always been a reference point for me, a very unique individual and with whom one could enjoy any kind of discussion.
I still cannot believe he's gone. So long Guido.
Posted by
Jean-Paul
at
8:51:00 AM
1 comments
Links to this post
12 March 2008
Scaling Rails with Apache 2, mod_proxy_balancer and Thin Clusters
Recently I have been working on a rails application that is now about to be deployed. One of my concerns about this app was to be able to make it really fast and responsive to the users while still loading a huge amount of database records and serving a relatively large amount of pages.
As far as making database calls faster is concerned, there are two things that work here:
- Whenever possible, use Model.find(:all/:first, :select => "model_table.column_1, model_table.column_2)" to only get what you want and not all the stuff ActiveRecord loads for you about the model's instance.
- Second, you might want to have a look at memcached and how to use it with rails. In order to facilitate the use of the memcached, you could either look at the acts_as_cached plugin or the cached_model gem. A good tutorial for using cached_model can be found here.
However, the purpose of this post is not to discuss database calls performance issues so I will reserve this for another article and focus on the matter at hand: scaling rails through Apache, Load Balancing and the Thin ruby server.
Thin
Thin is a fast Ruby HTTP server that puts together 3 excellent libraries and thus brings the best of all worlds: the Mongrel Parser (which Mongrel is based on), Event Machine and Rack. When I first read about Thin, I thought: Yet another RoR server. But I got curious, downloaded it, set it up on my development machine and ran the application I was working on with it. I was amazed at how much faster everything got. Just a click and your page would load in an instant. I liked the improvement over the original Mongrel. First, I assume we would want to get up and running with Thin.
Installing and running Thin
$ sudo gem install thin
All the dependencies will be downloaded with. The next step is to run it and appreciate the speed differences.
- CD into your app's directory
- Once in there, type "thin start".
By default, you will be able to access your application on http://localhost:3000. So nothing has changed as per normal. Give it a ride.
In case you want to run a cluster of say 5 servers, you would do so with the following command from within the directory of your rails app:
$ thin start -s5
To look at all available options and the features Thin offers, try:
$ thin --help
You can stop the cluster started above by running:
$ thin stop -s5
So, we have managed to get Thin up and running and we love the speed gains as well as the simplicity. Thin can be downloded here: http://code.macournoyer.com/thin/.
We'll now move on to the next step: getting Apache running with mod_proxy / mod_proxy_balancer and mod_proxy_http
Apache 2.2 and mod_proxy_balancer
If you did not have apache or are willing to recompile
If you do not have Apache on your machine or would prefer recompiling apache with mod_proxy, all you have to do is to download it, tar -xvzf (if file extension is tar.gz) or tar -xjf (if file extension is tar.bz2) the tarball and run a command similar to the following:
(assuming you've untar-red httpd-2.2.4)
$ cd httpd-2.2.4
$ ./configure --enable-modules=most --enable-shared=max --enable-ssl --enable-deflate --enable-headers --enable-proxy --disable-dav --prefix=/usr/local/apache2 --with-included-apr --with-apxs2
$ make
$ sudo make install
I have to say, there are a million ways of configuring Apache, all depending on what your needs are and there are a million tutorials on the web for the same. So I wouldn't waste too much ink on this. Find what works best for your environment.
If you already have apache and do not want to recompile
Now, for those who already have Apache 2 installed and wouldn't want to recompile from scratch, you can dynamically load the required modules in. But you still need the source code. Assuming you've gotten it and untar-red it as per above, the following commands would help you do just that:
(note that the APXS path that I am using is based on the configuration of my system. You will have to adapt it to your own)
$ cd httpd-2.2.4
$ cd modules/proxy
$ /usr/local/apache2/bin/apxs -i -a -c mod_proxy.c
$ /usr/local/apache2/bin/apxs -i -a -c mod_proxy_balancer.c
$ /usr/local/apache2/bin/apxs -i -a -c mod_proxy_http.c
If everything worked out fine, you should have those modules installed in the modules directory of your Apache 2 configuration. On my system, that means "/usr/local/apache2/modules". Check if yours are there. The next step would be to tell Apache to load those modules on startup. First, make sure Apache isn't running by shutting it down:
$ /usr/local/apache2/bin/apachectl stop
Next, edit "httpd.conf" that you will find at "/usr/local/apache2/conf/httpd.conf" and add the following lines to it:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
So, at this point, we've got Apache 2 running and configured with all the nice ingredients for load balancing.
Let's move on to step 3!
Configuring Apache for Thin clusters
I assume we're still in the "httpd.conf" file we were editing earlier. Let's scroll to the bottom of it.
Let's configure the load balancer
<Proxy balancer://super_production_balancer>
BalancerMember http://127.0.0.1:3000
BalancerMember http://127.0.0.1:3001
BalancerMember http://127.0.0.1:3002
</Proxy>
Note that the balancer can be called anything. It's just a reference name that can be used later when we're configuring our virtual host or something similar.
I have 3 BalancerMember(s). This assumes that I know the ports at which my clusters are going to run on and which IP addresses. Although, this example is very much focused on the "localhost", you could provide IP addresses to others servers within your network. There is even a special option to add more load to a server which you assume has more hardware to support that type of load. In such example we would have something such as:
<Proxy balancer://super_production_balancer>
BalancerMember http://127.0.0.1:3000
BalancerMember http://127.0.0.1:3001
BalancerMember http://127.0.0.1:3002
BalancerMemeber http://192.168.0.176:9001 loadfactor 4 #supermarchine this is
</Proxy>
Let's configure the virtual host
<VirtualHost *:80>
ServerAdmin info@superapp.com
ServerName www.production.superapp.com
ServerAlias production.superapp.com
ProxyPass / balancer://super_production_balancer/
ProxyPassReverse / balancer://super_production_balancer/
</VirtualHost>
All we did above is to give our app the possibility to be accessed via a normal HTTP request through any of the following domain names "www.production.superapp.com" or "production.superapp.com". From here, assuming you working on a "localhost" system, you will need to edit "/etc/hosts" to tell your computer where to find production.superapp.com. It goes as follows
127.0.0.1 production.superapp.com
Finally...
We're almost done. So assuming that we wanted to have a cluster of 3 Thin servers running in production mode, we would go back into our rails app directory and type in the following:
$ thin start -s3 -e production
It's now time to tell your users to go to http://production.superapp.com and have fun using your brand new super-scalable application!
Have fun!
Note: This is just an overview of what can be done in a very easy way. There is a lot more that you can do with the tools described above and I would recommend you research more on it.
Links:
Posted by
Jean-Paul
at
4:31:00 AM
0
comments
Links to this post
Labels: admin, apache 2, load balancing, mod_proxy_balancer, rails, ruby, scalability, servers, thin
12 November 2007
Google's Android: Paradise for mobile developers?
Alright, I am too excited not to post about this. So, like me and millions of other mobile developers out there, you've heard or seen Google's mobile operating system named Android and what it can do. I have just started downloading the SDK. I am so excited about this, I do not think I will be sleeping over the next few days. I've already dispatched a few mails out to some of my friends asking them to check it out and their thoughts. I wish we could have a discussion about it right here on this blog to make it public. I will try to set this up in the next few days.
Here are my initial thoughts so far:
- The architecture is awesome and clean.
- Java is on top of everything: I've got nothing new to learn except use the supplied APIs.
- It's built for developers and to allow people to do serious stuff on mobile devices. After one full year of doing exactly this kind of development and having come to the conclusion that there is too much fragmentation in it as well as the fact that device manufacturers do not necessarily build them for developers to take full advantage of the device capabilities, this new initiative from Google is more than welcome.
- Interesting stuff: SQL-Lite for persistent storage, Location Manager API, XMPP API built in, the concept of Intents and what the developer can do in the middle of the end-user navigating from one intent to the other, the Application Service Manager, and of course with WebKit, we don't have to worry anymore about javascript support on mobile phones (AJAX and all that...) and many more interesting goodies...
- This seems to be the mobile development paradise I've been looking for all along.
Here are a couple of links to start checking out:
The Android SDK: http://code.google.com/android
Some very nice videos: http://www.youtube.com/androiddevelopers
Some discussions I've started about the possibility of Apple releasing a version of their iPhone device with Android installed on it: New iPhone Software Next Year?
To conclude this, let's see what I come up with over the next few days of playing around with it. Seriously, I love the fact that I cannot sleep much anyways. Time to hack the hell out of "ze Android"!
Posted by
Jean-Paul
at
10:36:00 PM
0
comments
Links to this post
08 November 2007
Gmail upgrades and now fully supports Safari
Yay!!! I cannot believe it took Google 2/3 flipping years to get this right. They've upgraded Gmail. I instantly noticed the changes this morning. They're subtle, but if you're one of those people who pays attention to details and can notice an additional pixel in a picture, you'll notice those changes in an instant.
So anyways, after I noticed the upgrades, I thought, hey, maybe they finally got Gtalk to work in Safari. Ladies and gentleman, I am happy to announce that in fact they did!!
Not having GTalk in Safari has prevented me from using it for my daily browsing activities as I needed to keep track of my mail but also chat with collaborators, classmates, etc...
It's really nice to see all the upgrades happening right at this time of the year, whether from Apple, Google and other software service providers.
-- Cheers!
Posted by
Jean-Paul
at
6:28:00 AM
0
comments
Links to this post
12 October 2007
Google Code for Educators
Really interesting. I don't know how many people know of this but I thought I should point it out here on the blog. I think there are a few invaluable resources there.
Here: http://code.google.com/edu/
Personally, I am more interested in the Distributed Computing and Web Security stuff. I think those are very important fields to research in the future. Of course there is already quite some research going on in there but as we're moving towards a more integrated and inter-connected world, all of those above will become more important and will be great skills to develop and to have.
Cheers!
Posted by
Jean-Paul
at
10:58:00 PM
0
comments
Links to this post
Labels: Google, Google Code, Google engEDU