Reduce nesting by turning big if-else into guard clauses

Change-Id: If3d2f18af0cc7d0f8fe9f764bd4d0e4bae70e440
This commit is contained in:
Thiemo Kreuz 2019-11-08 17:19:13 +01:00
parent b777cc6b46
commit 9c6473dc1d
3 changed files with 35 additions and 39 deletions

View file

@ -386,22 +386,22 @@ class JobQueueDB extends JobQueue {
} }
} }
if ( $row ) { // claim the job if ( !$row ) {
$dbw->update( 'job', // update by PK break;
[ }
'job_token' => $uuid,
'job_token_timestamp' => $dbw->timestamp(), $dbw->update( 'job', // update by PK
'job_attempts = job_attempts+1' ], [
[ 'job_cmd' => $this->type, 'job_id' => $row->job_id, 'job_token' => '' ], 'job_token' => $uuid,
__METHOD__ 'job_token_timestamp' => $dbw->timestamp(),
); 'job_attempts = job_attempts+1' ],
// This might get raced out by another runner when claiming the previously [ 'job_cmd' => $this->type, 'job_id' => $row->job_id, 'job_token' => '' ],
// selected row. The use of job_random should minimize this problem, however. __METHOD__
if ( !$dbw->affectedRows() ) { );
$row = false; // raced out // This might get raced out by another runner when claiming the previously
} // selected row. The use of job_random should minimize this problem, however.
} else { if ( !$dbw->affectedRows() ) {
break; // nothing to do $row = false; // raced out
} }
} while ( !$row ); } while ( !$row );
@ -455,16 +455,17 @@ class JobQueueDB extends JobQueue {
__METHOD__ __METHOD__
); );
} }
if ( !$dbw->affectedRows() ) {
break;
}
// Fetch any row that we just reserved... // Fetch any row that we just reserved...
if ( $dbw->affectedRows() ) { $row = $dbw->selectRow( 'job', self::selectFields(),
$row = $dbw->selectRow( 'job', self::selectFields(), [ 'job_cmd' => $this->type, 'job_token' => $uuid ], __METHOD__
[ 'job_cmd' => $this->type, 'job_token' => $uuid ], __METHOD__ );
); if ( !$row ) { // raced out by duplicate job removal
if ( !$row ) { // raced out by duplicate job removal wfDebug( "Row deleted as duplicate by another process." );
wfDebug( "Row deleted as duplicate by another process." );
}
} else {
break; // nothing to do
} }
} while ( !$row ); } while ( !$row );

View file

@ -207,19 +207,14 @@ class SwiftFileBackend extends FileBackendStore {
$contentHeaders[$name] = $value; $contentHeaders[$name] = $value;
} }
// By default, Swift has annoyingly low maximum header value limits // By default, Swift has annoyingly low maximum header value limits
if ( isset( $contentHeaders['content-disposition'] ) ) { $maxLength = 255;
$disposition = ''; // @note: assume FileBackend::makeContentDisposition() already used
// @note: assume FileBackend::makeContentDisposition() already used $offset = $maxLength - strlen( $contentHeaders['content-disposition'] );
foreach ( explode( ';', $contentHeaders['content-disposition'] ) as $part ) { if ( $offset < 0 ) {
$part = trim( $part ); $pos = strrpos( $contentHeaders['content-disposition'], ';', $offset );
$new = ( $disposition === '' ) ? $part : "{$disposition};{$part}"; $contentHeaders['content-disposition'] = $pos === false
if ( strlen( $new ) <= 255 ) { ? ''
$disposition = $new; : trim( substr( $contentHeaders['content-disposition'], 0, $pos ) );
} else {
break; // too long; sigh
}
}
$contentHeaders['content-disposition'] = $disposition;
} }
return $contentHeaders; return $contentHeaders;

View file

@ -73,7 +73,7 @@ class SwiftFileBackendTest extends MediaWikiIntegrationTestCase {
], ],
[ [
'content-type' => 'image+bitmap/jpeg', 'content-type' => 'image+bitmap/jpeg',
'content-disposition' => 'inline;filename=xxx', 'content-disposition' => 'inline; filename=xxx',
'content-duration' => 35.6363, 'content-duration' => 35.6363,
'content-custom' => 'hello', 'content-custom' => 'hello',
'x-content-custom' => 'hello' 'x-content-custom' => 'hello'