General developer forum

How to catch scheduled task error before it fails

 
Picture of oliver stearn
How to catch scheduled task error before it fails
 

I have spent all day on this, so hoping one of you pros can give me a suggestion here. 

I have a scheduled task which makes a call to the google drive API. For whatever reason the google drive API throws an error from time to time. If I rerun the same API call it normally works the second time around due to the request being cached on the google end). 

I would like to catch the error, or ignore it and do a test afterwards on the returned object, but despite trying to catch errors, set custom exception handlers etc the scheduled task always insists on failing. 

Here is the code that occasionally returns a json file with error code:

$getScorm = $service->files->get($scorm->id, array('alt' => 'media'));


Scheduled task failed: BLC Module Synchronisation (block_scorm_package\task\cron_task),{ "error": { "code": 500, "message": null } }

Any suggestions on how I can stop the whole task from failing?


Many thanks,

Oliver

 
Average of ratings: -
Picture of Michael Aherne
Re: How to catch scheduled task error before it fails
Core developersPlugin developers

What have you done to catch the error? You're probably on the right track there, as the task is likely failing due to an uncaught exception. I'd guess you're trying to catch a different exception from the one that's being thrown.

If you're using PHP 7, something like this should show you the actual exception object in the task output:

try {
$getScorm = $service->files->get($scorm->id, array('alt' => 'media'));
} catch (\Throwable $e) {
print_r($e);
}


 
Average of ratings: -
Picture of oliver stearn
Re: How to catch scheduled task error before it fails
 
Thanks a lot for the reply. 

I was pretty much there with your suggestion, although I had not tried using \Throwable. 

Even with Throwable in place, I get the same result more or less. The error returned from the google drive API comes after the 'scheduled task failed' message, which is a bit bizarre;

Scheduled task failed: BLC Module Synchronisation (block_scorm_package\task\cron_task),{ "error": {
"code": 500,
"message": null
}
}

The actual code in question is this (the stop business is just debugging to see how far it gets - stop 1 is the last thing printed):

try {
print "stop 1";
$getScorm = $service->files->get($scorm->id, array('alt' => 'media'));
print "stop 2";
$content = $getScorm->getBody()->getContents();
print "stop 3";
} catch (\Throwable $e) {
print "An error occurred: " . $e->getMessage();
 
Average of ratings: -
Picture of Michael Aherne
Re: How to catch scheduled task error before it fails
Core developersPlugin developers

That's pretty odd! I think the error message that's displayed comes from the getMessage() call on the exception that caused the task to fail, so that's kind of expected, but it's strange that your try / catch isn't picking that exception up.

Have you tried running the code outside of a scheduled task?

 
Average of ratings: -
Picture of oliver stearn
Re: How to catch scheduled task error before it fails
 

outside the scheduled task it works fine, well the error is thrown, next time you run the script it picks up where it left off and succeeds. 

What im trying to do now is exec() the non scheduled task version from within the ST - clutching at straws really, and it's not going well. 


My only issue is that once the scheduled task fails a couple of times within an hour, the next run of that scheduled task gets pushed back by up to an hour. If there were a way to disable that functionality temporarily, that would just about do the trick. 

 
Average of ratings: -
Picture of oliver stearn
Re: How to catch scheduled task error before it fails
 

Thanks for all your support with this. 

As always it was something ludicrously simple, and as always I made it much worse before I made it any better. 

I actually started this message thinking that display_errors(false); in combination with the \throwable catch did the job... now I'm not so sure if display_errors has any impact on it. 

The working function (with errors being catched):



          try {

          print "stop 1";  

          $getScorm = $service->files->get($scorm->id, array('alt' => 'media'));

          print "stop 2"; 

          $content = $getScorm->getBody()->getContents();

          print "stop 3"; 

          $handle = fopen($docroot.'scormfiles/'.$scorm->name,"w");

          fwrite($handle, $content);

          fclose($handle);          

          } catch (\Throwable $e) {

             print "The Google Drive API threw an error, but dont worry, we'll come back for this.";

          }


        }

 
Average of ratings: -
Picture of Michael Aherne
Re: How to catch scheduled task error before it fails
Core developersPlugin developers

Glad you got it working!

 
Average of ratings: -