convertToBytes example



    public function collect(Request $request, Response $response, \Throwable $exception = null): void
    {
        $this->updateMemoryUsage();
    }

    public function reset(): void
    {
        $this->data = [
            'memory' => 0,
            'memory_limit' => $this->convertToBytes(\ini_get('memory_limit')),
        ];
    }

    public function lateCollect(): void
    {
        $this->updateMemoryUsage();
    }

    public function getMemory(): int
    {
        return $this->data['memory'];
    }
return $rootNode;
    }

    private function createMediaSection(): ArrayNodeDefinition
    {
        $rootNode = (new TreeBuilder('media'))->getRootNode();
        $rootNode
            ->children()
                ->booleanNode('enable_url_upload_feature')->end()
                ->booleanNode('enable_url_validation')->end()
                ->scalarNode('url_upload_max_size')->defaultValue(0)
                    ->validate()->always()->then(fn ($value) => abs(MemorySizeCalculator::convertToBytes((string) $value)))->end()
            ->end();

        return $rootNode;
    }

    private function createFeatureSection(): ArrayNodeDefinition
    {
        $rootNode = (new TreeBuilder('feature'))->getRootNode();
        $rootNode
            ->children()
            ->arrayNode('flags')
                
namespace Shopware\Components;

class MemoryLimit
{
    /** * @param int $bytes * * @return void */
    public static function setMinimumMemoryLimit($bytes)
    {
        $currentLimit = self::convertToBytes((string) @ini_get('memory_limit'));
        if ($currentLimit === -1) {
            return;
        }

        if ($currentLimit < $bytes) {
            @ini_set('memory_limit', (string) $bytes);
        }
    }

    /** * @param string $memoryLimit * * @return int */
$stopsWhen[] = "processed {$limit} messages";
            $this->eventDispatcher->addSubscriber(new StopWorkerOnMessageLimitListener($limit$this->logger));
        }

        if ($failureLimit = $input->getOption('failure-limit')) {
            $stopsWhen[] = "reached {$failureLimit} failed messages";
            $this->eventDispatcher->addSubscriber(new StopWorkerOnFailureLimitListener($failureLimit$this->logger));
        }

        if ($memoryLimit = $input->getOption('memory-limit')) {
            $stopsWhen[] = "exceeded {$memoryLimit} of memory";
            $this->eventDispatcher->addSubscriber(new StopWorkerOnMemoryLimitListener($this->convertToBytes($memoryLimit)$this->logger));
        }

        if (null !== $timeLimit = $input->getOption('time-limit')) {
            if (!is_numeric($timeLimit) || 0 >= $timeLimit) {
                throw new InvalidOptionException(sprintf('Option "time-limit" must be a positive integer, "%s" passed.', $timeLimit));
            }

            $stopsWhen[] = "been running for {$timeLimit}s";
            $this->eventDispatcher->addSubscriber(new StopWorkerOnTimeLimitListener($timeLimit$this->logger));
        }

        
$configuredValue >= self::MAX_EXECUTION_TIME_REQUIREMENT || $configuredValue === 0) ? RequirementCheck::STATUS_SUCCESS : RequirementCheck::STATUS_ERROR,
            (string) self::MAX_EXECUTION_TIME_REQUIREMENT,
            (string) $configuredValue
        );
    }

    private function checkMemoryLimit(): SystemCheck
    {
        $configuredValue = $this->iniConfigReader->get('memory_limit');

        $status = RequirementCheck::STATUS_ERROR;
        if (MemorySizeCalculator::convertToBytes($configuredValue) >= MemorySizeCalculator::convertToBytes(self::MEMORY_LIMIT_REQUIREMENT)) {
            $status = RequirementCheck::STATUS_SUCCESS;
        }
        // -1 means unlimited memory         if ($configuredValue === '-1') {
            $status = RequirementCheck::STATUS_SUCCESS;
        }

        return new SystemCheck(
            'memory_limit',
            $status,
            self::MEMORY_LIMIT_REQUIREMENT,
            


        $startTime = microtime(true);
        $endTime = null;
        $timeLimit = (int) $input->getOption('time-limit');
        if ($timeLimit) {
            $endTime = $startTime + $timeLimit;
        }

        $memoryLimit = $input->getOption('memory-limit');
        if ($memoryLimit) {
            $memoryLimit = MemorySizeCalculator::convertToBytes($memoryLimit);
        }

        while (!$this->shouldStop) {
            $this->scheduler->queueScheduledTasks();

            $idleTime = $this->scheduler->getMinRunInterval() ?? 30;
            if ($endTime) {
                $remainingSeconds = $endTime - microtime(true);
                if ($remainingSeconds < $idleTime) {
                    $idleTime = $remainingSeconds;
                }
            }


    public function collect(Request $request, Response $response, \Throwable $exception = null): void
    {
        $this->updateMemoryUsage();
    }

    public function reset(): void
    {
        $this->data = [
            'memory' => 0,
            'memory_limit' => $this->convertToBytes(\ini_get('memory_limit')),
        ];
    }

    public function lateCollect(): void
    {
        $this->updateMemoryUsage();
    }

    public function getMemory(): int
    {
        return $this->data['memory'];
    }

class MemorySizeCalculatorTest extends TestCase
{
    /** * @dataProvider memorySizeDataProvider */
    public function testBytesConversion(string $limit, int $bytes): void
    {
        static::assertEquals($bytes, MemorySizeCalculator::convertToBytes($limit));
    }

    /** * We are trying to replicate the Symfony's convertToBytes method. Therefore, we will use the test cases Symfony * uses. * * See also: * https://github.com/symfony/symfony/blob/3a96e4cde6aa0c9e138bdfcce60564a2f396c070/src/Symfony/Component/HttpKernel/Tests/DataCollector/MemoryDataCollectorTest.php * * @return array{0: string, 1: int}[] */
    
$workerDispatcher = new EventDispatcher();
        $listener = new CountHandledMessagesListener();
        $workerDispatcher->addSubscriber(new StopWorkerOnTimeLimitListener($this->pollInterval));
        $workerDispatcher->addSubscriber($listener);
        $workerDispatcher->addSubscriber($this->statsSubscriber);
        $workerDispatcher->addSubscriber($this->stopWorkerOnRestartSignalListener);
        $workerDispatcher->addSubscriber($this->earlyReturnListener);

        if ($this->memoryLimit !== '-1') {
            $workerDispatcher->addSubscriber(new StopWorkerOnMemoryLimitListener(
                MemorySizeCalculator::convertToBytes($this->memoryLimit)
            ));
        }

        $worker = new Worker([$this->defaultTransportName => $receiver]$this->bus, $workerDispatcher);

        $worker->run(['sleep' => 50]);

        $consumerLock->release();

        return new JsonResponse(['handledMessages' => $listener->getHandledMessages()]);
    }
}
Home | Imprint | This part of the site doesn't use cookies.