Not sure if others have this issue, but thought I'd paste here to help. We had 2.5 million of these sitting in the database.
What causes this is if the node which owns the token is removed, then the cron for the token module for some reason creates a new token for it, forever and ever. So we had literally millions of unexpired tokens in the database going back years. Not something I want to spend too much time on, but I've quickly modified the cron function to the below. I've set the limit to 100 so as not to kill the cron.
/**
* Implements hook_cron().
*/
function review_token_cron() {
$n_expired = 0;
$removed_expired = [];
$expired = db_select('node_preview_tokens', 't')
->fields('t')
->where('expires < UNIX_TIMESTAMP(NOW())')
->range(0, 100)
->execute()->fetchAll();
foreach ($expired as $e) {
$node = node_load($e->nid);
if ($node) {
$token = new PreviewToken($node);
$token->expireToken();
}
else {
$removed_expired[] = $e->token;
}
$n_expired++;
}
if (count($removed_expired) > 0) {
$deleted = db_delete('node_preview_tokens')
->condition('token', $removed_expired, 'IN')
->where('expires < UNIX_TIMESTAMP(NOW())')
->execute();
watchdog('Content Publish Review', 'Cron: %n_expired %pl_tokens orphan expired.',
array(
'%n_expired' => $deleted,
'%pl_tokens' => format_plural($deleted, 'token', 'tokens'),
),
WATCHDOG_NOTICE
);
}
if ($n_expired) {
watchdog('Content Publish Review', 'Cron: %n_expired %pl_tokens expired.',
array(
'%n_expired' => $n_expired,
'%pl_tokens' => format_plural($n_expired, 'token', 'tokens'),
),
WATCHDOG_NOTICE
);
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 2886342-token-node-disappear-4.patch | 3.22 KB | dpi |
Comments
Comment #2
camerongreen commentedComment #3
camerongreen commentedComment #4
dpiSlightly different route:
PreviewToken::cleanUpwhich searches for tokens with non existent nodes. This will automatically do a bulk cleanup of all the bogus nid=0 tokens accumulated.Bumping to major since this is creating an extreme amount of tokens. 2.5million in the case of the original report, 500k in my case.
Comment #5
larowlanLooks good to me
Comment #6
camerongreen commentedGiven this an other issues with this module we are probably just going to replace it with something like https://www.drupal.org/project/tca
Comment #8
jibranThanks, committed and pushed to 7.x-1.x. I have also created a new release.
Comment #9
dpiThankyou Jibran-san