Revision
15224
Author
trixter
Date
2009-10-24 12:07:44 -0500 (Sat, 24 Oct 2009)

Log Message

added "status" checking and not processing events if we are at the max sessions or max session rate.
The load detection is primitive, it only looks at sessions and not what the action is doing (which may not involve a session)
Additionally it does not look at the actual system load, so  if you have misconfigured your max sessions you may overdrive your box and cause problems

Modified Paths

Diff

Modified: freeswitch/trunk/contrib/trixter/scheduled_event.lua (15223 => 15224)


--- freeswitch/trunk/contrib/trixter/scheduled_event.lua	2009-10-24 01:48:45 UTC (rev 15223)
+++ freeswitch/trunk/contrib/trixter/scheduled_event.lua	2009-10-24 17:07:44 UTC (rev 15224)
@@ -166,29 +166,34 @@
 	 event_subclass = e:getHeader("Event-Subclass") or ""
 	 
 	 if(event_name == "HEARTBEAT") then
-	    env = assert (luasql.mysql())
-	    dbcon = assert (env:connect(DATABASE,USERNAME,PASSWORD,DBHOST))
-	    while true do
-	       assert (dbcon:execute("LOCK TABLE "..TABLENAME.." WRITE"))
-	       cur = assert (dbcon:execute("select * from "..TABLENAME.." where timestamp < NOW() and (server = '"..hostname.."' or server = '*') order by timestamp limit 1"))
-	       row = cur:fetch({},"a")
-	       if not row then
-		  break
-	       end
-	       assert(dbcon:execute("delete from "..TABLENAME.." where acctid = "..row.acctid));
-	       assert (dbcon:execute("UNLOCK TABLES"))
-	       apicmd,apiarg = string.match(row.action,"(%w+) (.*)")
-	       api:execute(apicmd,apiarg)
-	    end
-	    dbcon:close()
-	    env:close()
+	    -- check the system load
+	    load = api:execute("status")
+	    cur_sessions,rate_sessions,max_rate,max_sessions = string.match(load,"(%d+) session.s. (%d+)/(%d+)\n(%d+) session.s. max")
+	    if ((tonumber(cur_sessions) < tonumber(max_sessions)) and (tonumber(rate_sessions) < tonumber(max_rate))) then
+	       env = assert (luasql.mysql())
+	       dbcon = assert (env:connect(DATABASE,USERNAME,PASSWORD,DBHOST))
+	       while true do
+		  assert (dbcon:execute("LOCK TABLE "..TABLENAME.." WRITE"))
+		  cur = assert (dbcon:execute("select * from "..TABLENAME.." where timestamp < NOW() and (server = '"..hostname.."' or server = '*') order by timestamp limit 1"))
+		  row = cur:fetch({},"a")
+		  if not row then
+		     break
+		  end
+		  assert(dbcon:execute("delete from "..TABLENAME.." where acctid = "..row.acctid));
+		  assert (dbcon:execute("UNLOCK TABLES"))
+		  apicmd,apiarg = string.match(row.action,"(%w+) (.*)")
+		  api:execute(apicmd,apiarg)
+	       end -- while
+	       dbcon:close()
+	       env:close()
+	    end -- rate limiting
 	 else if (event_name == "CUSTOM" and event_subclass == "lua::scheduled_event") then
 	       action = e:getHeader("Action") or ""
 	       if (action == "stop") then
 		  logger("got stop message, Exiting")
 		  break
 	       end
-	    end
-	 end
-      end
-   end
\ No newline at end of file
+	    end -- not a custom message
+	 end -- not a processable event
+      end -- foreach event
+   end -- main loop, DB connection established
\ No newline at end of file