Register a temporary server connection failure

This method will attempt to temporarily excise the offending server from
the connect roster for a period of time.

@param string $server
552264
public function Fail($server) {
      
      // Use APC?
      $apc = false;
      if (C('Garden.Apc', false) && function_exists('apc_fetch'))
         $apc = true;

      // Get the active cache name
      $activeCache = Gdn_Cache::ActiveCache();
      $activeCache = ucfirst($activeCache);
      $sctiveStoreKey = "Cache.{$activeCache}.Store";

      // Get the localstore
      $localStore = GetValue($activeCache, Gdn_Cache::$Stores, null);
      if (is_null($localStore)) {
         Gdn_Cache::ActiveStore();
         $localStore = GetValue($activeCache, Gdn_Cache::$Stores, null);
         if (is_null($localStore)) {
            return false;
         }
      }

      $storeServerName = md5($server);
      if (!array_key_exists($storeServerName, $localStore)) {
         return false;
      }

      $storeServer = &$localStore[$storeServerName];
      $isActive = &$storeServer['Active'];
      if (!$isActive) return false;

      $fails = &$storeServer['Fails'];
      $fails++;
      $active = $isActive ? 'active' : 'inactive';

      // Check if we need to deactivate for 5 minutes
      if ($isActive && $storeServer['Fails'] > 3) {
         $isActive = false;
         $storeServer['Delay'] = time() + Gdn_Cache::CACHE_EJECT_DURATION;
      }

      // Save
      Gdn_Cache::$Stores[$activeCache] = $localStore;
      
      // Save to APC
      if ($apc) {
         apc_store($sctiveStoreKey, $localStore, Gdn_Cache::APC_CACHE_DURATION);
      }
      
      return true;
   }
1242131
protected function validateServer($server)
    {
        if (in_array($server, array_keys($this->servers)))
        {
            // Get server options
            $options = $this->config->get($server);

            if (!$options || $options['host'] === 'yoursite') {
                throw new \Exception("Remote server \"{$server}\" not setup in remote.yml.");
            }

            if (shell_exec("which {$this->servers[$server]}") == '') {
                throw new \Exception("Shell command '' is required to publish using {$server}");
            }

            return true;
        }

        throw new \Exception("Unknown remote server: \"{$server}\"");
    }
570947
public function registerFatalAction()
    {
        // As of PHP Version 7.0.2, the register_tick_function() causes PHP to
        // crash, with a connection reset! We need to check the version to avoid
        // this, and then tell the dev what happened.
        // Not to mention that fatals got removed anyway.
        if (version_compare(phpversion(), '7.0.0', '>=')) {
            // Too high! 420 Method Failure :-(
            $this->pool->messages->addMessage($this->pool->messages->getHelp('php7yellow'));
            krexx($this->pool->messages->getHelp('php7'));

            // Just return, there is nothing more to do here.
            return $this;
        }

        // Do we need another shutdown handler?
        if (static::$krexxFatal === null) {
            static::$krexxFatal = $this->pool->createClass('Brainworxx\\Krexx\\Errorhandler\\Fatal');
            declare(ticks = 1);
            register_shutdown_function(
                array(
                    static::$krexxFatal,
                    'shutdownCallback',
                )
            );
        }

        static::$krexxFatal->setIsActive(true);
        $this->fatalShouldActive = true;
        register_tick_function(array($this::$krexxFatal, 'tickCallback'));

        return $this;
    }
1267524
public function onClose($server, $serverSocket, $id)
    {
        if (isset($this->connections[$id])) {
            $this->fireCloseEvent(
                $this->connections[$id]['handler'],
                $this->connections[$id]['connection']
            );

            unset($this->connections[$id]);
        }
    }
1182524
public function listen(Server $server, SwoolePort $swoolePort = null)
    {
        if ($this->isBound()) {
            throw new LogicException('The server socket can be bound to server only once.');
        }

        if ($swoolePort == null) {
            // If the passed Swoole port is null, that means we are calling this
            // method directly, so here we need to call the server method "listen"
            // to do the actual binding.
            return $server->listen($this);
        }

        $this->server = $server;
        $this->swoolePort = $swoolePort;

        $this->swoolePort->set($this->options);

        $this->registerEventCallbacks($this->serverSocketEvents);

        $this->dispatchEvent('Bind', $server, $this);
    }
293800
private static void register(ObjectName objectName, Object mbean) {
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    try {
      if (!server.isRegistered(objectName)) {
        server.registerMBean(mbean, objectName);
      }
    } catch (InstanceAlreadyExistsException
        | MBeanRegistrationException | NotCompliantMBeanException e) {
      throw new CacheException("Error registering " + objectName, e);
    }
  }
911272
public function fail($e = null)
    {
        $this->markAsFailed();

        if ($this->isDeleted()) {
            return;
        }

        try {
            // If the job has failed, we will delete it, call the "failed" method and then call
            // an event indicating the job has failed so it can be logged if needed. This is
            // to allow every developer to better keep monitor of their failed queue jobs.
            $this->delete();

            $this->failed($e);
        } finally {
            $this->resolve(Dispatcher::class)->dispatch(new JobFailed(
                $this->connectionName, $this, $e ?: new ManuallyFailedException
            ));
        }
    }
732440
protected function processClientSocket($socket): void
    {
        $connection = $this->getConnectionForClientSocket($socket);

        if (!$connection) {
            $this->logger->warning('No connection for client socket');
            return;
        }

        try {
            $this->server->notify(Server::EVENT_CLIENT_DATA, [$socket, $connection]);

            $connection->process();
        } catch (CloseException $e) {
            $this->logger->notice('Client connection closed: {exception}', [
                'exception' => $e,
            ]);
            $connection->close(Protocol::CLOSE_UNEXPECTED, $e->getMessage());
        } catch (WrenchException $e) {
            $this->logger->warning('Error on client socket: {exception}', [
                'exception' => $e,
            ]);
            $connection->close(Protocol::CLOSE_UNEXPECTED);
        } catch (InvalidArgumentException $e) {
            $this->logger->warning('Wrong input arguments: {exception}', [
                'exception' => $e,
            ]);
            $connection->close(Protocol::CLOSE_UNEXPECTED);
        }
    }
687930
protected function registerConnectCallback()
    {
        $this->swoolePort->on('Connect', function ($server, $fd, $reactorId) {
            $this->dispatchEvent('Connect', $this->server, $this, $fd, $reactorId);
        });
    }
945072
protected function configureServerSockets($server, array $serverSockets)
    {
        foreach ($serverSockets as $config) {
            $serverSocket = $this->createServerSocket($config);

            if ($handler = $this->parseEventHandler($config)) {
                $serverSocket->setEventHandler($handler);
            }

            $server->listen($serverSocket);
        }
    }
386515
protected function registerShutdownCallback()
    {
        $this->swooleServer->on('Shutdown', function () {
            $this->events->dispatch(new Events\ServerShutdown($this));

            $this->dispatchEvent('Shutdown', $this);
        });
    }
249279
protected function registerConnectCallback()
    {
        $this->swooleServer->on('Connect', function ($server, $fd, $reactorId) {
            $this->dispatchEvent('Connect', $this, $fd, $reactorId);
        });
    }
835982
public static function registerConnections(array $drivers)
    {
        $manager = static::getInstance();

        foreach ($drivers as $name => $driver) {
            $class = __NAMESPACE__ . '\\' . studly_case($name) . 'Connection';

            if (class_exists($class)) {
                $driver = (new $class())->configure($driver);
                $manager->register($driver);
            } else {
                throw new DriverNotFound("Connection {$name} is not supported");
            }
        }
    }
1345059
public function close(?string $connection = null): void
	{
		$connection = $connection ?? $this->default;

		if(isset($this->connections[$connection]))
		{
			if(method_exists($this->connections[$connection], 'close'))
			{
				// Call close on the connection object in case there's a reference
				// to the connection that won't be garbage collected immediately

				$this->connections[$connection]->close();
			}

			unset($this->connections[$connection]);
		}
	}
481858
public function register($pid, ConnectionInterface $connection)
    {
        if ($this->status !== self::CREATED) {
            throw new \LogicException('Cannot register a slave that is not in created state');
        }

        $this->pid = $pid;
        $this->connection = $connection;

        $this->status = self::REGISTERED;
    }
74659
function checkinConnection(\Plasma\DriverInterface $driver): void {
        if($driver->getConnectionState() !== \Plasma\DriverInterface::CONNECTION_UNUSABLE && !$this->goingAway) {
            $this->connections->add($driver);
            $this->busyConnections->delete($driver);
        }
    }
1237412
protected function registerCloseCallback()
    {
        $this->swooleServer->on('Close', function ($server, $fd, $reactorId) {
            $this->dispatchEvent('Close', $this, $fd, $reactorId);
        });
    }