Tuesday, May 1, 2012

Hot Swapping Code on a Running Erlang Node

Today's random and extremely short post covers the topic of hot swapping code on an Erlang node. I'm writing this because I've chosen to use Erlang to write a server for a soon-to-be-released mobile game (www.zombyapp.com), and had trouble figuring out how to go about connecting to a detached Erlang shell (not surprisingly, this is an app that was started from the command line using the -detached flag). The code swapping is actually the easy part. Just issue the following command at the shell, and the new code is compiled and the next time any functions from that module are called, the new code will be executed:

c("/path/to/module.erl").

The tricky part is getting connected to that detached shell. I first encountered detached shells while tinkering with Chicago Boss (which I highly recommend even though it's a bit experimental), but I suppose I should first show you how to start a detached shell though, in case you are wondering. Of course, you would probably want to start an app or something from the command line as well (a detached shell is otherwise rather useless), but here is a barebones command:

erl -sname servernode -setcookie myR4nd0m_c00k!3 -detached

Great, so we've got this detached node. Now how do we connect to it? In the following command, you can put pretty much whatever you want for the sname argument as long as it is unique. The remsh command is the interesting part; the first chunk of the remote shell info contains the registered name of the node (this is why we use the sname argument), and the hostname is somewhat dependent on your setup. Your box may be configured to accept Erlang connections via a fully-qualified hostname. However, the default behavior seems to be looking for just the short hostname, as given by hostname -s.

erl -sname node1 -remsh servernode@hostname -setcookie myR4nd0m_c00k!3