Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger function just before exit

I'm using DHTMLX Scheduler on the front end and DHTMLX Connector on the backend as part of my radio automation app. Every time a user edits the calendar, an AJAX call is made to a file that looks like this:

require_once("dhtmlxScheduler_v4/connector/scheduler_connector.php");
require_once('QDRAconf.php');

$res = mysql_connect($QDRAconf['mysqlHost'], $QDRAconf['mysqlUser'], $QDRAconf['mysqlPass']);
mysql_select_db($QDRAconf['mysqlDb']);

// init the schedulerconnector
$conn = new SchedulerConnector($res);

// render the table
$conn->render_table("events","id","start_date,end_date,text");

This file is my "shim" that hooks up the fronted to the back end. I want to run another PHP script that writes the changes to my crontab, but it needs to happen after the DHTMLX library has updated the database. Trouble is, the DHTMLX library will automatically exit whenever it thinks it's done: sometimes it might not get past the first require_once('...') line so I can't just put require_once('cronwriter.php'); at the last line of the script.

My solution to this was to create a class with a destructor that updates the crontab with the latest changes. Since the php manual states that destructors will still be run if the exit() or die() function is called, I added a dummy class with a destructor that runs cronwriter.php script: (I added this to the beginning of the file.)

class ExitCatcher
{
    function __destruct()
    {
        require_once('cronwriter.php');
    }
}
//init the class
$ExitCatcher = new ExitCatcher;

For some reason, it doesn't work.

like image 496
BonsaiOak Avatar asked Sep 12 '14 15:09

BonsaiOak


1 Answers

register_shutdown_function may offer a quick solution; but, you might save yourself some future trouble by inspecting the cause of that library's sporadic process haltings.

A good place to start might be...

  • your browser's JS console for JS errors
  • your JS console's network tab for AJAX errors
  • your server's error logs for PHP errors
like image 65
chaseisabelle Avatar answered Oct 09 '22 10:10

chaseisabelle