Using php register_shutdown_function

Permalink
In response to a prb comment from mkly, I have been playing with php register_shutdown_function().

The attached class provides a pair of calls to be placed about a section of code (with microtime measurement as a side effect), with the intention that if the completion call does not happen (ie something between them fails), a message is added to the log. The aim was to try and catch failures that an exception handler would not catch, and be used on a bigger scale than the simple example below.

Loader::library('nice_ending_check','my_package_name');
NiceEndingCheck::register_start('my_package_name');
// Some critical code starts here
......
// Some critical code ends here
$ex_time = NiceEndingCheck::register_complete('my_package_name');


After various issues, I simply placed it about some code in a single page view to get some diagnostics. It runs OK when called correctly, but when I introduce a failure (by calling register_complete with a mismatched token), the final Log::addEvent is failing with an unknown mysql error.

My current conclusion is that something needed to run the sql within Log::addEvent is no longer in existence when called from register_shutdown_function().

If anyone feels like playing and offering some ideas on making this work, I am looking at it as 'an interesting digression' rather than something that I absolutely have to get working.

<?php
defined('C5_EXECUTE') or die("Access Denied.");
/*
Library to put round important bits of code
The idea is that code in question is wrapped in register_start
and register_complete. $token being used to match them together.
The check_complete should then be called automatically to confirm
that register_complete has been done for the $token, and log if
it has not.
BUGS - Log::addEvent fails
*/
if (! class_exists('NiceEndingCheck')){
class NiceEndingCheck {
  public static $client_track = array();
  public static function register_start($token, $failure_message=null){

1 Attachment

JohntheFish