A note on making the Perl PubSub Client library long-lived.

"Adam Rifkin" <[email protected]> Mon, 12 Jan 2004 18:28:12 -0800
Newsgroups gmane.comp.web.mod-pubsub.devel
Message-ID <[email protected]>
Offline from this list someone noted that the Perl PubSub Client
library regularly kills its calling process whenever errors occur,
using a call to the Perl "die" function.  (There are 57 calls to
"die" in the library, for those keeping track at home. :)

Sometimes we want an application using this library (such as a daemon)
to run for months on end.  I asked Kragen how to do this and below is
his response.  Kragen, is it ok if we put this on the Wiki?

   Adam



-----Original Message-----
From: Kragen Sitaker
Sent: Monday, January 12, 2004 5:14 PM
To: Adam Rifkin

The Perl "die" function is similar to the "throw" keyword in Java or C++
--- it raises an exception that propagates up the call stack until
somebody handles it explicitly.  If nobody handles it explicitly, Perl
prints an error message and ends the program.  [This person] is
complaining about this default handling, but just as in Java or C++,
it's possible to handle the exception differently.

The Perl construct that corresponds to Java or C++'s "catch" is called
"eval" in Perl; confusingly, it shares its name and provenance with a
similar, but more dangerous, construct that allows your program to compile
and run code at runtime, much like the ClassLoader functionality in Java.
"perldoc -f eval" can give you more details.

If you want your daemon to live for months, you can do that by wrapping
its main loop like this:
sub daemon_main_loop_routine {
  # ...
  eval {
    $pubsub_client_object->handle_events();
  };
  warn "Pubsub error: $@" if $@;
  # ...
}

Perl's usual perspicuous naming convention reigns here; eval saves the
exception for you in a variable helpfully named $@.  

I usually observe an exception in the client library code when the event
router closes the connection, so it may be a good idea to close and reopen
the connection (recreating all subscriptions) when it raises an exception.

Hope this helps.  You may want to forward an edited version of this to
the original sender and/or the mod_pubsub mailing list.

- Kragen


-------------------------------------------------------
This SF.net email is sponsored by: Perforce Software.
Perforce is the Fast Software Configuration Management System offering
advanced branching capabilities and atomic changes on 50+ platforms.
Free Eval! http://www.perforce.com/perforce/loadprog.html