Skip to content

Commit 955b1bc

Browse files
alfredangclaude
andcommitted
feat(reindex): token-secured HTTP reindex API endpoint
GET /reindex/api/run?token=<token>[&flush=1] runs a full reindex (and optional cache flush) without shell access — fixes a stale catalog_category_flat (landing pages stuck in product-list mode) or a stale merged CSS/JS bundle after a deploy. Reuses MMD_Reindex Cron::run() so results land in the Reindex Logs + Scheduler; the existing daily cron still handles scheduled runs. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 8c547ee commit 955b1bc

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Token-secured HTTP reindex endpoint — lets ops trigger a full reindex (and
4+
* optional cache flush) on any environment without shell access. Reuses the
5+
* same Cron::run() the scheduled job uses, so results are logged to
6+
* mmd_reindex_log and the Reindex Scheduler page.
7+
*
8+
* GET /reindex/api/run?token=<token> full reindex
9+
* GET /reindex/api/run?token=<token>&flush=1 flush cache first (fixes a
10+
* stale merged CSS/JS bundle)
11+
*
12+
* Token is mmd_reindex/api/token (default in config.xml; override in
13+
* core_config_data for production).
14+
*/
15+
class MMD_Reindex_ApiController extends Mage_Core_Controller_Front_Action
16+
{
17+
public function runAction()
18+
{
19+
$res = $this->getResponse();
20+
$res->setHeader('Content-Type', 'application/json', true);
21+
$res->setHeader('Cache-Control', 'no-store', true);
22+
23+
$token = (string) $this->getRequest()->getParam('token');
24+
$expected = (string) Mage::getStoreConfig('mmd_reindex/api/token');
25+
if ($expected === '' || !hash_equals($expected, $token)) {
26+
$res->setHttpResponseCode(403)->setBody(json_encode(array('ok' => false, 'error' => 'forbidden')));
27+
return;
28+
}
29+
30+
$flush = (bool) $this->getRequest()->getParam('flush');
31+
try {
32+
if ($flush) {
33+
Mage::app()->cleanCache();
34+
Mage::app()->getCacheInstance()->flush();
35+
}
36+
$results = Mage::getModel('mmd_reindex/cron')->run('api');
37+
$failed = array_filter($results, function ($v) { return strpos((string) $v, 'fail') === 0; });
38+
$res->setBody(json_encode(array(
39+
'ok' => empty($failed),
40+
'flushed' => $flush,
41+
'reindexed' => count($results),
42+
'failed' => count($failed),
43+
'results' => $results,
44+
), JSON_PRETTY_PRINT));
45+
} catch (Exception $e) {
46+
Mage::logException($e);
47+
$res->setHttpResponseCode(500)->setBody(json_encode(array('ok' => false, 'error' => $e->getMessage())));
48+
}
49+
}
50+
}

app/code/local/MMD/Reindex/etc/config.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@
4242
</adminhtml>
4343
</routers>
4444
</admin>
45+
<frontend>
46+
<routers>
47+
<mmd_reindex>
48+
<use>standard</use>
49+
<args>
50+
<module>MMD_Reindex</module>
51+
<frontName>reindex</frontName>
52+
</args>
53+
</mmd_reindex>
54+
</routers>
55+
</frontend>
4556
<crontab>
4657
<jobs>
4758
<mmd_reindex_scheduled>
@@ -52,5 +63,10 @@
5263
</crontab>
5364
<default>
5465
<crontab><mmd_reindex><schedule>0 3 * * *</schedule></mmd_reindex></crontab>
66+
<mmd_reindex>
67+
<api>
68+
<token>5e9b1d7c3a4f86e20b9c7d1a4f8e6c3b</token>
69+
</api>
70+
</mmd_reindex>
5571
</default>
5672
</config>

0 commit comments

Comments
 (0)