<!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 ("clustered") 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 "License"); 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 "AS IS" 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 <bret AT 0xdecafbad dot com>
+--
+-- 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 <dbuser username> <dbhost hostname> <dbpass password> <dbname database_name>
+-- 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:
+--          <param name="startup-script" value="scheduled_event.lua"/>
+--        
+--
+-- You need to get "mysql.so" for lua and install it in <freeswitch install>/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)
+-- );
+--
+-- "actions" in the scheduler table should be api commands eg:
+-- insert into scheduler (action,timestamp) values ("pa call 1234",NOW())
+--
+--
+-- Every "heartbeat" 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 "luasql.mysql"
+
+ -- Database setup
+ DATABASE = "database"
+ USERNAME = "dbuser"
+ PASSWORD = "het is geheim"
+ DBHOST = "localhost"
+ TABLENAME = "scheduler"
+
+ -- LOGGING
+ LOGLEVEL = "info"
+
+ -- PROGNAME
+ PROGNAME = "scheduled_event.lua"
+
+ function logger(message)
+ freeswitch.console_log(LOGLEVEL,"["..PROGNAME.."] "..message.."\n")
+ end
+
+
+ if argv[1] then
+ i=1
+ while argv[i] do
+         if argv[i] == "stop" then
+         local event = freeswitch.Event("custom", "lua::scheduled_event")
+         event:addHeader("Action", "stop")
+         event:fire()
+         logger("Sent stop message to lua script")
+         return
+         elseif (argv[i] == "dbuser") then
+         i=i+1
+         if argv[i] then
+         logger("Setting DB Username to "..argv[i])
+         USERNAME = argv[i]
+         else
+         logger("You must specify a username!")
+         end
+         elseif (argv[i] == "dbhost") then
+         i=i+1
+         if argv[i] then
+         logger("Setting DB Hostname to "..argv[i])
+         DBHOST = argv[i]
+         else
+         logger("You must specify a hostname!")
+         end
+         elseif (argv[i] == "dbpass") then
+         i=i+1
+         if argv[i] then
+         logger("Setting DB Password to "..argv[i])
+         PASSWORD = argv[i]
+         else
+         logger("You must specify a password!")
+         end
+         elseif (argv[i] == "dbname") then
+         i=i+1
+         if argv[i] then
+         logger("Setting DB to "..argv[i])
+         DATABASE = argv[i]
+         else
+         logger("You must specify a database name!")
+         end
+         end
+         i=i+1
+ end
+ return
+ end
+
+ --Main function starts here
+ logger("Starting")
+
+ -- 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("CREATE TABLE if not exists "..TABLENAME.." ("..
+                        "acctid int(11) NOT NULL auto_increment,"..
+                        "action varchar(1024) NOT NULL,"..
+                        "timestamp timestamp NOT NULL,"..
+                        "primary key (acctid)"..
+                         ")"))
+ dbcon:close()
+ env:close()
+
+ local event_name
+ local event_subclass
+
+ con = freeswitch.EventConsumer("all") -- too lazy to figure out why "heartbeat CUSTOM lua::scheduled_event"
+ -- does not work properly, that is really all we need
+ api = freeswitch.API()
+
+
+ if blah ~= 0 then
+ logger("Unable to connect to DB or create the table, something is broken.")
+ else
+ for e in (function() return con:pop(1) end) do
+         event_name = e:getHeader("Event-Name") or ""
+         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() order by timestamp desc 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()
+         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
</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>