Add signal handler.

@param $signal
@param $callback
@return bool
337865
public function addSignal($signal, $callback)
    {
        $event = event_new();
        $this->_signalEvents[$signal] = $event;
        event_set($event, $signal, EV_SIGNAL | EV_PERSIST, $callback);
        event_base_set($event, $this->_eventBase);
        event_add($event);
    }
141800
public function receive(Signal $signal)
    {
        if (!$signal instanceof Signal\TrashService\TrashSignal) {
            return;
        }

        if ($signal->contentTrashed) {
            $this->searchHandler->deleteContent(
                $signal->contentId
            );
        }

        $this->searchHandler->deleteLocation(
            $signal->locationId,
            $signal->contentId
        );
    }
386254
protected function registerSignalHandlers(): bool
    {
        if (!function_exists('pcntl_signal')) {
            $this->log->warning('Signal Handling is not supported on this system');

            return false;
        }

        pcntl_async_signals(true);

        pcntl_signal(SIGTERM, [$this, 'shutdownNow']);
        pcntl_signal(SIGINT, [$this, 'shutdownNow']);
        pcntl_signal(SIGQUIT, [$this, 'shutdown']);
        pcntl_signal(SIGUSR1, [$this, 'killChild']);
        pcntl_signal(SIGUSR2, [$this, 'pauseProcessing']);
        pcntl_signal(SIGCONT, [$this, 'continueProcessing']);

        $this->log->debug('Registered Signals');

        return true;
    }
1269859
public function receive(Signal $signal)
    {
        if (!$signal instanceof Signal\ObjectStateService\UpdateObjectStateSignal) {
            return;
        }

        parent::receive($signal);
    }
23043
def add_signal(self, signal):
        """"""
        self.__signals.append(signal)
        if self.__connected:
            # Connects signal if the current state is "connected"
            self.__connect_signal(signal)
1195771
public function install(array $signals)
    {
        if (self::isSupported()) {
            $callback = [$this, 'dispatchSignal'];
            foreach ($signals as $signal) {
                \pcntl_signal($signal, $callback);
            }
        };
    }
1331620
public function stop($signal = SIGKILL, $wait = false)
    {
        if ($this->isAlive()) {
            posix_kill($this->_pid, $signal);
            if ($wait) {
                pcntl_waitpid($this->_pid, $status = 0);
            }
        }
    }
464380
public function signalReceived($signal): void
    {
        $form = $this->getForm();
        /** @var Presenter $presenter */
        $presenter = $this->getPresenter();
        $request = $presenter->getRequest();

        switch ($signal) {
            case 'submit':
                $this->processSubmit($form, $request);
                break;
            case 'validate':
                $this->processValidate($form, $request);
                break;
            case 'render':
                $this->processRender($form, $request);
                break;
            default:
                throw new BadSignalException(
                    sprintf('Missing handler for signal "%s" in %s.', $signal, get_class($this))
                );
        }
    }
817563
public function listen($event, $callback)
    {
        if (!isset($this->events[$event])) {
            $this->events[$event] = [];
        }

        $this->events[$event][] = $callback;
        return true;
    }
290890
function signal() {
  var handlers = []

  notify.add    = add
  notify.once   = once
  notify.remove = remove

  return notify


  // ### Function Signal
  //
  // Notifies all handlers of a signal.
  //
  // :: A... -> ()
  function notify() {
    var args = arguments
    handlers.forEach(function(f){ f.apply(null, args) }) }

  // ### Function add
  //
  // Adds a new handler.
  //
  // :: (A... -> B) -> Signal
  function add(f)  {
    if (handlers.every(isnt(f)))  handlers.push(f)
    return notify }

  // ### Function once
  //
  // Adds a new handler that'll be executed at most once.
  //
  // :: (A... -> B) -> Signal
  function once(f) {
    add(function() { f.apply(this, arguments)
                     remove(f) })
    return notify }

  // ### Function remove
  //
  // Removes a handler.
  //
  // :: (A... -> B) -> Signal
  function remove(f) {
    handlers = handlers.filter(isnt(f))
    return notify }
}
849077
public function signalToEvent($signal) {

        if ( $this->pid == ProcessTools::getPid() ) {

            $signame = $this->signals->signame($signal);

            $this->logger->debug("Received $signame ($signal) signal, firing associated event(s)");

            $this->events->emit(new PosixEvent($signal, $this));
            $this->events->emit(new PosixEvent($signame, $this));

        }

        return $this;

    }
547594
public function receive(Signal $signal)
    {
        if (!$signal instanceof Signal\ContentService\UpdateContentMetadataSignal) {
            return;
        }

        $contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($signal->contentId);
        if (!$contentInfo->isPublished) {
            return;
        }
        $this->searchHandler->indexContent(
            $this->persistenceHandler->contentHandler()->load($contentInfo->id, $contentInfo->currentVersionNo)
        );
        $this->searchHandler->indexLocation($this->persistenceHandler->locationHandler()->load($contentInfo->mainLocationId));
    }
373049
public function emit($signal, $args = null)
    {
        if ($this->signalsBlocked) {
            return;
        }

        if (isset($this->signals[$signal]) === false) {
            return;
        }

        /* @var $slots MSlot[] */
        $slots = $this->signals[$signal];

        foreach ($slots as /* @var $slot MSlot */
                 $slot) {
            $method = $slot->getMethod();
            $object = $slot->getReceiver();

            if ($args == null) {
                if ($object == null) {
                    $method();
                } else {
                    $object->$method();
                }
            } else {
                if ($object == null) {
                    $method($args);
                } else {
                    $object->$method($args);
                }
            }
        }
    }
720901
public function signalReceived($signal)
	{
		if (!$this->tryCall($this->formatSignalMethod($signal), $this->params)) {
			$class = get_class($this);
			throw new BadSignalException("There is no handler for signal '$signal' in class $class.");
		}
	}
341306
public function workerSignal(int $signal) {
        $this->log(LogLevel::DEBUG, "[{pid}] Caught signal '{$signal}' (SIGHUP) at worker - handing off to payload handler");

        switch ($signal) {

            // Daemon was asked to hang up
            case SIGHUP:
                $this->payloadExec('signal', [$signal]);
                break;

            // Custom signal - Nothing
            case SIGUSR1:
                $this->payloadExec('signal', [$signal]);
                break;

            // Custom signal - Nothing
            case SIGUSR2:
                $this->payloadExec('signal', [$signal]);
                break;
        }
    }
842993
public function signal_handler_sigchild($signal_number)
	{
		// do not allow signals to interrupt this
		pcntl_async_signals(false);
		{
			// reap all child zombie processes
			if (self::$parent_pid == getmypid())
			{
				$status = '';

				do
				{
					// get child pid that exited
					$child_pid = pcntl_waitpid(0, $status, WNOHANG);
					if ($child_pid > 0)
					{
						// child exited
						$identifier = false;
						if (!isset($this->forked_children[$child_pid]))
						{
							$this->log("Cannot find $child_pid in array! (This may be a subprocess not in our fork)", self::LOG_LEVEL_INFO);
							continue;
						}

						$child = $this->forked_children[$child_pid];
						$identifier = $child['identifier'];

						// call exit function if and only if its declared */
						if ($child['status'] == self::WORKER)
						{
							$this->invoke_callback($this->parent_function_child_exited[ $this->forked_children[$child_pid]['bucket'] ], array($child_pid, $this->forked_children[$child_pid]['identifier']), true);
						}

						// stop the child pid
						$this->forked_children[$child_pid]['status'] = self::STOPPED;
						$this->forked_children_count--;

						// respawn helper processes
						if ($child['status'] == self::HELPER && $child['respawn'] === true)
						{
							$this->log('Helper process ' . $child_pid . ' died, respawning', self::LOG_LEVEL_INFO);
							$this->helper_process_spawn($child['function'], $child['arguments'], $child['identifier'], true);
						}

						// Poll for results from any children
						$this->post_results($child['bucket']);
					}
					elseif ($child_pid < 0)
					{
						// ignore acceptable error 'No child processes' given we force this signal to run potentially when no children exist
						if (pcntl_get_last_error() == 10)
						{
							continue;
						}

						// pcntl_wait got an error
						$this->log('pcntl_waitpid failed with error ' . pcntl_get_last_error() . ':' . pcntl_strerror((pcntl_get_last_error())), self::LOG_LEVEL_DEBUG);
					}
				} while ($child_pid > 0);
			}
		}
	}
375900
protected function createSignalCallback(): callable
    {
        return function ($signo) {
            $handled = false;

            if (isset($this->signals[$signo])) {
                foreach ($this->signals[$signo] as $signal) {
                    $handled = true;
                    $signal->call();
                }
            }

            switch ($signo) {
                case SIGHUP:
                case SIGINT:
                case SIGQUIT:
                case SIGABRT:
                case SIGTRAP:
                case SIGXCPU:
                    if (!$handled) {
                        $this->loop->stop();
                    }
                    break;

                case SIGTERM:
                case SIGBUS:
                case SIGSEGV:
                case SIGFPE:
                    $this->loop->stop();
                    break;
            }
        };
    }