Commit 3dba3f0c authored by Nigel McNie's avatar Nigel McNie
Browse files

Add a cronjob to recalculate users' used quota.

This is quite an expensive operation. I guess one day large installs will have problems with it, but it can probably be re-implemented in a more performant manner when needed.

Conflicts:

	htdocs/lib/version.php
parent 527316d4
......@@ -728,6 +728,10 @@ class ArtefactTypeFile extends ArtefactTypeFileBase {
}
return false;
}
public static function get_quota_usage($artefact) {
return get_field('artefact_file_files', 'size', 'artefact', $artefact);
}
}
class ArtefactTypeFolder extends ArtefactTypeFileBase {
......
......@@ -552,6 +552,11 @@ class ArtefactTypeProfileIcon extends ArtefactTypeProfileField {
public function in_view_list() {
return true;
}
public static function get_quota_usage($artefact) {
return filesize(get_config('dataroot') . 'artefact/internal/profileicons/originals/'
. ($artefact % 256) . '/' . $artefact);
}
}
......
......@@ -245,6 +245,18 @@ abstract class ArtefactType {
return get_record('artefact','id',$this->parent);
}
/**
* Returns how much quota this artefact has used.
*
* It should try to not instantiate the artefact, because it is normally
* called as part of an expensive cron job
*
* @return int Size in bytes that the artefact is taking up in quota
*/
public static function get_quota_usage($artefact) {
return 0;
}
public function get($field) {
if (!property_exists($this, $field)) {
throw new InvalidArgumentException("Field $field wasn't found in class " . get_class($this));
......
......@@ -978,6 +978,18 @@ function xmldb_core_upgrade($oldversion=0) {
insert_record('cron', $cron);
}
if ($oldversion < 2008040203) {
// Install a cron job to recalculate user quotas
$cron = new StdClass;
$cron->callfunction = 'recalculate_quota';
$cron->minute = '15';
$cron->hour = '2';
$cron->day = '*';
$cron->month = '*';
$cron->dayofweek = '*';
insert_record('cron', $cron);
}
return $status;
}
......
......@@ -1818,4 +1818,38 @@ function profile_sideblock() {
return $data;
}
/**
* Cronjob to recalculate how much quota each user is using and update it as
* appropriate.
*
* This gives a backstop for the possibility that there is a bug elsewhere that
* has caused the quota count to get out of sync
*/
function recalculate_quota() {
if (!$artefacts = get_records_array('artefact', '', '', '', 'id, artefacttype, owner')) {
// Nothing to do
return;
}
$userquotas = array();
foreach ($artefacts as $artefact) {
safe_require('artefact', get_field('artefact_installed_type', 'plugin', 'name', $artefact->artefacttype));
if (!isset($userquotas[$artefact->owner])) {
$userquotas[$artefact->owner] = 0;
}
$userquotas[$artefact->owner] += call_static_method(generate_artefact_class_name($artefact->artefacttype), 'get_quota_usage', $artefact->id);
}
foreach ($userquotas as $user => $quota) {
$data = (object) array(
'quota' => $quota
);
$where = (object) array(
'id' => $user
);
update_record('usr', $data, $where);
}
}
?>
......@@ -689,6 +689,7 @@ function core_install_firstcoredata_defaults() {
'auth_handle_account_expiries' => array('5', '10', '*', '*', '*'),
'activity_process_queue' => array('*/5', '*', '*', '*', '*'),
'auth_remove_old_session_files' => array('30', '20', '*', '*', '*'),
'recalculate_quota' => array('15', '2', '*', '*', '*'),
);
foreach ($cronjobs as $callfunction => $times) {
$cron = new StdClass;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment