Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove data files for deleted export requests #1030

Merged
merged 1 commit into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion classes/DataWarehouse/Export/BatchProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ public function setDryRun($dryRun)
*/
public function processRequests()
{
$this->processSubmittedRequests();
// Delete removed and expiring files before creating new files.
$this->processDeletedRequests();
$this->processExpiringRequests();
$this->processSubmittedRequests();
}

/**
Expand Down Expand Up @@ -213,6 +215,25 @@ private function processExpiringRequest(array $request)
}
}

/**
* Process requests that have been deleted.
*
* If a request has been deleted then the associated data file needs to be
* removed from the file system.
*/
private function processDeletedRequests()
{
$this->logger->info('Processing deleted requests');
$this->fileManager->removeDeletedRequests(
array_map(
function ($request) {
return $request['id'];
},
$this->queryHandler->listAvailableRecords()
)
);
}

/**
* Get the data set for the given request.
*
Expand Down
29 changes: 29 additions & 0 deletions classes/DataWarehouse/Export/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,33 @@ public function removeExportFile($id)
throw new Exception(sprintf('Failed to delete "%s"', $zipFile));
}
}

/**
* Remove all data files corresponding to deleted requests.
*
* @param array $availableRequestIds Request IDs for "Available" export
* files. These correspond to data files that should not be deleted.
*/
public function removeDeletedRequests(array $availableRequestIds)
{
$availableFiles = array_map(
[$this, 'getExportDataFilePath'],
$availableRequestIds
);

foreach (glob($this->exportDir . '/*.zip') as $exportFile) {
if (!in_array($exportFile, $availableFiles)) {
$this->logger->info([
'message' => 'Removing export file',
'zip_file' => $exportFile
]);
if (!unlink($exportFile)) {
throw new Exception(sprintf(
'Failed to delete "%s"',
$exportFile
));
}
}
}
}
}
12 changes: 12 additions & 0 deletions classes/DataWarehouse/Export/QueryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ public function listSubmittedRecords()
return $this->dbh->query($sql);
}

/**
* Return details of all export requests presently in Available state.
*
* @return array
*/
public function listAvailableRecords()
{
$sql = 'SELECT id, user_id, realm, start_date, end_date, export_file_format, requested_datetime
FROM batch_export_requests ' . $this->whereAvailable . ' ORDER BY requested_datetime, id';
return $this->dbh->query($sql);
}

/**
* Return export requests in Available state that should expire.
*
Expand Down