testing scheduled file deletions
-
01-03-2021 - |
Question
Long story short, is there a correct way to override DRUPAL_MAXIMUM_TEMP_FILE_AGE
?
I'm working on testing a fix to a module I wrote a while back that handles some uploaded files. I neglected to call file_usage_delete
and set file_managed
status to 0. To test my changes more quickly, I'd like to schedule files for deletion after doing some operations on them. To do this reasonably I had to reduce the turn around time on deletions to something on the order of minutes than hours. I went ahead and edited system.module where DRUPAL_MAXIMUM_TEMP_FILE_AGE
is defined as 6 hours, but the comments immediately preceding the defines indicate:
@file Configuration system that lets administrators modify the workings of the site.
I checked the places where this value is used, and it doesn't look to be actually configurable besides editing system.module directly. Maybe I'm missing something?
Is there a correct way to set this that doesn't involve "hacking core"?
Solution
It isn't configurable. You would be better served writing your own routine that executes on a regular interval and searches for temporary files whose creation age is greater than 10 minutes ago.
The DRUPAL_MAXIMUM_TEMP_FILE_AGE constant is specifically intended to inform Drupal on when a file that has not yet been set to permanent should be removed.
If you take a look at system.module, the relevant code is contained in a hook_cron() implementation. The code reads:
$result = db_query('SELECT fid FROM {file_managed} WHERE status <> :permanent AND timestamp < :timestamp', array(
':permanent' => FILE_STATUS_PERMANENT,
':timestamp' => REQUEST_TIME - DRUPAL_MAXIMUM_TEMP_FILE_AGE,
));
My recommendation is to simply implement your own hook_cron()
and use the same code, but calculate for files older than 10 minutes.
You will also need for cron to execute on a 10 minute interval. For this, you'll need to add a crontab and install either ultimate_cron or elysisia_cron. I prefer ultimate_cron.
Please see my answer to this question for more information on setting up crontab on a 60 second interval for maximum timeliness. You'll then use elysia_cron or ultimate_cron to control the actual job execution interval.