Skip to content

Commit

Permalink
SwiftDump: use more reliable storage check (#538)
Browse files Browse the repository at this point in the history
We also use exit code 75 to allow us to do custom handling for it, so
we can customize the log message if the dump fails due to lack of
storage. miraheze/DataDump#92 makes that possible.
  • Loading branch information
Universal-Omega authored Nov 21, 2024
1 parent cab2661 commit a58cfab
Showing 1 changed file with 53 additions and 11 deletions.
64 changes: 53 additions & 11 deletions maintenance/swiftDump.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,47 @@ public function __construct() {
public function execute() {
global $wmgSwiftPassword;

$limits = [ 'memory' => 0, 'filesize' => 0, 'time' => 0, 'walltime' => 0 ];

$wiki = $this->getConfig()->get( MainConfigNames::DBname );
$this->output( "Starting swift dump for $wiki...\n" );

// Available disk space must be 10GB
$df = disk_free_space( '/tmp' );
if ( $df < 10 * 1024 * 1024 * 1024 ) {
$this->fatalError( "Not enough disk space available ( < 10GB). Aborting dump...\n" );
}
// If no wiki then errror
// If no wiki then error
if ( !$wiki ) {
$this->fatalError( 'No wiki has been defined' );
}

$this->output( "Starting swift dump for $wiki...\n" );

$container = "miraheze-$wiki-local-public";
$limits = [
'memory' => 0,
'filesize' => 0,
'time' => 0,
'walltime' => 0,
];

// Calculate the required disk space (container size + 10GB)
$containerSize = $this->getContainerSize( $container, $limits );

// 5GB in bytes
$additionalSpace = 5 * 1024 * 1024 * 1024;
$requiredSpace = $containerSize + $additionalSpace;
$availableSpace = (int)disk_free_space( '/tmp' );

if ( $availableSpace < $requiredSpace ) {
$contentLanguage = $this->getServiceContainer()->getContentLanguage();
$formattedRequiredSpace = $contentLanguage->formatSize( $requiredSpace );
$formattedAvailableSpace = $contentLanguage->formatSize( $availableSpace );

// We use exit code 75 to allow for custom handling
$this->fatalError( sprintf(
"Not enough disk space available (required: %s, available: %s). Aborting dump...\n",
$formattedRequiredSpace,
$formattedAvailableSpace
), 75 );
}

// Download the Swift container
Shell::command(
'swift', 'download',
"miraheze-$wiki-local-public",
'swift', 'download', $container,
'-A', 'https://swift-lb.wikitide.net/auth/v1.0',
'-U', 'mw:media',
'-K', $wmgSwiftPassword,
Expand All @@ -64,6 +86,26 @@ public function execute() {

$this->output( "Swift dump for $wiki complete!\n" );
}

private function getContainerSize(
string $container,
array $limits
): int {
global $wmgSwiftPassword;

$output = Shell::command(
'swift', 'stat', $container,
'-A', 'https://swift-lb.wikitide.net/auth/v1.0',
'-U', 'mw:media',
'-K', $wmgSwiftPassword
)->limits( $limits )
->disableSandbox()
->execute()->getStdout();

if ( preg_match( '/Bytes: (\d+)/', $output, $matches ) ) {
return (int)$matches[1];
}
}
}

$maintClass = SwiftDump::class;
Expand Down

0 comments on commit a58cfab

Please sign in to comment.