简体   繁体   中英

php file_get_contents executes twice

I have created a script to check in backup tapes into our tape library and check them in with TSM. This script is activated by SMS.

Our SMS server receives the command to start the check-in and then executes a script on the TSM server with a file_get_contents command.

I have an issue that the script is being executed twice when there are allot of tapes to check in(+20). This results errors on out TSM server because the move media commands are allso double.

I overcame this by putting in an inital timestamp logging when the first file_get_content is started so the commands arent executed twice. Allthough this fixes the double command issue it still presents a problem because the SMS server sends back confirmation that the script started or not. So this means that on every check in with +20 tapes, the operator gets 2 messages, 1 saying check in failed, the other check in started.

I suspect this is caused because of the time it takes for the commands to be passed onto the TSM server (can take upto 45seconds).

Long story short, is there a way i can set some sort of longer timeout, or give any parameter/checks to prevent this behaviour? Thanks in advance. Paths are replaced by *****.

SMS server code
    //DRM checkin
            if($auth == 1 AND strtolower($sms_body) == "******"){
                $knowncommand = 1;
                $url = "http://*******/******/checkin.php?remote&exec&sender=" . $from;
                $dodrm = file_get_contents($url);
                if ($stmt2 = $mysqli->prepare("UPDATE messagein SET checked = 1 WHERE checked = 0 ")) {
                    $stmt2->execute();
                    $stmt2->close();
                }
            }

TSM Server script code:

if(isset($_GET['exec'])){
    if(isset($_GET['remote'])){
        $rcs = CheckRemoteCheckinStatus();
        $to = $_GET['sender'];
        //Execute drm check-in
        $commit = CheckButtonStatus();
        if($commit == "" AND $rcs == 0){
            SetRemoteCheckinStatus();
            $psDIR = "*****";
            $psScript = "drm_checkin_retrieve.ps1";
            $runCMD = $psPath. ' -ExecutionPolicy RemoteSigned '.$psDIR.$psScript;
            exec($runCMD, $out);
            SetCheckinStatus();
            $psDIR = "*****";
            $psScript = "QueueSMS.ps1 $to 'Check-in gestart...'";
            $runCMD = $psPath. ' -ExecutionPolicy RemoteSigned '.$psDIR.$psScript;
            exec($runCMD, $out);
        }
        else{
            //Send Failed SMS
            $psDIR = "*****";
            $psScript = "QueueSMS.ps1 $to 'Fout: Geen Check-in mogelijk.'";
            $runCMD = $psPath. ' -ExecutionPolicy RemoteSigned '.$psDIR.$psScript;
            exec($runCMD, $out);
        }
    }
    else{
        $psDIR = "*******";
        $psScript = "drm_checkin_retrieve.ps1";
        $runCMD = $psPath. ' -ExecutionPolicy RemoteSigned '.$psDIR.$psScript;
        exec($runCMD, $out);
        echo "Check-in gestart...<br><br>";
        SetCheckinStatus();
    }
}

If you have access to your php.ini, this topic can be usefull

If you want to increase your timeout only in a specific script, you can use set_time_limit

Or another way, at the beginning of your php script :

ini_set('max_execution_time', 120); //120 seconds

I encountered a similar problem when using file_get_contents in combination with stream_context_create to call a REST API.

For some reason, file_get_contents sometimes seems to execute calls twice (maybe for error correction). This led to duplicate items being created via the API, even if our wrapper function has been called only once.

After switching to a cURL-based implementation, the error did not occur anymore. Here's an example: https://github.com/ubtue/tuefind/pull/2270/files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM