1

私はpdftkを使ってpdfファイルをマージしています。時折、ユーザーが不適切な形式の PDF をアップロードすると、プロセスがハングアップし、エラーが返されず、すべてのサーバー リソースが消費されます。これを防ぐために、プロセス呼び出しを実装することを検討してproc_openおり、プロセスが実行する時間制限を設定し、制限時間を超えた場合にプロセスを終了したいと考えています。

以下は、設定した場合にpdfファイルをマージするために使用している関数の例です

stream_set_blocking($process, 0);

エラーを返します。

stream_set_blocking(): supplied resource is not a valid stream resource

この関数の何かが間違っていると推測し、誰かが私を正しい方向に向けることができることを願っています...関数は現在エラーを返していませんが、必要に応じて30秒後に終了しません

 protected function pdf_merge($documents,$output_file,$time = 30){      

        $end = time() + $time;
        $cmd = sprintf('/usr/local/bin/pdftk %s cat output %s', $documents, $output_file);

        $descriptorspec = array(
           0 => array("pipe", "r"),  
           1 => array("pipe", "w"),  
           2 => array("file","./error.log","a")
        );

        $process = proc_open($cmd, $descriptorspec, $pipes);

        if (is_resource($process)) {
            stream_set_blocking($pipes[1], 0);
            while (!feof($pipes[1]) && (time() < $end)) {

                fwrite($pipes[0], stream_get_contents($pipes[0]));
                fclose($pipes[0]);

                $pdf_content = stream_get_contents($pipes[1]);
                fclose($pipes[1]);

                $return_value = proc_close($process);

                return $return_value;
            }   
            error_log('file is taking too long... kill process');
            proc_terminate();               
        }
    }
4

1 に答える 1

0

Perhaps I'm off base here, does most of this code run?

fwrite($pipes[0], stream_get_contents($pipes[0]));

Looks like you are trying to write to the pipe whatever you can read from the pipe...

Also, you may want to check with get_proc_status() as the command may have completed by the time you try to set the blocking...

于 2013-02-05T13:02:21.437 に答える