Difference between revisions of "Watchdog"

From Second Life Wiki
Jump to navigation Jump to search
Line 6: Line 6:


First and foremost, a watchdog needs to be very robust. Small and simple is better. Performance is not a high priority and a watchdog does not need to run at a high rate of speed. For these reasons, these code examples have been written with timed self-resets, to keep the heap space clean. No fancy tricks that could lead to potential problems (more parts to break) have been included in these examples.
First and foremost, a watchdog needs to be very robust. Small and simple is better. Performance is not a high priority and a watchdog does not need to run at a high rate of speed. For these reasons, these code examples have been written with timed self-resets, to keep the heap space clean. No fancy tricks that could lead to potential problems (more parts to break) have been included in these examples.
'''Use'''
Simply place the desired watchdog script into the prim containing the script(s) to monitor.
'''Optional'''
Indeed, for extra assurance, you may wish to place BOTH watchdog scripts into the prim, and have the basic single-script watchdog watch the multiple script watchdog. However, it is very unlikely that you will in practice need to do so. If either watchdog crashes, you have bigger problems. :)


'''BASIC LSL WATCHDOG FOR A SINGLE PRIMARY SCRIPT'''
'''BASIC LSL WATCHDOG FOR A SINGLE PRIMARY SCRIPT'''

Revision as of 03:59, 17 January 2012

LSL WATCHDOG

These are some simple scripts that watch other scripts in the same prim and restarts them if they crash.

Design Principle

First and foremost, a watchdog needs to be very robust. Small and simple is better. Performance is not a high priority and a watchdog does not need to run at a high rate of speed. For these reasons, these code examples have been written with timed self-resets, to keep the heap space clean. No fancy tricks that could lead to potential problems (more parts to break) have been included in these examples.

Use

Simply place the desired watchdog script into the prim containing the script(s) to monitor.

Optional

Indeed, for extra assurance, you may wish to place BOTH watchdog scripts into the prim, and have the basic single-script watchdog watch the multiple script watchdog. However, it is very unlikely that you will in practice need to do so. If either watchdog crashes, you have bigger problems. :)

BASIC LSL WATCHDOG FOR A SINGLE PRIMARY SCRIPT <lsl> // Basic LSL Watchdog Script to watch a single script // 2012-01-17 By Tika Oberueng // Released to the public domain. // You break it, you get to keep both pieces. // string watchee = "Test"; // Set this to the name of a script in prim inventory to watch float interval = 60.0; // Number of seconds between watchdog checks. Probably best to keep this above >= 10

default {

   state_entry() {
       llSetTimerEvent(interval);
   }
   on_rez(integer param) {
       llResetScript();
   }
   timer() {
       llSetTimerEvent(0);
       if (!llGetScriptState(watchee)) {
           llResetOtherScript(watchee);
           llSetScriptState(watchee, TRUE);
           llRegionSay(DEBUG_CHANNEL, "Watchdog Trip: The script ["+watchee+"] has crashed and has been restarted.");
       }
       llSleep(1);
       llResetScript(); // Reset this script to keep it clean
   }

} </lsl>

BASIC LSL WATCHDOG FOR MULTIPLE SCRIPTS <lsl> // Basic LSL Watchdog Script to watch multiple scripts // 2012-01-17 By Tika Oberueng // Released to the public domain. // You break it, you get to keep both pieces. // float interval = 60.0; // Number of seconds between watchdog checks. Probably best to keep this above >= 10

default {

   state_entry() {
       llSetTimerEvent(interval);
   }
   on_rez(integer param) {
       llResetScript();
   }
   timer() {
       integer loop;
       string watchee;
       llSetTimerEvent(0);
       for (loop = llGetInventoryNumber(INVENTORY_SCRIPT) -1; loop > -1; --loop) {
           watchee = llGetInventoryName(INVENTORY_SCRIPT,loop);
           if (!llGetScriptState(watchee)) {
               if (watchee != llGetScriptName()) {
                   llResetOtherScript(watchee);
                   llSetScriptState(watchee, TRUE);
                   llRegionSay(DEBUG_CHANNEL, "Multiple Watchdog Trip: The script ["+watchee+"] has crashed and has been restarted.");
               }
           }
       }
       llSleep(1);
       llResetScript(); // Reset this script to keep it clean
   }

} </lsl>