Skip to content

Commit 98723f6

Browse files
committed
Log output of optipng when it fails
* Remove the `-quiet` parameter * Use `run_cmd_with_log_return_error` for logging the command output in the error case * Improve error messages in `run_cmd_with_log_return_error` to include the program name so the error message is also useful on its own (without the prior `Running cmd: …` message) * Make `run_cmd_with_log_return_error` return the error message so it can be passed as reason when using it to run `optipng` * See https://progress.opensuse.org/issues/190920
1 parent a3a7f84 commit 98723f6

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

lib/OpenQA/Utils.pm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ sub run_cmd_with_log_return_error ($cmd, %args) {
305305
my $stdout_level = $args{stdout} // 'debug';
306306
my $stderr_level = $args{stderr} // 'debug';
307307
my $output_file = $args{output_file};
308+
my $program = $cmd->[0] // 'cmd';
308309
log_info('Running cmd: ' . join(' ', @$cmd));
309310
try {
310311
my ($stdin, $stdout, $stderr) = ('') x 3;
@@ -314,8 +315,8 @@ sub run_cmd_with_log_return_error ($cmd, %args) {
314315
my $return_code = ($error_code & 127) ? (undef) : ($error_code >> 8);
315316
my $message
316317
= defined $return_code
317-
? ("cmd returned $return_code")
318-
: sprintf('cmd died with signal %d', $error_code & 127);
318+
? ("$program returned $return_code")
319+
: sprintf('%s died with signal %d', $program, $error_code & 127);
319320
my $expected_return_codes = $args{expected_return_codes};
320321
chomp $stderr;
321322
if (
@@ -337,6 +338,7 @@ sub run_cmd_with_log_return_error ($cmd, %args) {
337338
return_code => $return_code,
338339
stdout => $stdout,
339340
stderr => $stderr,
341+
message => $message,
340342
};
341343
}
342344
catch ($e) {
@@ -345,6 +347,7 @@ sub run_cmd_with_log_return_error ($cmd, %args) {
345347
return_code => undef,
346348
stderr => 'an internal error occurred',
347349
stdout => '',
350+
message => "failed to execute $program",
348351
};
349352
}
350353
}

lib/OpenQA/Worker/Job.pm

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use OpenQA::Jobs::Constants;
1111
use OpenQA::Worker::Engines::isotovideo;
1212
use OpenQA::Worker::Isotovideo::Client;
1313
use OpenQA::Log qw(log_error log_warning log_debug log_info redact_settings_in_file);
14-
use OpenQA::Utils qw(find_video_files usleep_backoff);
14+
use OpenQA::Utils qw(find_video_files usleep_backoff run_cmd_with_log_return_error);
1515

1616
use Digest::MD5;
1717
use Fcntl;
@@ -1241,11 +1241,9 @@ sub _optimize_image ($image, $job_settings, $optipng_bin = 'optipng', $pngquant_
12411241
my @command
12421242
= $job_settings->{USE_PNGQUANT}
12431243
? ($pngquant_bin, '--force', '--output', $image, $image)
1244-
: ($optipng_bin, '-quiet', '-o2', $image);
1245-
system @command;
1246-
die "$OPTIMIZE_ERROR failed to execute $command[0]: $!\n" if $? == -1;
1247-
die sprintf("$OPTIMIZE_ERROR %s failed with signal %d\n", $command[0], $? & 127) if $? & 127;
1248-
die sprintf("$OPTIMIZE_ERROR %s exited with non-zero return code %d\n", $command[0], $? >> 8) if $?;
1244+
: ($optipng_bin, '-o2', $image);
1245+
my $res = run_cmd_with_log_return_error \@command;
1246+
die "$OPTIMIZE_ERROR $res->{message}\n" unless $res->{status};
12491247
return 1;
12501248
}
12511249

t/24-worker-jobs.t

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,11 +1242,9 @@ subtest 'image optimization' => sub {
12421242
combined_like {
12431243
is $opt->('foo', {OPTIMIZE_IMAGES => 0}), 0, 'image optimization can be skipped';
12441244
throws_ok { $opt->('foo', {}) }
1245-
qr/(failed to execute optipng|optipng exited with non-zero return code)/,
1246-
'failing to run optipng is a hard error';
1245+
qr/(failed to execute optipng|optipng returned)/, 'failing to run optipng is a hard error';
12471246
throws_ok { $opt->('foo', {USE_PNGQUANT => 1}) }
1248-
qr/(failed to execute pngquant|pngquant exited with non-zero return code)/,
1249-
'failing to run pngquant is a hard error';
1247+
qr/(failed to execute pngquant|pngquant returned)/, 'failing to run pngquant is a hard error';
12501248
is $opt->('bar', {}, 'true'), 1, 'successful optimization';
12511249
}
12521250
qr/Optimizing foo.*Optimizing bar/s, 'optimizing logged';

0 commit comments

Comments
 (0)