Updated 2012-08-19 05:17:39 by RLE

I run tclhttpd on a machine that has probably way over 99% 24/7 uptime ( ), and I like the responsiveness and relative ease of use of the server, so I stuck with it after I investigated its use 5 years ago, even though I still haven't figured out how its page counting exactly takes place.

Because at times I want to keep the page (hit) statistics in place over a restart, for instance over periodic restarts to make sure the (home) Microsoft Windows XP machine doesn't get all too sluggish or whatever it does after 3 weeks of running such a time, I made a routine to save the hit statistic arrays, and a one-liner to read them back.

That's not the whole state, so strictly speaking the title is not exact, but there is not much point saving socket states of sockets who'll be lost anyhow after a restart. Strictly speaking, one could use file descriptor passing to an external server process to keep the sockets alive (I did that on a 'connection server' in university 10 years ago) and restart only the web server, but that is not possible in tcl, and doesn't work over a machine restart.

The save procedure saves a file sstateXXX.tcl in the current directory which in tcl form contains the contents of the variables of the counter::namespace:
 # reboot and save page counter state procedures
 proc save_state {} {
   global counter
   set o {}
   foreach i [info var counter::*] {
      if {[array get $i] != ""} {
         append o "array set $i {[array get $i]}\n"
   set f [open sstate[format "%03d" [expr [scan [string range [lindex [lsort [glob sstate???.tcl]] end] 6 8] "%03d"] + 1]].tcl w]
   puts $f $o
   close $f

To read this data back after a restart, use:
 # read last state back
 source sstate[string range [lindex [lsort [glob sstate???.tcl]] end] 6 8].tcl

Generates no problems, thus far.