action-scheduler icon indicating copy to clipboard operation
action-scheduler copied to clipboard

How to debug properly?

Open nicmare opened this issue 2 years ago • 4 comments
trafficstars

Hi guys, is there a guide how to debug code within a action scheduler function properly? I tried WPs build in error_log function but for some reason it does not trigger. instead i see a vague error code description in AS "Log" column like "devision by zero" but without a hint of a line of code nor a file. So each time i have to guess whats wrong. Very exhausting. image

best Nic

nicmare avatar Jun 08 '23 12:06 nicmare

Hi @nicmare,

I think we can improve on this and so will mark as an enhancement request.

Until then, and within the same table as your screenshot, you should be able to see both the hook and any arguments passed to the hook. With that in mind, it would seem like it should generally be possible for you to try and replicate with code looking something like this:

do_action_ref_array( $hook, array_values( $args ) );

Or, suppose for a moment there are no arguments, then via WP CLI you could simply do:

wp eval "do_action( 'the_hook' );"

This will hopefully allow you to see more complete error information. Of course, if the circumstances leading to a division-by-zero error are dynamic in nature or if the callback function is non-deterministic, then this may not get you very far: but you should still be able to start the process of debugging your callback function.

barryhughes avatar Jun 08 '23 20:06 barryhughes

Hi @barryhughes @nicmare This can be fixed by changing line 94 in 'classes/abstracts/ActionScheduler_Abstract_QueueRunner.php' to throw new Exception( $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine(), $e->getCode(), $e->getPrevious() );

webdevs-pro avatar Jun 25 '23 09:06 webdevs-pro

Thanks! Though, worth noting that (as we currently support as far back as PHP 5.6) we'd ideally need an additional change within the custom error handler.

barryhughes avatar Jun 26 '23 15:06 barryhughes

Hi @barryhughes @nicmare This can be fixed by changing line 94 in 'classes/abstracts/ActionScheduler_Abstract_QueueRunner.php' to throw new Exception( $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine(), $e->getCode(), $e->getPrevious() );

this helps me A LOT. so much easier to identify bugs. thank you

nicmare avatar Oct 09 '23 08:10 nicmare