You are here

function _varnish_terminal_run in Varnish 5

Same name and namespace in other branches
  1. 8 varnish.module \_varnish_terminal_run()
  2. 6 varnish.module \_varnish_terminal_run()
  3. 7 varnish.module \_varnish_terminal_run()

Helper function that sends commands to Varnish

Utilizes sockets to talk to varnish terminal.

4 calls to _varnish_terminal_run()
varnish_admin_reports_page in ./varnish.module
Menu callback for varnish admin settings.
varnish_admin_settings_form in ./varnish.module
Menu callback for varnish admin settings.
varnish_purge_all_pages in ./varnish.module
Helper function to quickly flush all caches for the current site.
varnish_requirements in ./varnish.module
Implementation of hook_requirements()

File

./varnish.module, line 207
varnish.module Provide drupal hooks for integration with the Varnish control layer.

Code

function _varnish_terminal_run($command, $returnlength = 1000) {
  if (!extension_loaded('sockets')) {
    drupal_set_message(t('Sockets extension not enabled. Varnish terminal communication aborted.'), 'error');
    return FALSE;
  }
  $server = variable_get('varnish_control_terminal_server', '127.0.0.1');
  $port = variable_get('varnish_control_terminal_port', '6082');
  $client = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
  if (!socket_connect($client, $server, $port)) {
    watchdog('varnish', t('Unable to connect to server socket !server:!port', array(
      '!server' => $server,
      '!port' => $port,
    )));
    return FALSE;
  }
  socket_write($client, "{$command}\n");
  $code = socket_read($client, 3, PHP_BINARY_READ);
  if ($code != 200) {
    $error = socket_read($client, 3000, PHP_BINARY_READ);
    watchdog('varnish', t('Recieved status code !code running !command. Full response text: !error', array(
      '!code' => $code,
      '!command' => $command,
      '!error' => $error,
    )));
    $ret = FALSE;
  }
  else {

    // successful connection
    $ret = socket_read($client, $returnlength, PHP_BINARY_READ);
  }
  socket_close($client);
  return $ret;
}