core/script.js
function (fullpath, publname) {

    var curr_time = new Date();

    // store script if modified for first time
    if (fullpath !== undefined && publname !== undefined) {

        if (l_modified_scripts.hasOwnProperty(fullpath) === false) {
                            
            LOG.warn('script modified: ' + fullpath, l_name);
            l_modified_scripts[fullpath] = {
                time: new Date(curr_time.getTime() + l_reloadTime * 1000),
                name: publname
            };
        }
        // if already stored, ignore this request
        else
            return;
    }
    else {

        // check queue for scripts that can be safely reloaded
        for (var path in l_modified_scripts) {
        
            // check if wait time has expired
            if (curr_time - l_modified_scripts[path].time > 0) {

                // get public name
                var name = l_modified_scripts[path].name;
                var notify_msg = 'reloading [' + name + '] from: ' + path;
				LOG.warn(notify_msg, l_name);
				
				// send e-mail notify to project admin (only if specified)
				if (SR.Settings.NOTIFY_SCRIPT_RELOAD === true)
					UTIL.notifyAdmin('script reloading', notify_msg);

				// save current script in cache as backup
				var backup_script = require.cache[path];

                // NOTE: if 'path' is incorrect, may not delete successfully, and new script won't load
                delete require.cache[path];
				
				// NOTE: this will show false
				//LOG.warn('after delete, has path: ' + require.cache.hasOwnProperty(path), l_name);
				
                // NOTE: something can go wrong if the script is corrupt
                try {
                    // re-require
					if (l_args.hasOwnProperty(path)) {
						LOG.warn('args exist..', l_name);
						require(path)(l_args[path]);
						l_loaded[name] = require.cache[path];
					}
					else {
                    	l_loaded[name] = require(path);
						SR.Handler.add(l_loaded[name]);						
					}						
				}
                catch (e) {
					LOG.error('reload error: ', l_name);
					LOG.error(UTIL.dumpError(e), l_name);
					
					LOG.warn('restoring old script...', l_name);
					require.cache[path] = backup_script;
					l_loaded[name] = require.cache[path];
					
					// this will show 'true'
					//LOG.warn('after restore, has path: ' + require.cache.hasOwnProperty(path), l_name);         
                }

                // remove file record
                delete l_modified_scripts[path];
            }
        }
    }

    // reload myself to check later if there are scripts to be loaded
    if (Object.keys(l_modified_scripts).length > 0) {
        var timeout = l_reloadTime * 1.5 * 1000;
        LOG.sys('automatic reloading after: ' + timeout + ' ms', l_name);
        setTimeout(l_loadScript, timeout);
    }        
}
function (fullpath, publname) {

    var curr_time = new Date();

    // store script if modified for first time
    if (fullpath !== undefined && publname !== undefined) {

        if (l_modified_scripts.hasOwnProperty(fullpath) === false) {
                            
            LOG.warn('script modified: ' + fullpath, l_name);
            l_modified_scripts[fullpath] = {
                time: new Date(curr_time.getTime() + l_reloadTime * 1000),
                name: publname
            };
        }
        // if already stored, ignore this request
        else
            return;
    }
    else {

        // check queue for scripts that can be safely reloaded
        for (var path in l_modified_scripts) {
        
            // check if wait time has expired
            if (curr_time - l_modified_scripts[path].time > 0) {

                // get public name
                var name = l_modified_scripts[path].name;
                var notify_msg = 'reloading [' + name + '] from: ' + path;
				LOG.warn(notify_msg, l_name);
				
				// send e-mail notify to project admin (only if specified)
				if (SR.Settings.NOTIFY_SCRIPT_RELOAD === true)
					UTIL.notifyAdmin('script reloading', notify_msg);

				// save current script in cache as backup
				var backup_script = require.cache[path];

                // NOTE: if 'path' is incorrect, may not delete successfully, and new script won't load
                delete require.cache[path];
				
				// NOTE: this will show false
				//LOG.warn('after delete, has path: ' + require.cache.hasOwnProperty(path), l_name);
				
                // NOTE: something can go wrong if the script is corrupt
                try {
                    // re-require
					if (l_args.hasOwnProperty(path)) {
						LOG.warn('args exist..', l_name);
						require(path)(l_args[path]);
						l_loaded[name] = require.cache[path];
					}
					else {
                    	l_loaded[name] = require(path);
						SR.Handler.add(l_loaded[name]);						
					}						
				}
                catch (e) {
					LOG.error('reload error: ', l_name);
					LOG.error(UTIL.dumpError(e), l_name);
					
					LOG.warn('restoring old script...', l_name);
					require.cache[path] = backup_script;
					l_loaded[name] = require.cache[path];
					
					// this will show 'true'
					//LOG.warn('after restore, has path: ' + require.cache.hasOwnProperty(path), l_name);         
                }

                // remove file record
                delete l_modified_scripts[path];
            }
        }
    }

    // reload myself to check later if there are scripts to be loaded
    if (Object.keys(l_modified_scripts).length > 0) {
        var timeout = l_reloadTime * 1.5 * 1000;
        LOG.sys('automatic reloading after: ' + timeout + ' ms', l_name);
        setTimeout(l_loadScript, timeout);
    }        
}
function ( fullpath , publname ) { var curr_time = new Date ( ) ; // store script if modified for first time if ( fullpath !== undefined && publname !== undefined ) { if ( l_modified_scripts . hasOwnProperty ( fullpath ) === false ) { LOG . warn ( 'script modified: ' + fullpath , l_name ) ; l_modified_scripts [ fullpath ] = { time : new Date ( curr_time . getTime ( ) + l_reloadTime * 1000 ) , name : publname } ; } // if already stored, ignore this request else return ; } else { // check queue for scripts that can be safely reloaded for ( var path in l_modified_scripts ) { // check if wait time has expired if ( curr_time - l_modified_scripts [ path ] . time > 0 ) { // get public name var name = l_modified_scripts [ path ] . name ; var notify_msg = 'reloading [' + name + '] from: ' + path ; LOG . warn ( notify_msg , l_name ) ; // send e-mail notify to project admin (only if specified) if ( SR . Settings . NOTIFY_SCRIPT_RELOAD === true ) UTIL . notifyAdmin ( 'script reloading' , notify_msg ) ; // save current script in cache as backup var backup_script = require . cache [ path ] ; // NOTE: if 'path' is incorrect, may not delete successfully, and new script won't load delete require . cache [ path ] ; // NOTE: this will show false //LOG.warn('after delete, has path: ' + require.cache.hasOwnProperty(path), l_name); // NOTE: something can go wrong if the script is corrupt try { // re-require if ( l_args . hasOwnProperty ( path ) ) { LOG . warn ( 'args exist..' , l_name ) ; require ( path ) ( l_args [ path ] ) ; l_loaded [ name ] = require . cache [ path ] ; } else { l_loaded [ name ] = require ( path ) ; SR . Handler . add ( l_loaded [ name ] ) ; } } catch ( e ) { LOG . error ( 'reload error: ' , l_name ) ; LOG . error ( UTIL . dumpError ( e ) , l_name ) ; LOG . warn ( 'restoring old script...' , l_name ) ; require . cache [ path ] = backup_script ; l_loaded [ name ] = require . cache [ path ] ; // this will show 'true' //LOG.warn('after restore, has path: ' + require.cache.hasOwnProperty(path), l_name);          } // remove file record delete l_modified_scripts [ path ] ; } } } // reload myself to check later if there are scripts to be loaded if ( Object . keys ( l_modified_scripts ) . length > 0 ) { var timeout = l_reloadTime * 1.5 * 1000 ; LOG . sys ( 'automatic reloading after: ' + timeout + ' ms' , l_name ) ; setTimeout ( l_loadScript , timeout ) ; } }
script loader, will check periodically if modified script queue is non-empty
script loader will check periodically if modified script queue is non - empty