I'm using cloud based continuous integration to deploy my theme to my drupal site, and I would like to be able to automate the clearing of the cache after the theme files are copied in place.
Is there a way of doing this remotely?
I'm thinking it would be great if there was a rest api to do things like this, so I could do for example:
curl http://mysite.example.com/admin-api/clear-cache?key=<secret>
This would be done remotely from a script. I'm thinking of this as a complement to drush, a kind of "remote drush" for the cases where you don't have ssh access to the server but you still want to automate things.
Is there a module for this? Or some other strategy?
You could write a small module that hooks into cron:
// function that will be triggered on next cron run
function <MODULE>_cron() {
// clear all caches
drupal_flush_all_caches();
}
Then you could simply run cron:
curl http://mysite.example.com/cron.php?cron_key=<YOUR_CRON_KEY>
Which in turn will trigger <MODULE>_cron()
that will clear the cache.
Another approach would be to create a small module that adds a callback through hook_menu()
:
function <MODULE>_menu() {
$menu['cache/clear/%'] = array(
'page callback' => '<MODULE>_clear_cache',
'access arguments' => array('access content'),
'page arguments' => array(2)
);
return $menu;
}
function <MODULE>_clear_cache($cache) {
// check so we have a valid access_token
$access_token = variable_get('cache_access_token');
$token = filter_input(INPUT_GET, 'access_token');
if($token == $access_token) {
switch($cache) {
case 'all':
drupal_flush_all_caches();
break;
case 'theme':
cache_clear_all('theme_registry', 'cache', TRUE);
break;
}
}
}
function <MODULE>_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'system_site_information_settings') {
// add a cache access token field under site information
$form['cache_access_token'] = array(
'#type' => 'textfield',
'#title' => t('Access Token'),
'#description' => t('Token used to access clear cache remotely'),
'#default_value' => variable_get('cache_access_token')
);
}
}
Enable the module and you should be able to clear you cache by running:
curl http://mysite.example.com/cache/clear/all?access_token=<YOUR_ACCESS_TOKEN>
if you only would like to clear the theme cache you would do like this:
curl http://mysite.example.com/cache/clear/theme?access_token=<YOUR_ACCESS_TOKEN>
There is also a module called Clear Cache Remotely
that I think does exactly what you want.