<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][15204] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=15204">15204</a></dd>
<dt>Author</dt> <dd>trixter</dd>
<dt>Date</dt> <dd>2009-10-23 07:24:48 -0500 (Fri, 23 Oct 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>multi-server (&quot;clustered&quot;) event scheduler
events are stored in MySQL so they survive a shutdown, reboot, crash, alien abduction and more!
events are any api command</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunkcontribtrixterREADME">freeswitch/trunk/contrib/trixter/README</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#freeswitchtrunkcontribtrixterscheduled_eventlua">freeswitch/trunk/contrib/trixter/scheduled_event.lua</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunkcontribtrixterREADME"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/contrib/trixter/README (15203 => 15204)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/trixter/README        2009-10-22 22:35:08 UTC (rev 15203)
+++ freeswitch/trunk/contrib/trixter/README        2009-10-23 12:24:48 UTC (rev 15204)
</span><span class="lines">@@ -23,6 +23,13 @@
</span><span class="cx"> 
</span><span class="cx"> == FILE LIST ==
</span><span class="cx"> 
</span><ins>+scheduled_event.lua
+        LUA script that adds scheduler ability via a MySQL store.
+        Allows for many systems to process events to scale to enterprise capability
+        Events survive switch shutdown, restart, reboot, crash, etc
+        Simple DB schema which allows you to easily integrate into webapps
+
+
</ins><span class="cx"> apiexec.pl
</span><span class="cx">         Execute api commands from the console/cron/windows scheduler/etc
</span><span class="cx"> 
</span></span></pre></div>
<a id="freeswitchtrunkcontribtrixterscheduled_eventlua"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/contrib/trixter/scheduled_event.lua (0 => 15204)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/trixter/scheduled_event.lua                                (rev 0)
+++ freeswitch/trunk/contrib/trixter/scheduled_event.lua        2009-10-23 12:24:48 UTC (rev 15204)
</span><span class="lines">@@ -0,0 +1,189 @@
</span><ins>+-- Version: MPL 1.1
+--
+-- The contents of this file are subject to the Mozilla Public License Version
+-- 1.1 (the &quot;License&quot;); you may not use this file except in compliance with
+-- the License. You may obtain a copy of the License at
+-- http://www.mozilla.org/MPL
+--
+-- Software distributed under the License is distributed on an &quot;AS IS&quot; basis
+-- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+-- for the specific language governing rights and limitations under the
+-- License
+--
+-- The Original Code is FreeSWITCH[tm] LUA event scheduler
+--
+-- The Initial Developer of the Original Code is
+-- Bret McDanel &lt;bret AT 0xdecafbad dot com&gt;
+--
+-- Portions created by the Initial Developer are Copyright (C) 2007
+-- the Initial Developer. All Rights Reserved
+--
+-- Contributor(s)
+--
+-- Why use this?
+--  No reason in particular, however if you use sched_api and shut down you lose your scheduled events, by
+--  placing them in a database you get the advantage of them being able to survive a shutdown/crash/reboot/etc
+--  Additionally this script is written in a way to allow for multiple switch boxes to access the same database
+--  and look  for events to process, in this way you can scale this to a very large amount of actions
+--
+--
+-- Usage: in FreeSWITCH console or fs_cli,
+-- luarun scheduled_event.lua
+--        starts the script - this is not needed if its auto loaded
+-- luarun scheduled_event.lua &lt;dbuser username&gt; &lt;dbhost hostname&gt; &lt;dbpass password&gt; &lt;dbname database_name&gt;
+--        changes the DB info allowing you to do load management, maintenence, or other things
+-- luarun scheduled_event.lua stop
+--        stops the running script
+--   
+--      in lua.conf.xml:
+--          &lt;param name=&quot;startup-script&quot; value=&quot;scheduled_event.lua&quot;/&gt;
+--         
+--
+-- You need to get &quot;mysql.so&quot; for lua and install it in &lt;freeswitch install&gt;/luasql/mysql.so 
+--   (or some other place that is in the search path)
+--
+-- Minimal DB schema **AUTOMAGICALLY CREATED IF IT DOES NOT EXIST** (at script start)
+-- CREATE TABLE scheduler (
+--    acctid int(11) NOT NULL auto_increment,
+--    action varchar(1024) NOT NULL,
+--    timestamp timestamp NOT NULL,
+--    primary key (acctid)
+-- );
+--
+-- &quot;actions&quot; in the scheduler table should be api commands eg:
+-- insert into scheduler (action,timestamp) values (&quot;pa call 1234&quot;,NOW())
+-- 
+--
+-- Every &quot;heartbeat&quot; events are pulled 1 at a time using write locks on mysql
+-- if your MySQL supports those locks, then only one process can access the DB at a time
+-- events are immediately deleted, and then processed to keep the lock time low
+-- failed events are NOT rescheduled
+-- many boxes can pull events and process them, however I did not attempt to process more
+-- events than time allows for in a given heartbeat interval, some backlog may exist which
+-- may cause this script to totally freak
+
+
+   require &quot;luasql.mysql&quot;
+   
+   -- Database setup
+   DATABASE = &quot;database&quot;
+   USERNAME = &quot;dbuser&quot;
+   PASSWORD = &quot;het is geheim&quot;
+   DBHOST   = &quot;localhost&quot;
+   TABLENAME = &quot;scheduler&quot;
+
+   -- LOGGING
+   LOGLEVEL = &quot;info&quot;
+   
+   -- PROGNAME
+   PROGNAME = &quot;scheduled_event.lua&quot;
+   
+   function logger(message)
+      freeswitch.console_log(LOGLEVEL,&quot;[&quot;..PROGNAME..&quot;] &quot;..message..&quot;\n&quot;)
+   end
+   
+   
+   if argv[1] then
+      i=1
+      while argv[i] do
+         if argv[i] == &quot;stop&quot; then
+            local event = freeswitch.Event(&quot;custom&quot;, &quot;lua::scheduled_event&quot;)
+            event:addHeader(&quot;Action&quot;, &quot;stop&quot;)
+            event:fire()
+            logger(&quot;Sent stop message to lua script&quot;)
+            return
+         elseif (argv[i] == &quot;dbuser&quot;) then
+            i=i+1
+            if argv[i] then
+               logger(&quot;Setting DB Username to &quot;..argv[i])
+               USERNAME = argv[i]
+            else
+               logger(&quot;You must specify a username!&quot;)
+            end
+         elseif (argv[i] == &quot;dbhost&quot;) then
+            i=i+1
+            if argv[i] then
+               logger(&quot;Setting DB Hostname to &quot;..argv[i])
+               DBHOST = argv[i]
+            else
+               logger(&quot;You must specify a hostname!&quot;)
+            end
+         elseif (argv[i] == &quot;dbpass&quot;) then
+            i=i+1
+            if argv[i] then
+               logger(&quot;Setting DB Password to &quot;..argv[i])
+               PASSWORD = argv[i]
+            else
+               logger(&quot;You must specify a password!&quot;)
+            end
+         elseif (argv[i] == &quot;dbname&quot;) then
+            i=i+1
+            if argv[i] then
+               logger(&quot;Setting DB to &quot;..argv[i])
+               DATABASE = argv[i]
+            else
+               logger(&quot;You must specify a database name!&quot;)
+            end
+         end
+         i=i+1
+      end
+      return
+   end
+   
+   --Main function starts here
+   logger(&quot;Starting&quot;)
+
+   -- ensure DB works, create table if it doesnt exist
+   env = assert (luasql.mysql())
+   dbcon = assert (env:connect(DATABASE,USERNAME,PASSWORD,DBHOST))
+   blah = assert(dbcon:execute(&quot;CREATE TABLE if not exists &quot;..TABLENAME..&quot; (&quot;..
+                        &quot;acctid int(11) NOT NULL auto_increment,&quot;..
+                        &quot;action varchar(1024) NOT NULL,&quot;..
+                        &quot;timestamp timestamp NOT NULL,&quot;..
+                        &quot;primary key (acctid)&quot;..
+                               &quot;)&quot;))
+   dbcon:close()
+   env:close()
+
+   local event_name
+   local event_subclass
+
+   con = freeswitch.EventConsumer(&quot;all&quot;) -- too lazy to figure out why &quot;heartbeat CUSTOM lua::scheduled_event&quot;
+                                         -- does not work properly, that is really all we need
+   api = freeswitch.API()
+
+
+   if blah ~= 0 then
+      logger(&quot;Unable to connect to DB or create the table, something is broken.&quot;)
+   else
+      for e in (function() return con:pop(1) end) do
+         event_name = e:getHeader(&quot;Event-Name&quot;) or &quot;&quot;
+         event_subclass = e:getHeader(&quot;Event-Subclass&quot;) or &quot;&quot;
+         
+         if(event_name == &quot;HEARTBEAT&quot;) then
+            env = assert (luasql.mysql())
+            dbcon = assert (env:connect(DATABASE,USERNAME,PASSWORD,DBHOST))
+            while true do
+               assert (dbcon:execute(&quot;LOCK TABLE &quot;..TABLENAME..&quot; WRITE&quot;))
+               cur = assert (dbcon:execute(&quot;select * from &quot;..TABLENAME..&quot; where timestamp &lt; NOW() order by timestamp desc limit 1&quot;))
+               row = cur:fetch({},&quot;a&quot;)
+               if not row then
+                  break
+               end
+               assert(dbcon:execute(&quot;delete from &quot;..TABLENAME..&quot; where acctid = &quot;..row.acctid));
+               assert (dbcon:execute(&quot;UNLOCK TABLES&quot;))
+               apicmd,apiarg = string.match(row.action,&quot;(%w+) (.*)&quot;)
+               api:execute(apicmd,apiarg)
+            end
+            dbcon:close()
+            env:close()
+         else if (event_name == &quot;CUSTOM&quot; and event_subclass == &quot;lua::scheduled_event&quot;) then
+               action = e:getHeader(&quot;Action&quot;) or &quot;&quot;
+               if (action == &quot;stop&quot;) then
+                  logger(&quot;got stop message, Exiting&quot;)
+                  break
+               end
+            end
+         end
+      end
+   end
</ins><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>