CodeIgniter Forums
[SOLVED] REST API request does not reply until shell script is done - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: External Resources (https://forum.codeigniter.com/forumdisplay.php?fid=7)
+--- Forum: Addins (https://forum.codeigniter.com/forumdisplay.php?fid=13)
+--- Thread: [SOLVED] REST API request does not reply until shell script is done (/showthread.php?tid=68439)



[SOLVED] REST API request does not reply until shell script is done - Datenshi - 07-11-2017

Hi All,

we are using REST API module by Chris Kacerguis. This is the code:


PHP Code:
public function getAllInformation_put()
 
   {
 
       ...

 
           if (!empty($values['Network'])){
 
               $partial_response $partial_response.self::setNetworkParams($values['Network']);
 
           }

 
           // Generate final REST response
 
           $response '{
                              "status": 200,
                              "message": "PUT '
.$partial_response.'successful"
                        }'
;
 
           $response_array json_decode($responsetrue);
 
           $this->set_response($response_arrayREST_Controller::HTTP_OK); // CREATED (201) being the HTTP response code - Changed to 200
 
           
            if 
(!empty($values['Configuration'])){
 
               shell_exec("/bin/reboot.sh &");
 
           }
 
       }
 
   

And the problem we find is that the response is not immediately sent and it waits for the reboot script to be executed (even we try to run it in the background and it has a sleep of 3 secs)

So sometimes we get network error by response, although the logic is well executed. Because probably the reboot timing and the set_response is too tight. 

Any idea about this issue or how we can try to fix it?

Thanks a lot in advance.


RE: REST API request does not reply until shell script is done - ivantcholakov - 07-11-2017

The client library that makes the request probably has an option for increasing the timeout.


RE: REST API request does not reply until shell script is done - Datenshi - 07-11-2017

I found the solution to this issue. Related to the way we execute the shell script. If I redirect both stdout and stderr to /dev/null, then background it, everything works fine.

Solution found here: LINK

Thanks @ivantcholakov for your suggestion.