[asterisk-commits] mnicholson: testsuite/asterisk/trunk r782 - /asterisk/trunk/asttest/lib/lua/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Aug 26 15:55:49 CDT 2010


Author: mnicholson
Date: Thu Aug 26 15:55:45 2010
New Revision: 782

URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=782
Log:
Created a new asterisk:stop() function that ends asterisk gracefully or forcefully if it does not respond to a graceful shutdown.  Also added some extra fancy code to make sure that this new graceful shutdown function gets called when ever an asterisk process needs to be stopped even if the user forgets to call it.

Modified:
    asterisk/trunk/asttest/lib/lua/astlib.c
    asterisk/trunk/asttest/lib/lua/astlib.lua

Modified: asterisk/trunk/asttest/lib/lua/astlib.c
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/asttest/lib/lua/astlib.c?view=diff&rev=782&r1=781&r2=782
==============================================================================
--- asterisk/trunk/asttest/lib/lua/astlib.c (original)
+++ asterisk/trunk/asttest/lib/lua/astlib.c Thu Aug 26 15:55:45 2010
@@ -470,9 +470,49 @@
 	return 1;
 }
 
+static int set_gc_func(lua_State *L) {
+	lua_pushvalue(L, 1);
+	lua_setfield(L, LUA_REGISTRYINDEX, "astlib_asterisk_gc");
+	return 0;
+}
+
+static int setup_gc(lua_State *L) {
+	/* since we know this function is only called by internal methods, we
+	 * don't do any error checking
+	 *
+	 * We expect two arguments, an asterisk table and a proc table */
+
+	/* create a special userdata so that we can have a __gc method called
+	 * on our proc table */
+	lua_pushvalue(L, 2);
+	lua_replace(L, LUA_ENVIRONINDEX);
+
+	lua_pushliteral(L, "__gc");
+
+	lua_newuserdata(L, sizeof(char));
+
+	/* create the metatable for our special userdata */
+	lua_createtable(L, 0, 1);
+
+	/* call our __gc closure generator and store the result */
+	lua_getfield(L, LUA_REGISTRYINDEX, "astlib_asterisk_gc");
+	lua_pushvalue(L, 1);
+	lua_pushvalue(L, 2);
+	lua_call(L, 2, 1);
+	lua_setfield(L, -2, "__gc");
+
+	lua_setmetatable(L, -2);
+
+	/* store the new userdata in the proc table */
+	lua_settable(L, 2);
+	return 0;
+}
+
 static luaL_Reg astlib[] = {
 	{"unlink", unlink_file},
 	{"_version", get_asterisk_version},
+	{"_set_asterisk_gc_generator", set_gc_func},
+	{"_setup_gc", setup_gc},
 	{NULL, NULL},
 };
 

Modified: asterisk/trunk/asttest/lib/lua/astlib.lua
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/asttest/lib/lua/astlib.lua?view=diff&rev=782&r1=781&r2=782
==============================================================================
--- asterisk/trunk/asttest/lib/lua/astlib.lua (original)
+++ asterisk/trunk/asttest/lib/lua/astlib.lua Thu Aug 26 15:55:45 2010
@@ -60,6 +60,15 @@
 
 	return v1.branch == v2.branch
 end
+
+function asterisk_gc(a, p)
+	return function()
+		_stop(a, p)
+	end
+end
+
+_set_asterisk_gc_generator(asterisk_gc)
+_set_asterisk_gc_generator = nil
 
 -- asterisk table is created in astlib.c
 function asterisk:new()
@@ -84,6 +93,7 @@
 		"-f", "-g", "-q", "-m",
 		"-C", self.asterisk_conf
 	)
+	_setup_gc(self, p)
 	rawset(self, "proc", p)
 end
 
@@ -173,20 +183,40 @@
 	return self.proc:wait(timeout)
 end
 
-function asterisk:term(timeout)
+function _stop(a, p)
+	local res, err
+
+	-- 1.6+ stop commands
+	local stop_gracefully = "core stop gracefully"
+	local stop_now = "core stop now"
+
+	-- if this is 1.4 or less use 1.4 stop commands
+	if version() < version("1.6") then
+		stop_gracefully = "stop gracefully"
+		stop_now = "stop now"
+	end
+
+	a:cli(stop_gracefully)
+	res, err = p:wait(5000)
+	if res or (not res and err ~= "timeout") then
+		return res, err
+	end
+
+	a:cli(stop_now)
+	res, err = p:wait(5000)
+	if res or (not res and err ~= "timeout") then
+		return res, err
+	end
+
+	return p:term_or_kill()
+end
+
+function asterisk:stop()
 	if not self.proc then return nil, "error" end
-	return self.proc:term(timeout)
-end
-
-function asterisk:kill()
-	if not self.proc then return nil, "error" end
-	return self.proc:kill()
-end
-
-function asterisk:term_or_kill()
-	if not self.proc then return nil, "error" end
-	return self.proc:term_or_kill()
-end
+	return _stop(self, self.proc)
+end
+
+asterisk.term_or_kill = asterisk.stop
 
 function asterisk:__newindex(conffile_name, conffile)
 	if (getmetatable(conffile) ~= config) then




More information about the asterisk-commits mailing list