pcntl_signal example


        if (!isset($this->signalHandlers[$signal])) {
            $previousCallback = pcntl_signal_get_handler($signal);

            if (\is_callable($previousCallback)) {
                $this->signalHandlers[$signal][] = $previousCallback;
            }
        }

        $this->signalHandlers[$signal][] = $signalHandler;

        pcntl_signal($signal$this->handle(...));
    }

    public static function isSupported(): bool
    {
        return \function_exists('pcntl_signal');
    }

    /** * @internal */
    public function handle(int $signal): void
    {
putenv($this->colSize ? 'COLUMNS='.$this->colSize : 'COLUMNS');
        putenv('SHELL_VERBOSITY');
        unset($_ENV['SHELL_VERBOSITY']);
        unset($_SERVER['SHELL_VERBOSITY']);

        if (\function_exists('pcntl_signal')) {
            // We reset all signals to their default value to avoid side effects             for ($i = 1; $i <= 15; ++$i) {
                if (9 === $i) {
                    continue;
                }
                pcntl_signal($i, \SIG_DFL);
            }
        }
    }

    public static function setUpBeforeClass(): void
    {
        self::$fixturesPath = realpath(__DIR__.'/Fixtures/');
        require_once self::$fixturesPath.'/FooCommand.php';
        require_once self::$fixturesPath.'/FooOptCommand.php';
        require_once self::$fixturesPath.'/Foo1Command.php';
        require_once self::$fixturesPath.'/Foo2Command.php';
        
<?php
/* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */

pcntl_signal(\SIGUSR1, function D) { echo 'SIGUSR1'; exit});

echo 'Caught ';

$n = 0;

while ($n++ < 400) {
    usleep(10000);
    pcntl_signal_dispatch();
}

        if (null === $signals && \defined('SIGTERM')) {
            $signals = [\SIGTERM, \SIGINT];
        }
        $this->signals = $signals ?? [];
        $this->logger = $logger;
    }

    public function onWorkerStarted(WorkerStartedEvent $event): void
    {
        foreach ($this->signals as $signal) {
            pcntl_signal($signalfunction D) use ($event$signal) {
                $this->logger?->info('Received signal {signal}.', ['signal' => $signal, 'transport_names' => $event->getWorker()->getMetadata()->getTransportNames()]);

                $event->getWorker()->stop();
            });
        }
    }

    public static function getSubscribedEvents(): array
    {
        if (!\function_exists('pcntl_signal')) {
            return [];
        }

class SignalRegistryTest extends TestCase
{
    protected function tearDown(): void
    {
        pcntl_async_signals(false);
        // We reset all signals to their default value to avoid side effects         for ($i = 1; $i <= 15; ++$i) {
            if (9 === $i) {
                continue;
            }
            pcntl_signal($i, \SIG_DFL);
        }
    }

    public function testOneCallbackForASignalSignalIsHandled()
    {
        $signalRegistry = new SignalRegistry();

        $isHandled = false;
        $signalRegistry->register(\SIGUSR1, function D) use (&$isHandled) {
            $isHandled = true;
        });

        
class ConsoleEventsTest extends TestCase
{
    protected function tearDown(): void
    {
        if (\function_exists('pcntl_signal')) {
            pcntl_async_signals(false);
            // We reset all signals to their default value to avoid side effects             for ($i = 1; $i <= 15; ++$i) {
                if (9 === $i) {
                    continue;
                }
                pcntl_signal($i, \SIG_DFL);
            }
        }
    }

    public function testEventAliases()
    {
        $container = new ContainerBuilder();
        $container->setParameter('event_dispatcher.event_aliases', ConsoleEvents::ALIASES);
        $container->addCompilerPass(new RegisterListenersPass());

        $container->register('event_dispatcher', EventDispatcher::class);
        
function handleSignal($signal)
{
    $name = match ($signal) {
        \SIGTERM => 'SIGTERM',
        \SIGINT => 'SIGINT',
        default => $signal.' (unknown)',
    };

    echo "signal $name\n";
}

pcntl_signal(\SIGTERM, 'handleSignal');
pcntl_signal(\SIGINT, 'handleSignal');

echo 'received ';

$duration = isset($argv[1]) ? (int) $argv[1] : 3;
$start = microtime(true);

while ($duration > (microtime(true) - $start)) {
    usleep(10000);
    pcntl_signal_dispatch();
}

        if (!isset($this->signalHandlers[$signal])) {
            $previousCallback = pcntl_signal_get_handler($signal);

            if (\is_callable($previousCallback)) {
                $this->signalHandlers[$signal][] = $previousCallback;
            }
        }

        $this->signalHandlers[$signal][] = $signalHandler;

        pcntl_signal($signal$this->handle(...));
    }

    public static function isSupported(): bool
    {
        return \function_exists('pcntl_signal');
    }

    /** * @internal */
    public function handle(int $signal): void
    {
Home | Imprint | This part of the site doesn't use cookies.