1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-09 16:32:39 +01:00

Allow files to TTL and and be garbage collected

Summary: Added ttl field to files. Gabage collect files with expired ttl

Test Plan: created file with a ttl. Let garbage collector run

Reviewers: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D4987
This commit is contained in:
kwadwo 2013-02-20 13:33:47 -08:00 committed by epriestley
parent 204d6481e4
commit 762ace810d
9 changed files with 70 additions and 5 deletions

View file

@ -0,0 +1,3 @@
ALTER TABLE {$NAMESPACE}_file.file
ADD ttl INT(10) UNSIGNED DEFAULT NULL,
ADD KEY key_ttl (ttl);

View file

@ -779,6 +779,7 @@ final class DiffusionBrowseFileController extends DiffusionController {
$data, $data,
array( array(
'name' => basename($path), 'name' => basename($path),
'ttl' => time() + 60 * 60 * 24,
)); ));
} }

View file

@ -11,6 +11,7 @@ final class PhabricatorImageTransformer {
$image, $image,
array( array(
'name' => 'meme-'.$file->getName(), 'name' => 'meme-'.$file->getName(),
'ttl' => time() + 60 * 60 * 24,
)); ));
} }

View file

@ -35,6 +35,14 @@ final class PhabricatorFileInfoController extends PhabricatorFileController {
->setObjectName('F'.$file->getID()) ->setObjectName('F'.$file->getID())
->setHeader($file->getName()); ->setHeader($file->getName());
$ttl = $file->getTTL();
if ($ttl !== null) {
$ttl_tag = id(new PhabricatorTagView())
->setType(PhabricatorTagView::TYPE_OBJECT)
->setName(pht("Temporary"));
$header->addTag($ttl_tag);
}
$actions = $this->buildActionView($file); $actions = $this->buildActionView($file);
$properties = $this->buildPropertyView($file); $properties = $this->buildPropertyView($file);

View file

@ -89,12 +89,10 @@ final class PhabricatorFileListController extends PhabricatorFileController {
$id = $file->getID(); $id = $file->getID();
$phid = $file->getPHID(); $phid = $file->getPHID();
$name = $file->getName(); $name = $file->getName();
$file_name = "F{$id} {$name}"; $file_name = "F{$id} {$name}";
$file_uri = $this->getApplicationURI("/info/{$phid}/"); $file_uri = $this->getApplicationURI("/info/{$phid}/");
$date_created = phabricator_date($file->getDateCreated(), $user); $date_created = phabricator_date($file->getDateCreated(), $user);
$author_phid = $file->getAuthorPHID(); $author_phid = $file->getAuthorPHID();
if ($author_phid) { if ($author_phid) {
$author_link = $this->getHandle($author_phid)->renderLink(); $author_link = $this->getHandle($author_phid)->renderLink();
@ -110,6 +108,11 @@ final class PhabricatorFileListController extends PhabricatorFileController {
->addAttribute($uploaded) ->addAttribute($uploaded)
->addIcon('none', phabricator_format_bytes($file->getByteSize())); ->addIcon('none', phabricator_format_bytes($file->getByteSize()));
$ttl = $file->getTTL();
if ($ttl !== null) {
$item->addIcon('blame', pht('Temporary'));
}
if (isset($highlighted_ids[$id])) { if (isset($highlighted_ids[$id])) {
$item->setEffect('highlighted'); $item->setEffect('highlighted');
} }

View file

@ -21,6 +21,8 @@ final class PhabricatorFile extends PhabricatorFileDAO
protected $storageFormat; protected $storageFormat;
protected $storageHandle; protected $storageHandle;
protected $ttl;
public function getConfiguration() { public function getConfiguration() {
return array( return array(
self::CONFIG_AUX_PHID => true, self::CONFIG_AUX_PHID => true,
@ -148,6 +150,7 @@ final class PhabricatorFile extends PhabricatorFileDAO
$file_name = idx($params, 'name'); $file_name = idx($params, 'name');
$file_name = self::normalizeFileName($file_name); $file_name = self::normalizeFileName($file_name);
$file_ttl = idx($params, 'ttl');
$authorPHID = idx($params, 'authorPHID'); $authorPHID = idx($params, 'authorPHID');
$new_file = new PhabricatorFile(); $new_file = new PhabricatorFile();
@ -155,6 +158,7 @@ final class PhabricatorFile extends PhabricatorFileDAO
$new_file->setName($file_name); $new_file->setName($file_name);
$new_file->setByteSize($copy_of_byteSize); $new_file->setByteSize($copy_of_byteSize);
$new_file->setAuthorPHID($authorPHID); $new_file->setAuthorPHID($authorPHID);
$new_file->setTtl($file_ttl);
$new_file->setContentHash($hash); $new_file->setContentHash($hash);
$new_file->setStorageEngine($copy_of_storage_engine); $new_file->setStorageEngine($copy_of_storage_engine);
@ -223,6 +227,7 @@ final class PhabricatorFile extends PhabricatorFileDAO
$file_name = idx($params, 'name'); $file_name = idx($params, 'name');
$file_name = self::normalizeFileName($file_name); $file_name = self::normalizeFileName($file_name);
$file_ttl = idx($params, 'ttl');
// If for whatever reason, authorPHID isn't passed as a param // If for whatever reason, authorPHID isn't passed as a param
// (always the case with newFromFileDownload()), store a '' // (always the case with newFromFileDownload()), store a ''
@ -231,6 +236,7 @@ final class PhabricatorFile extends PhabricatorFileDAO
$file->setName($file_name); $file->setName($file_name);
$file->setByteSize(strlen($data)); $file->setByteSize(strlen($data));
$file->setAuthorPHID($authorPHID); $file->setAuthorPHID($authorPHID);
$file->setTtl($file_ttl);
$file->setContentHash(self::hashFileContent($data)); $file->setContentHash(self::hashFileContent($data));
$file->setStorageEngine($engine_identifier); $file->setStorageEngine($engine_identifier);
@ -354,6 +360,17 @@ final class PhabricatorFile extends PhabricatorFileDAO
} }
public function delete() { public function delete() {
// delete all records of this file in transformedfile
$trans_files = id(new PhabricatorTransformedFile())->loadAllWhere(
'TransformedPHID = %s', $this->getPHID());
$this->openTransaction();
foreach ($trans_files as $trans_file) {
$trans_file->delete();
}
$ret = parent::delete();
$this->saveTransaction();
// Check to see if other files are using storage // Check to see if other files are using storage
$other_file = id(new PhabricatorFile())->loadAllWhere( $other_file = id(new PhabricatorFile())->loadAllWhere(
'storageEngine = %s AND storageHandle = %s AND 'storageEngine = %s AND storageHandle = %s AND
@ -366,9 +383,6 @@ final class PhabricatorFile extends PhabricatorFileDAO
$engine = $this->instantiateStorageEngine(); $engine = $this->instantiateStorageEngine();
$engine->deleteFile($this->getStorageHandle()); $engine->deleteFile($this->getStorageHandle());
} }
$ret = parent::delete();
return $ret; return $ret;
} }

View file

@ -126,4 +126,23 @@ final class PhabricatorFileTestCase extends PhabricatorTestCase {
$this->assertEqual($data, $second_file->loadFileData()); $this->assertEqual($data, $second_file->loadFileData());
} }
public function testReadWriteTtlFiles() {
$engine = new PhabricatorTestStorageEngine();
$data = Filesystem::readRandomCharacters(64);
$ttl = (time() + 60 * 60 * 24);
$params = array(
'name' => 'test.dat',
'ttl' => ($ttl),
'storageEngines' => array(
$engine,
),
);
$file = PhabricatorFile::newFromFileData($data, $params);
$this->assertEqual($ttl, $file->getTTL());
}
} }

View file

@ -17,6 +17,7 @@ final class PhabricatorGarbageCollectorDaemon extends PhabricatorDaemon {
$n_tasks = $this->collectArchivedTasks(); $n_tasks = $this->collectArchivedTasks();
$n_cache_ttl = $this->collectGeneralCacheTTL(); $n_cache_ttl = $this->collectGeneralCacheTTL();
$n_cache = $this->collectGeneralCaches(); $n_cache = $this->collectGeneralCaches();
$n_files = $this->collectExpiredFiles();
$collected = array( $collected = array(
'Herald Transcript' => $n_herald, 'Herald Transcript' => $n_herald,
@ -26,6 +27,7 @@ final class PhabricatorGarbageCollectorDaemon extends PhabricatorDaemon {
'Archived Tasks' => $n_tasks, 'Archived Tasks' => $n_tasks,
'General Cache TTL' => $n_cache_ttl, 'General Cache TTL' => $n_cache_ttl,
'General Cache Entries' => $n_cache, 'General Cache Entries' => $n_cache,
'Temporary Files' => $n_files,
); );
$collected = array_filter($collected); $collected = array_filter($collected);
@ -206,5 +208,15 @@ final class PhabricatorGarbageCollectorDaemon extends PhabricatorDaemon {
return $conn_w->getAffectedRows(); return $conn_w->getAffectedRows();
} }
private function collectExpiredFiles() {
$files = id(new PhabricatorFile())->loadAllWhere('ttl < %d LIMIT 100',
time());
foreach ($files as $file) {
$file->delete();
}
return count($files);
}
} }

View file

@ -1137,6 +1137,10 @@ final class PhabricatorBuiltinPatchList extends PhabricatorSQLPatchList {
'type' => 'sql', 'type' => 'sql',
'name' => $this->getPatchPath('20130218.longdaemon.sql'), 'name' => $this->getPatchPath('20130218.longdaemon.sql'),
), ),
'20130215.phabricatorfileaddttl.sql' => array(
'type' => 'sql',
'name' => $this->getPatchPath('20130215.phabricatorfileaddttl.sql'),
),
); );
} }