Skip to content

Commit

Permalink
refs #41959, clear batch data to prevent memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
jimyhuang committed Nov 6, 2024
1 parent 947c393 commit 7a72e5b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions CRM/Batch/BAO/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,23 @@ public static function runQueue() {
$message = ts('Success processing queuing batch.');
}
}
if (date('G') >= 3 && date('G') <= 6) {
self::expireBatch();
}
return $message;
}

/**
* Run last queuing batching
* Expire batches over expire days
* This will delete data column and purge download file to prevent db growing
*
* @return string
* message that indicate current running status
* message that indicate expires
*/
public static function expireBatch() {
$type = self::batchType();
$status = self::batchStatus();
unset($status['Running']);
unset($status['Pending']);
$purgeDay = self::EXPIRE_DAY*4;
$sql = "SELECT id FROM civicrm_batch WHERE type_id = %1 AND status_id IN (".CRM_Utils_Array::implode(',', $status).") AND DATE_ADD(modified_date, INTERVAL ".$purgeDay." DAY) < NOW() AND modified_date IS NOT NULL ORDER BY modified_date ASC";
$sql = "SELECT id FROM civicrm_batch WHERE type_id = %1 AND status_id IN (".CRM_Utils_Array::implode(',', $status).") AND DATE_ADD(modified_date, INTERVAL ".$purgeDay." DAY) < NOW() AND modified_date IS NOT NULL AND data IS NOT NULL ORDER BY modified_date ASC";
$dao = CRM_Core_DAO::executeQuery($sql, array(
1 => array($type['Auto'], 'Integer'),
));
Expand All @@ -204,9 +202,11 @@ public static function expireBatch() {
@unlink($batch->data['download']['file']);
$expires[] = $dao->id;
}
CRM_Core_DAO::executeQuery("UPDATE civicrm_batch SET data = NULL WHERE id = %1", array(1 => array($dao->id, 'Integer')));
}
// refs #41959, free memory of batch result to prevent memory leak
$batch->free();
unset($batch);
}
if (count($expires)) {
$msg = 'Batch ids in '.CRM_Utils_Array::implode(",", $expires).' has been expires';
Expand Down

0 comments on commit 7a72e5b

Please sign in to comment.