Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
ENH: refs #917. Schedule temp/unofficial scalars for deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmullen committed Feb 18, 2013
1 parent 0b6f18a commit a032578
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
14 changes: 13 additions & 1 deletion modules/tracker/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Tracker_Notification extends ApiEnabled_Notification
{
public $moduleName = 'tracker';
public $_models = array('User');
public $_moduleModels = array('Trend');
public $_moduleModels = array('Scalar', 'Trend');
public $_moduleComponents = array('Api');

/** init notification process*/
Expand All @@ -33,6 +33,7 @@ public function init()
$this->addCallBack('CALLBACK_CORE_USER_DELETED', 'userDeleted');

$this->addTask('TASK_TRACKER_SEND_THRESHOLD_NOTIFICATION', 'sendEmail', 'Send threshold violation email');
$this->addTask('TASK_TRACKER_DELETE_TEMP_SCALAR', 'deleteTempScalar', 'Delete an unofficial/temporary scalar value');
$this->enableWebAPI($this->moduleName);
}//end init

Expand Down Expand Up @@ -72,6 +73,17 @@ public function userDeleted($args)
$user = $args['userDao'];
}

/**
* Delete temporary (unofficial) scalars after n hours, where n is specified as
* a module configuration option
*/
public function deleteTempScalar($params)
{
$scalarId = $params['scalarId'];
$scalar = $this->Tracker_Scalar->load($scalarId);
$this->Tracker_Scalar->delete($scalar);
}

/**
* Send an email to the user that a threshold was crossed
*/
Expand Down
49 changes: 49 additions & 0 deletions modules/tracker/controllers/components/ApiComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,29 @@ public function scalarAdd($args)
$notifyComponent = MidasLoader::loadComponent('ThresholdNotification', 'tracker');
$notifyComponent->scheduleNotifications($scalar, $notifications);
}
if(!$official)
{
$jobModel = MidasLoader::loadModel('Job', 'scheduler');
$settingModel = MidasLoader::loadModel('Setting');
$nHours = $settingModel->getValueByName('tempScalarTtl', 'tracker');
if(!$nHours)
{
$nHours = 24; //default to 24 hours
}
foreach($notifications as $notification)
{
$job = MidasLoader::newDao('JobDao', 'scheduler');
$job->setTask('TASK_TRACKER_DELETE_TEMP_SCALAR');
$job->setPriority(1);
$job->setRunOnlyOnce(1);
$job->setFireTime(date('Y-m-j G:i:s', strtotime('+'.$nHours.' hours')));
$job->setTimeInterval(0);
$job->setStatus(SCHEDULER_JOB_STATUS_TORUN);
$job->setCreatorId($user->getKey());
$job->setParams(JsonComponent::encode(array('scalarId' => $scalar->getKey())));
$jobModel->save($job);
}
}
return $scalar;
}

Expand All @@ -229,6 +252,16 @@ public function resultsUploadJson($args)
$user = $this->_getUser($args);

$official = !array_key_exists('unofficial', $args);
if(!$official)
{
$jobModel = MidasLoader::loadModel('Job', 'scheduler');
$settingModel = MidasLoader::loadModel('Setting');
$nHours = $settingModel->getValueByName('tempScalarTtl', 'tracker');
if(!$nHours)
{
$nHours = 24; //default to 24 hours
}
}

// Unofficial submissions only require read access to the community
$community = $communityModel->load($args['communityId']);
Expand Down Expand Up @@ -362,6 +395,22 @@ public function resultsUploadJson($args)
$notifyComponent = MidasLoader::loadComponent('ThresholdNotification', 'tracker');
$notifyComponent->scheduleNotifications($scalar, $notifications);
}
if(!$official)
{
foreach($notifications as $notification)
{
$job = MidasLoader::newDao('JobDao', 'scheduler');
$job->setTask('TASK_TRACKER_DELETE_TEMP_SCALAR');
$job->setPriority(1);
$job->setRunOnlyOnce(1);
$job->setFireTime(date('Y-m-j G:i:s', strtotime('+'.$nHours.' hours')));
$job->setTimeInterval(0);
$job->setStatus(SCHEDULER_JOB_STATUS_TORUN);
$job->setCreatorId($user->getKey());
$job->setParams(JsonComponent::encode(array('scalarId' => $scalar->getKey())));
$jobModel->save($job);
}
}
}
}
}
Expand Down

0 comments on commit a032578

Please sign in to comment.