Thursday, December 11, 2008

The Best Ruby/SVN IDE setup for Hardy Heron

After experimenting with Aptana, Ganymede/Eclipse, the Ubuntu Eclipse package, and Netbeans on Ubuntu Hardy Heron 8.04, I came up with the following conclusions.

Aptana:

* It does too much -- there are lots of plugins that pop up by default, and every once in a while you get annoyed by interactive ruby consoles and windows that want you to install various gems.
* The CSS editor and JS editors are slow on large files
* Otherwise, it's a great IDE -- with good SVN support (subclipse) and good ruby hilighting (RDT)

Ganymede/Eclipse:

* I got the latest version (3.4) and installed dynamic languages support and subversive (which is now the default SVN plugin).
* The subversive commit window fetches from the repository when doing diffs -- way too slow.
* I ran into an occasional and hard-to-reproduce bug where funky characters were inserted at the end of a line instead of a real line break (when cutting/pasting)
* Dynamic languages support screws up auto-indenting

Netbeans:

* Really good ruby highlighting
* Tries to do a little too much with rake tasks, ruby console, and gem plugins
* Nice and small download
* SVN integration doesn't work for me because you can't easily checkbox a few selective files, and you can't easily perform diffs on the commit dialog box

Ubuntu Eclipse Package:

* Ubuntu ships with version 3.2. It will not work well with Java 6
* sudo vi /etc/eclipse/java_home (move /usr/lib/jvm/java-1.5.0-sun to the top of the list)
* Add eclipse update repositories: http://subclipse.tigris.org/update_1.2.x, http://update.aptana.com/install/rdt/3.2/
* Install Ruby Development Tools, Subclipse, Web Standard Tools
* Runs like a charm. Stable, and all features I want.

Wednesday, November 5, 2008

Ubuntu Linux Command Line Background Magic (without screen)

This is useful when you are in an ssh session and you want to background a long-running task and log out. The job will continue running after you log out, and you can even log back in later and resume it. This solution does not require screen.

~$ nice -n10 -- super_long_running_script
...

Now say that the script is taking for ever, and you need to kill your ssh session. First, hit + Z

...
[1]+ Stopped(SIGTSTP) nice -n10 -- super_long_running_script
~$

This will stop the task (but not kill it) and give you a console again. Note that the [1] indicates that the task is indexed as job 1. Next, we want to resume the task again, but in the background:

~$ bg 1
[1] nice -n10 -- mysql -u root bamboo_staging <backup_20081105.sql &
~$

the number "1" corrolates with the job number above. The task is now running again in the background. Note that if you log out now bash will kill the task because bash kills its children on exit. To prevent bash from killing the child, do:

~$ disown -h 31968

Where 31968 is the PID of the backgrounded process. Now you can exit your ssh shell:

~$ exit
...

Note that your ssh session doesn't really close properly. However it is now safe to kill your ssh client. The backgrounded process will happily keep running.

Ubuntu/Debian and nfs mount issue solved

I spent a while trying to figure this one out: nfs client mounts were very slow on one of my servers. The dmesg output kept saying:


portmap: server localhost not responding, timed out
RPC: failed to contact portmap (errno -5).


Also, rpcinfo was not working for me:


~$ sudo rpcinfo -p 127.0.0.1
rpcinfo: can't contact portmapper: RPC: Remote system error - Connection refused
~$ sudo rpcinfo -p localhost
rpcinfo: can't contact portmapper: RPC: Remote system error - Connection refused


NFS mounts were working but were extremely slow:


~$ sudo mount hostname:/path

...


I was convinced that the problem lied with my hosts file, firewall, or hosts.allow or hosts.deny. It turns out that the problem was simply that a few packages had to be installed:


~$ sudo apt-get install nfs-common
...
Selecting previously deselected package libevent1.
Selecting previously deselected package libnfsidmap1.
Selecting previously deselected package nfs-common.

Tuesday, October 21, 2008

Unique Random Numbers & Random Rows from Large ActiveRecord Tables

I was looking for an easy way to get unique and random numbers. In my case, I need a small quantity of numbers from a large pool. Here is the best solution I could come up with (derived from a message board):

def unique_randoms(n, max = nil)
seen = {}
n.times{x = rand max; seen[x] ? redo : seen[x] = 1}
seen.keys
end

It's a little more terse than it needs to be. Here is a more explicit version:

def unique_randoms(n, max = nil)
seen = {}
n.times do
x = rand max
redo if seen[x]
seen[x] = true
end
seen.keys
end

If you need a large number of randoms it is faster to use the Array sort method:

def unique_randoms(n, max)
(0..max).sort{rand}[0..n]
end

This leads us to a solution to quickly getting random rows through ActiveRecord:

def random(scope, num_randoms)
count = scope.count
unique_randoms(num_randoms, count){|i| scope.first :offset => i}
end

The above solution is appropriate for large result sets where :order => 'RAND()' is inefficient, and for small quantities of random rows. Each random row adds a query to this solution.

Monday, June 2, 2008

How to create missing subversion tmp files

After importing a project into my IDE, subversion started giving this error:

svn: Your .svn/tmp directory may be missing or corrupt; run 'svn cleanup' and try again

Unfortunately, 'svn cleanup' was unable to fix this problem.

I was able to create all of the required .svn temporary directories with the following command:

find . -iname '.svn' -exec mkdir {}/tmp \;