/// <summary> /// Executes the scripted event. /// </summary> /// <param name="eventType">Type of the event.</param> /// <param name="taskId">The task id.</param> /// <param name="name">The name.</param> /// <param name="sourceCode">The source code.</param> /// <param name="language">The language.</param> /// <param name="sender">The sender.</param> /// <param name="scriptArguments">The <see cref="System.EventArgs"/> instance containing the event data.</param> /// <param name="preview">if set to <c>true</c> [preview].</param> /// <returns></returns> public static Dictionary<string, object> ExecuteScriptedEvent(string eventType, Guid taskId, string name, string sourceCode, string language, object sender, EventArgs scriptArguments, bool preview) { ("EVENT " + eventType + " > delegate " + name + "(" + taskId.ToString() + ")").Debug(10); Dictionary<string, object> j = new Dictionary<string, object>(); string consoleOutput = ""; string errorNumber = ""; string errorJSON = ""; DateTime startDate = DateTime.Now; List<object> errors = new List<object>(); object[] args = { sender, scriptArguments }; object obj = Admin.ExecuteScript(sourceCode, language, "script", "main", ref args, ref errors); if(errors.Count == 0) { if(consoleOutput.Length > 0) { if(obj.GetType() == typeof(string)) { consoleOutput = (string)obj; } Dictionary<string, object> err = new Dictionary<string, object>(); err.Add("errorNumber", 0); err.Add("errorText", "EVENT " + eventType + " > delegate " + name + " completed without error."); (" --------------------------------------------------").Debug(6); (" | MESSAGE FROM " + name).Debug(6); (" --------------------------------------------------").Debug(6); (consoleOutput).Debug(6);/*MESSAGE!*/ (" --------------------------------------------------").Debug(6); err.Add("line", 0); errorNumber = "0"; errorJSON = err.ToJson(); err["errorText"].Debug(6); } } else { errorJSON = errors.ToJson(); errorNumber = (string)((Dictionary<string, object>)(errors[0]))["errorNumber"].ToString(); errorJSON.Debug(6); } if(!preview) { updateEventTaskStatus(taskId, startDate, false, DateTime.Now, errorNumber, errorJSON); } j.Add("error", errorNumber); j.Add("errors", errors); j.Add("console", consoleOutput); return j; }
/// <summary> /// Executes a scheduled task. /// </summary> /// <param name="taskId">The task id.</param> /// <param name="name">The name.</param> /// <param name="interval">The interval.</param> /// <param name="sourceCode">The source code.</param> /// <param name="language">The language.</param> /// <param name="lastRun">The last run.</param> /// <param name="lastErrorId">The last error id.</param> /// <param name="lastErrorJSON">The last error JSON.</param> /// <returns></returns> public Admin.Timer ExecuteScheduledTask( Guid taskId, string name, int interval, string sourceCode, string language, DateTime lastRun, string lastErrorId, string lastErrorJSON ) { ( "FUNCTION executeScheduledTask (init and start task event timers) /W ADHOC (!PRIVATE!)" ).Debug( 10 ); Admin.Timer timer = new Admin.Timer(); /* last time this ran, minus the interval is the starting interval */ timer.Interval = interval; timer.Name = "Compiled DB Timer Event" + Utilities.Iif( name.Length > 0, ":", "" ); timer.elapsed += new EventHandler( delegate( object e, EventArgs args ) { List<object> errors = new List<object>(); DateTime startDate = new DateTime(); startDate = DateTime.Now; DateTime endDate = new DateTime(); endDate = DateTime.MinValue; string errorJSON = ""; string errorNumber = "0"; string consoleOut = ""; try { /* and and run someone elses code */ ( "EVENT DELEGATE Task " + name + " started." ).Debug( 6 ); if( timer.Interval != interval ) { timer.Interval = interval;/* now interval should be set to the actual interval */ } using( SqlConnection cn = Site.CreateConnection( true, true ) ) { cn.Open(); using( SqlTransaction trns = cn.BeginTransaction( "Scheduled Task" ) ) { using( SqlCommand cmd = new SqlCommand( "update eventHandlers set startTime = @startTime, lock = 1 where taskId = @taskId", cn ) ) { cmd.Parameters.Add( "@taskId", SqlDbType.UniqueIdentifier ).Value = new Guid( taskId.ToString() ); cmd.Parameters.Add( "@startTime", SqlDbType.DateTime ).Value = startDate; cmd.ExecuteNonQuery(); TimerEventArgs evntArgs = new TimerEventArgs( cn, trns, lastRun, taskId, name, lastErrorId, lastErrorJSON ); object[] scriptArguments = { Main.Site, evntArgs }; object obj = Admin.ExecuteScript( sourceCode, language, "script", "main", ref scriptArguments, ref errors ); if( errors.Count == 0 ) { if( obj.GetType() == typeof( string ) ) { consoleOut = ( string )obj; } Dictionary<string, object> s = new Dictionary<string, object>(); s.Add( "errorNumber", 0 ); s.Add( "errorDesc", "Timer Event " + name + " completed without error." ); s.Add( "console", consoleOut ); errorNumber = "0"; errorJSON = s.ToJson(); trns.Commit();/* no errors occured in the script so commit the transaction */ } else { errorJSON = errors.ToJson(); errorNumber = ( ( Dictionary<string, object> )( errors[ 0 ] ) )[ "errorNumber" ].ToString(); trns.Rollback();/* one or more errors occured so rollback the transaction */ } endDate = DateTime.Now; updateEventTaskStatus( taskId, startDate, false, endDate, errorNumber, errorJSON ); ( "EVENT DELEGATE Task " + name + " ended." ).Debug( 6 ); } } } } catch( Exception excp ) { String.Format( "EVENT DELEGATE Task {0} threw and exception. {1}", name, excp.Message ).Debug( 1 ); } } ); timer.Start(); return timer; }