Cleverfoo

Rafael Ferreira's blog

Async delegates in… bash!

This may not surprise some of you but, believe it or not, you can write very simple asynchronous delegates in plain old bash. Yeah that’s right, BASH.

Here’s the scenario, let’s say you have to perform a task that might take a long time (let’s pretend you need to check the health of an nfs server prior to attempting to mount exports from it) but you do not want to block the main “process” waiting for it, here’s how something like that can be implemented:

###################################################
# ASYNC NFS CHECK
# Rafael Ferreira <raf@ophion.org>
###################################################

# CONFIG FLAG - whether NFS has been checked or not
NFS_OK=1

# Runs a command asynchronously
# $1 is the command to run
# $2 is the callback function
async_run() {
        {
                $1 &>/dev/null

                # calling the callback passing the result
                $2 $?
        }&
}

nfs_callback() {
        if [ $1 == "0" ]
        then
                touch /tmp/$$
        fi
}

for i in $(mount -l  -t nfs | grep nfs2 | awk -F ":" '{print $1}')
do
        if [ $NFS_OK == "1" ]
        then
             async_run "/usr/sbin/rpcinfo -p $i" nfs_callback
             disown

        fi
done

sleep 1s

if [ -f /tmp/$$ ]
then
        echo "NFS OK"

else
        echo "NFS NOT OK"
fi

In a nutshell, async_run() is where all the action happens. It takes a string parameter of a command to be run and a callback function to be dispatched once the command is done. On the example above, I decided to block the main process and wait for the async call to return for at most 1 sec, this allows me to have constant execution O(1), no matter how long the async task takes.

Yeah, I know this is pretty silly, but hey, I like it.

And for the Arse of the Year Award…

This guy Mark Fink should get the Arse of the Year Award for the most obtuse gnome mailing list post:

http://mail.gnome.org/archives/desktop-devel-list/2008-February/msg00131.html

My fav quote from that whole thread:

””I’ve never written a program before so I also need some help. Also I need a place to put it on the web””

We are indeed doomed….

Mike Huckabee pulls a “Howard Dean”

This might turn out to have the same effect to Mike Huckabee’s bid for the White House as Howard Dean’s now infamous “rahhh!”.

http://firstread.msnbc.msn.com/archive/2008/01/15/579265.aspx

Now, to be clear, I’m not putting Howard Dean’s deranged scream in equal footing with Mr. Huckabee’s comments but I do see a parallel that could lead to the same kind of political turmoil.

Fundamentally, I see two things wrong with Huckabee’s stance:

a) He manages to display complete lack of respect towards the US Constitution and the principles in which it was based
b) He shows a lack of insight into the complexity of Christianity and how one must quantify what “God’s standards” mean in light of the different interpretations of the Bible (I must say this one is extra troublesome coming from a “minister”)

Well, enough said. Let’s all seat back and watch.

Quote of the day

“Given enough eyeballs, all bugs are shallow.” – Eric S. Raymond

No, I’m not dead.. Uva Software is unveiled

For the last year or so, along with my dual Master’s degree, I’ve been working with a partner on what I think is a long overdue addition to the educational marketplace in the US, an open source educational software company providing software that aims at shaking up things and really increase the value of what you get today when you think educational software.

[url]http://www.uvasoftware.com[/url]
Open Educational Middleware

We have just released version 0.1 of our flagship product: Uva Core an open source SIF agent library.

That’s it… now it’s official…. carry on…