vuongxuongminh / laravel-async

Package provide simple way to run code asynchronously for your Laravel application.
MIT License
154 stars 26 forks source link

File are not removed for big payload size #48

Open BBonnet22 opened 2 months ago

BBonnet22 commented 2 months ago

When $maxTaskPayloadInBytes is greater than 100000 (by default), the \Spatie\Async\Runtime\ParentRuntime::encodeTask function creates a temporary file in the /tmp directory.

if (strlen($serializedTask) > $maxTaskPayloadInBytes) {
    // Write the serialized task to a temporary file and package it as a `FileTask`:
    $filename = tempnam(sys_get_temp_dir(), 'spatie_async_task_');
    file_put_contents($filename, $serializedTask);
    $file_task = new FileTask($filename);
    $serializedTask = base64_encode(serialize($file_task));
}

However, this function is called twice, resulting in two temporary files being created. At the end of the process, only one of these files is removed, leaving the other as a lingering temporary file.

The \VXM\Async\Runtime\ParentRuntime::createProcess method calls its parent method \Spatie\Async\Runtime\ParentRuntime::createProcess. Is there a specific reason for this? With this approach, everything looks to work fine

public static function createProcess($task, ?int $outputLength = null, ?string $binary = 'php', ?int $max_input_size = 100000): Runnable
    {
        if (! self::$isInitialised) {
            self::init();
        }

        if (! Pool::isSupported()) {
            return SynchronousProcess::create($task, self::getId());
        }

        $process = new Process([
            $binary,
            static::$childProcessScript,
            static::$autoloader,
            static::encodeTask($task),
            $outputLength,
            base_path(),
        ]);

        return ParallelProcess::create($process, self::getId());
    }