0 follower

Final Class Yiisoft\Queue\Cli\SignalLoop

InheritanceYiisoft\Queue\Cli\SignalLoop
ImplementsYiisoft\Queue\Cli\LoopInterface
Uses TraitsYiisoft\Queue\Cli\SoftLimitTrait

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Queue\Cli\SignalLoop
canContinue() Checks signals state. Yiisoft\Queue\Cli\SignalLoop

Constants

Hide inherited constants

Constant Value Description Defined By
SIGNALS_EXIT [ SIGHUP, SIGINT, SIGTERM, ] Yiisoft\Queue\Cli\SignalLoop
SIGNALS_RESUME [ SIGCONT, ] Yiisoft\Queue\Cli\SignalLoop
SIGNALS_SUSPEND [ SIGTSTP, ] Yiisoft\Queue\Cli\SignalLoop

Property Details

Hide inherited properties

$exit protected property
protected boolean $exit false
$memorySoftLimit protected property
protected integer $memorySoftLimit 0
$pause protected property
protected boolean $pause false

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( integer $memorySoftLimit 0 )
$memorySoftLimit integer

Soft RAM limit in bytes. The loop won't let you continue to execute the program if soft limit is reached. Zero means no limit.

                public function __construct(protected int $memorySoftLimit = 0)
{
    foreach (self::SIGNALS_EXIT as $signal) {
        pcntl_signal($signal, fn () => $this->exit = true);
    }
    foreach (self::SIGNALS_SUSPEND as $signal) {
        pcntl_signal($signal, fn () => $this->pause = true);
    }
    foreach (self::SIGNALS_RESUME as $signal) {
        pcntl_signal($signal, fn () => $this->pause = false);
    }
}

            
canContinue() public method

Checks signals state.

{@inheritdoc}

public boolean canContinue ( )

                public function canContinue(): bool
{
    if ($this->memoryLimitReached()) {
        return false;
    }
    return $this->dispatchSignals();
}

            
dispatchSignals() protected method

protected boolean dispatchSignals ( )

                protected function dispatchSignals(): bool
{
    pcntl_signal_dispatch();
    // Wait for resume signal until the loop is suspended
    while ($this->pause && !$this->exit) {
        usleep(10000);
        pcntl_signal_dispatch();
    }
    return !$this->exit;
}

            
getMemoryLimit() protected method

protected integer getMemoryLimit ( )

                protected function getMemoryLimit(): int
{
    return $this->memorySoftLimit;
}

            
memoryLimitReached() protected method
protected boolean memoryLimitReached ( )

                protected function memoryLimitReached(): bool
{
    $limit = $this->getMemoryLimit();
    if ($limit !== 0) {
        $usage = memory_get_usage(true);
        if ($usage >= $limit) {
            return true;
        }
    }
    return false;
}