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

Commit

Permalink
Cron to delete hidden mission db events after x days
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcroox committed May 1, 2018
1 parent cfa3d2e commit 71e0c15
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dist/config.template.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
*/
define('HOURS_EXPIRE_EVENT_CACHE_FILE', 5);

/*
To reduce the growing size of your event database, you can setup a cron job to run
/crons/delete-hidden-events.php. The value below controls how many days old a hidden replay
must be before deleting it's events
*/
define('MIN_DAYS_DELETE_HIDDEN_MISSION_EVENTS', 60);

// Set this to FALSE if you are deploying
// to production server
define('DEBUG', TRUE);
30 changes: 30 additions & 0 deletions dist/crons/delete-hidden-events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
ini_set('max_execution_time', 900);

// Delete events from missions that have been "hidden". Missions are hidden because
// not enough players are present or they finish too soon (usually applies to mission testing)
if (!isset($_SERVER['cron']))
die('No browser access.');

require_once('../inc/bootstrap.php');
$replays = Replays::Instance();
$db = Database::Instance()->conn;

// Sometimes missions can be incorrectly hidden. Let's not delete anything too recent.
$daysOlderThan = MIN_DAYS_DELETE_HIDDEN_MISSION_EVENTS;
$deletedReplays = [];

$hiddenMissionIds = $replays->fetchHidden($daysOlderThan);

foreach ($hiddenMissionIds as $mission) {
if ($mission->id) {
$query = $db->prepare("DELETE FROM events WHERE replayId = :id");

$query->execute(array('id' => $mission->id));

$deletedReplays[] = $mission->id;
}
}

echo '<pre>';
print_r($deletedReplays);
14 changes: 14 additions & 0 deletions dist/inc/classes/Replays.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ public function fetchOne($replayId, $forceHidden = FALSE) {
return $query->fetch();
}

public function fetchHidden($daysOlderThan = 60) {

$query = $this->_db->prepare("
SELECT id FROM replays
WHERE
hidden = 1 AND
dateStarted < DATE_SUB(NOW(), INTERVAL 60 DAY)
");

$query->execute();

return $query->fetch();
}

public function updateMeta() {

$util = Util::Instance();
Expand Down

0 comments on commit 71e0c15

Please sign in to comment.