jeudi 28 mars 2019

Command execution timesout when using Process in Laravel

I am building website screenshots SaaS with Laravel. I am trying to execute a command from it, more precisely a nodejs script; however I keep getting ProcessTimedOutException error. Here is where the magic happens, look at the take method:

<?php

namespace App\Logic;

use App\Logic\TimeHelper;
use App\UrlRequest;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class Screenshot {

    static function take(UrlRequest $urlRequest)
    {
        $name = self::generateName($urlRequest);
        $command = self::buildScreenshotCommand($name, $urlRequest);

        $startTime = TimeHelper::milliseconds();

        $process = new Process($command);
        $process->run();

        $endTime = TimeHelper::milliseconds();

        if (!$process->isSuccessful())
        {
            throw new ProcessFailedException($process);
        }

        $output = $process->getOutput();

        if (trim($output) === '')
        {
            $urlRequest->successful = 1;

            $file = self::uploadToS3($name);
            $urlRequest->image_url = $file['url'];
            $urlRequest->file_name = $name;
            $urlRequest->file_size = $file['size'];
            $urlRequest->time_it_took_to_take_screenshot_ms = $endTime - $startTime;
        }
        else
        {
            $urlRequest->error = $output;
        }

        if ($urlRequest->save())
        {
            return $urlRequest;
        }

        return false;
    }

    static function uploadToS3($name)
    {
        $name = 'screenshots/' . $name;

        Storage::disk('s3')->put($name, Storage::disk('local')->get($name), ['visibility' => 'public']); // upload to S3

        $fileSize = Storage::disk('local')->size($name);
        Storage::disk('local')->delete($name);

        return [
            'url' => Storage::disk('s3')->url($name),
            'size' => $fileSize
        ];
    }

    static function generateName($urlRequest)
    {
        $name = time() . rand(10000, 99999);
        $extension = '.png';

        if (isset($urlRequest->pdf) AND $urlRequest->pdf == 1)
        {
            $extension = '.pdf';
        }

        while (UrlRequest::where('file_name', '=', $name . $extension)->first())
        {
            $name = time() . rand(10000, 99999);
        }

        return $name . $extension;
    }

    static function buildScreenshotCommand($name, $urlRequest)
    {
        $command = 'cd ' . base_path() . ' && node puppeteer-screenshots-init.js ';
        $command .= "--url={$urlRequest->url} ";

        $fullPath = storage_path('app') . '/screenshots/' . $name;

        $command .= "--path={$fullPath} ";

        if (isset($urlRequest->pdf))
        {
            $command .= "--pdf=true ";
        }

        if (isset($urlRequest->viewport_width))
        {
            $command .= "--viewportWidth={$urlRequest->viewport_width} ";
        }

        if (isset($urlRequest->mobile))
        {
            $command .= '--mobile=true ';
        }

        if (isset($urlRequest->media_type_print))
        {
            $command .= '--mediaTypePrint=true ';
        }

        if (isset($urlRequest->user_agent))
        {
            $command .= "--userAgent={$urlRequest->user_agent}";
        }

        return $command;
    }

}

Here is the nodejs script:

require('puppeteer-screenshots').init();

puppeteer-screenshots is a package which I've written for taking screenshots in an easy manner using Google's Puppeteer package.

I've tried rewriting the code with exec instead of Process but in that case the script just hangs and it doesn't finish.

Also, when executing the exact same command from the command line everything works as expected.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire