Esempio n. 1
0
 /**
  * @param string $socketAddress
  *
  * @throws Exception\BindOrListenException
  * @throws Exception\RuntimeException
  *
  * @return resource
  */
 private static function startListening($socketAddress)
 {
     $lock = Lock::acquire();
     if (self::$killSwitchPath !== null) {
         $kswitch = new KillSwitch(self::$killSwitchPath);
         if ($kswitch->getGlobal() || $kswitch->hasAddress($socketAddress)) {
             throw new Exception\RuntimeException('This worker has been prevented from starting using the kill switch');
         }
     }
     $socketFile = IdentificationHelper::getSocketFile($socketAddress);
     if ($socketFile !== null) {
         $socketDir = dirname($socketFile);
         if (!is_dir($socketDir)) {
             mkdir($socketDir, 0777, true);
         }
     }
     $server = SocketFactory::createServerSocket($socketAddress, self::$socketContext);
     $lock->release();
     self::$socket = $server;
     self::$listening = true;
     self::$toDelete = $socketFile;
     return $server;
 }
Esempio n. 2
0
 /**
  * @param callable $operation
  *
  * @throws Exception\LogicException
  *
  * @return mixed|$this
  */
 private function transactKillSwitch(callable $operation)
 {
     $ksPath = $this->bootstrapProfile->getKillSwitchPath();
     if ($ksPath === null) {
         throw new Exception\LogicException('No kill switch has been configured');
     }
     $lock = Lock::acquire();
     $kswitch = new KillSwitch($ksPath);
     $retval = call_user_func($operation, $kswitch);
     $kswitch->save();
     $lock->release();
     return $retval !== null ? $retval : $this;
 }