Skip to content

Instantly share code, notes, and snippets.

@opengeek
Created October 4, 2011 14:48
Show Gist options
  • Save opengeek/1261818 to your computer and use it in GitHub Desktop.
Save opengeek/1261818 to your computer and use it in GitHub Desktop.
RemoteCommands and SendClearCache plugins for MODX Revolution 2.2+
<?php
/* RemoteCommands plugin -- register with OnHandleRequest OR OnWebPageComplete event */
/* define the IP of the master instance which does not need to execute remote commands */
$master_instance = $modx->getOption('master_instance', $scriptProperties, '127.0.0.1');
/* get the instance IP */
$instance = $_SERVER['SERVER_ADDR'];
/* the number of seconds the remote command is valid for */
$seconds = !empty($seconds) ? intval($seconds) : 1440;
/* find any remote commands to execute from the master instance */
if (!empty($instance) && $modx->getService('registry', 'registry.modRegistry') && $instance !== $master_instance) {
$modx->registry->addRegister('remotes', 'registry.modDbRegister', array('directory' => 'remotes'));
$modx->registry->remotes->connect();
/* if not already registered, register this instance for $seconds */
$modx->registry->remotes->subscribe("/distrib/instances/{$instance}");
$registration = $modx->registry->remotes->read(array('poll_limit' => 1, 'msg_limit' => 1, 'remove_read' => false));
$modx->registry->remotes->unsubscribe("/distrib/instances/{$instance}");
if (empty($registration) || !reset($registration)) {
$modx->registry->remotes->subscribe("/distrib/instances/");
$modx->registry->remotes->send("/distrib/instances/", array($instance => "{$instance}"), array('ttl' => $seconds));
$modx->registry->remotes->unsubscribe("/distrib/instances/");
}
/* find any valid command messages for this instance and act on them */
$modx->registry->remotes->subscribe("/distrib/commands/{$instance}/");
$commands = $modx->registry->remotes->read(array('poll_limit' => 1, 'msg_limit' => 1));
$modx->registry->remotes->unsubscribe("/distrib/commands/{$instance}/");
if (!empty($commands)) {
$command = reset($commands);
if (!empty($command)) {
/* customize this with your own command handlers */
switch ($command) {
/* refresh the remote instance's cache */
case 'clearCache':
switch ($modx->event->name) {
case 'OnHandleRequest':
$modx->reloadConfig();
$modx->reloadContext();
break;
case 'OnWebPageComplete':
$cacheRefreshOptions = array(
'auto_publish' => array( 'contexts' => array('web') ),
'system_settings' => array(),
'db' => array(),
'context_settings' => array( 'contexts' => array('web') ),
'scripts' => array(),
'resource' => array( 'contexts' => array('web') ),
'default' => array(),
);
$results = $modx->cacheManager->refresh($cacheRefreshOptions);
break;
}
break;
case 'clearErrorLog':
/* empty the remote instance's error.log */
$modx->cacheManager->writeFile($modx->getCachePath() . 'logs/error.log', '', 'wb');
break;
default:
break;
}
}
}
}
<?php
/* SendClearCache plugin -- register with OnSiteRefresh event */
/* seconds property defines a default ttl value for the clearCache message [default=1440] */
$seconds = !empty($seconds) ? intval($seconds) : 1440;
/* number of seconds to delay the clearCache message before it should be executed [default=0] */
$delay = !empty($delay) ? intval($delay) : 0;
/* number of seconds to stagger the clearCache messages between instances [default=5] */
$stagger = !empty($stagger) ? intval($stagger) : 15;
/* read instances and write clear cache msg to each command directory */
if ($modx->getService('registry', 'registry.modRegistry')) {
$modx->registry->addRegister('remotes', 'registry.modDbRegister', array('directory' => 'remotes'));
$modx->registry->remotes->connect();
$modx->registry->remotes->subscribe('/distrib/instances/');
$instances = $modx->registry->remotes->read(array('poll_limit' => 1, 'msg_limit' => 200, 'remove_read' => false));
$modx->registry->remotes->unsubscribe('/distrib/instances/');
if (!empty($instances)) {
$staggerCurrent = 0;
foreach ($instances as $instance) {
if ($instance == $_SERVER['SERVER_ADDR']) continue;
$modx->registry->remotes->subscribe("/distrib/commands/{$instance}/");
$modx->registry->remotes->send("/distrib/commands/{$instance}/", array('clearCache'), array('ttl' => $seconds, 'delay' => $delay + $staggerCurrent));
$modx->registry->remotes->unsubscribe("/distrib/commands/{$instance}/");
$staggerCurrent = $staggerCurrent + $stagger;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment