Debugging Cron

I guess most Drupal site builders have had issues with cron being stuck from time to time. On a recent project I again had this issue and it seemed to be really relunctant to go away by just deleting some cron_last entries.
So I dug deeper and found the Cron Debug module. Cron Debug allows you to run each cron hook individually and track the time needed to execute each hook. This helped me to quickly identify the search index hook being the cause of my troubles, as it always caused an error 500.

So it seemed pretty obvious that some or several bad nodes were causing the problems. Thanks to this blog post on tappetyclick I found the following snippet which simply writes a watchdog entry for every node being indexed. So when cron gets stuck, the last node mentioned in your log is highly probable to be some kind of problematic. For me it was a node that had some base64-encoded images inside a textarea. Probably some crazy CKeditor feature that tries to enable copy & paste for images, but that’s a different story.

Anyways, here’s the snippet for modules/node/node.module:
Notice: you just have to add the line watchdog ('cron', "indexing node {$node -> nid}"); at the right place.
Don’t forget to revert your changes when you’re done identifying your evil nodes!

 * Implements hook_update_index().
function node_update_index() {
  $limit = (int)variable_get('search_cron_limit', 100);

  $result = db_query_range("SELECT n.nid FROM {node} n LEFT JOIN {search_dataset} d ON d.type = 'node' AND d.sid = n.nid WHERE d.sid IS NULL OR d.reindex <> 0 ORDER BY d.reindex ASC, n.nid ASC", 0, $limit, array(), array('target' => 'slave'));

  foreach ($result as $node) {
    watchdog ('cron', "indexing node {$node -> nid}"); // ADD THIS LINE

Leave a Reply