Skip to content

Commit

Permalink
experiment: Unify perl linting rules
Browse files Browse the repository at this point in the history
Testing the perltidy config from os-autoinst/os-autoinst-common#30
  • Loading branch information
josegomezr committed Oct 25, 2023
1 parent f8f5bc4 commit c3d8fe8
Show file tree
Hide file tree
Showing 193 changed files with 1,402 additions and 3,307 deletions.
2 changes: 1 addition & 1 deletion .perltidyrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# # See <https://github.com/houseabsolute/perl-code-tidyall/issues/84>.
--character-encoding=none
--no-valign
-l=120 # 120 characters per line
-l=160
-fbl # don't change blank lines
-nsfs # no spaces before semicolons
-baao # space after operators
Expand Down
3 changes: 1 addition & 2 deletions lib/OpenQA/CLI/archive.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ sub command ($self, @args) {

my $url = $self->url_for("jobs/$job/details");
my $client = $self->client($url);
$client->archive->run(
{url => $url, archive => $path, 'with-thumbnails' => $thumbnails, 'asset-size-limit' => $limit});
$client->archive->run({url => $url, archive => $path, 'with-thumbnails' => $thumbnails, 'asset-size-limit' => $limit});

return 0;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/OpenQA/CacheService.pm
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ sub setup_workers {
my @args = @_;

my $global_settings = OpenQA::Worker::Settings->new->global_settings;
my $cache_workers
= exists $global_settings->{CACHEWORKERS} ? $global_settings->{CACHEWORKERS} : DEFAULT_MINION_WORKERS;
my $cache_workers = exists $global_settings->{CACHEWORKERS} ? $global_settings->{CACHEWORKERS} : DEFAULT_MINION_WORKERS;
push @args, '-j', $cache_workers;

return @args;
Expand Down
20 changes: 6 additions & 14 deletions lib/OpenQA/CacheService/Model/Cache.pm
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ sub get_asset ($self, $host, $job, $type, $asset) {
# We can't just throw it away if database locks.
my $att = 0;
my $ok;
++$att and sleep 1 and $log->info("Updating cache failed (attempt $att)")
until ($ok = $self->_update_asset($asset, $etag, $size)) || $att > 5;
++$att and sleep 1 and $log->info("Updating cache failed (attempt $att)") until ($ok = $self->_update_asset($asset, $etag, $size)) || $att > 5;
die qq{Updating the cache for "$asset" failed, this should never happen} unless $ok;
my $cache_size = human_readable_size($self->{cache_real_size});
my $speed = download_speed($start, $end, $size);
Expand Down Expand Up @@ -160,8 +159,7 @@ sub track_asset ($self, $asset) {
eval {
my $db = $self->sqlite->db;
my $tx = $db->begin('exclusive');
my $sql = "INSERT INTO assets (filename, size, last_use) VALUES (?, 0, strftime('%s','now'))"
. "ON CONFLICT (filename) DO UPDATE SET pending=1";
my $sql = "INSERT INTO assets (filename, size, last_use) VALUES (?, 0, strftime('%s','now'))" . "ON CONFLICT (filename) DO UPDATE SET pending=1";
$db->query($sql, $asset);
$tx->commit;
};
Expand All @@ -180,14 +178,11 @@ sub _exclusive_query ($self, $sql, @args) {
}

sub _update_metric ($self, $name, $value) {
$self->_exclusive_query('INSERT INTO metrics (name, value) VALUES ($1, $2) ON CONFLICT DO UPDATE SET value = $2',
$name, $value);
$self->_exclusive_query('INSERT INTO metrics (name, value) VALUES ($1, $2) ON CONFLICT DO UPDATE SET value = $2', $name, $value);
}

sub _increase_metric ($self, $name, $by_value) {
$self->_exclusive_query(
'INSERT INTO metrics (name, value) VALUES ($1, $2) ON CONFLICT DO UPDATE SET value = value + $2',
$name, $by_value);
$self->_exclusive_query('INSERT INTO metrics (name, value) VALUES ($1, $2) ON CONFLICT DO UPDATE SET value = value + $2', $name, $by_value);
}

sub reset_download_count ($self) { $self->_update_metric(download_count => 0) }
Expand Down Expand Up @@ -284,12 +279,9 @@ sub _check_limits ($self, $needed, $to_preserve = undef) {
my $cache_size = human_readable_size($self->{cache_real_size});
my $needed_size = human_readable_size($needed);
my $limit_size = human_readable_size($limit);
$log->info(
"Cache size $cache_size + needed $needed_size exceeds limit of $limit_size, purging least used assets");
$log->info("Cache size $cache_size + needed $needed_size exceeds limit of $limit_size, purging least used assets");
eval {
my $results
= $self->sqlite->db->select('assets', [qw(filename size last_use)], {pending => '0'},
{-asc => 'last_use'});
my $results = $self->sqlite->db->select('assets', [qw(filename size last_use)], {pending => '0'}, {-asc => 'last_use'});
for my $asset ($results->hashes->each) {
my $filename = $asset->{filename};
next if $to_preserve && $to_preserve->{$filename};
Expand Down
6 changes: 2 additions & 4 deletions lib/OpenQA/Client/Archive.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ sub _download_test_result_details ($self, $url, $path, $module, $options) {

if ($options->{'with-thumbnails'}) {
$url->path("/image/$dir/.thumbs/$module->{md5_basename}");
$ua->get($url)
->res->content->asset->move_to($path->child('testresults', 'thumbnails', $module->{md5_basename}));
$ua->get($url)->res->content->asset->move_to($path->child('testresults', 'thumbnails', $module->{md5_basename}));
}

}
Expand Down Expand Up @@ -174,8 +173,7 @@ sub _progress_monitior ($ua, $tx) {
my $msg = shift;
return unless my $len = $msg->headers->content_length;
if ($limit && $msg->headers->content_length >= $limit) {
$msg->error(
{message => path($filename)->basename . " exceeeds maximum size limit of $limit", code => 509});
$msg->error({message => path($filename)->basename . " exceeeds maximum size limit of $limit", code => 509});
}
});

Expand Down
3 changes: 1 addition & 2 deletions lib/OpenQA/Client/Upload.pm
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ sub asset ($self, $job_id, $opts) {
my $parts = OpenQA::File->new(file => Mojo::File->new($opts->{file}))->split($chunk_size);
$self->emit('upload_chunk.prepare', $parts);

$self->once('upload_chunk.error',
sub { $self->_upload_asset_fail($uri => {filename => $file_name, scope => $opts->{asset}}) });
$self->once('upload_chunk.error', sub { $self->_upload_asset_fail($uri => {filename => $file_name, scope => $opts->{asset}}) });

# Each chunk of the file should get the full number of retry attempts
my $max_retries = $opts->{retries} // 10;
Expand Down
10 changes: 4 additions & 6 deletions lib/OpenQA/Command.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use Mojo::URL;
use Mojo::File qw(path);
use Term::ANSIColor qw(colored);

my $JSON = Cpanel::JSON::XS->new->utf8->canonical->allow_nonref->allow_unknown->allow_blessed->convert_blessed
->stringify_infnan->escape_slash->allow_dupkeys->pretty;
my $JSON
= Cpanel::JSON::XS->new->utf8->canonical->allow_nonref->allow_unknown->allow_blessed->convert_blessed->stringify_infnan->escape_slash->allow_dupkeys->pretty;
my $PARAM_RE = qr/^([[:alnum:]_\[\]\.\:]+)=(.*)$/s;

has apibase => '/api/v1';
Expand All @@ -23,8 +23,7 @@ has host => 'http://localhost';
has options => undef;

sub client ($self, $url) {
my $client = OpenQA::Client->new(apikey => $self->apikey, apisecret => $self->apisecret, api => $url->host)
->ioloop(Mojo::IOLoop->singleton);
my $client = OpenQA::Client->new(apikey => $self->apikey, apisecret => $self->apisecret, api => $url->host)->ioloop(Mojo::IOLoop->singleton);
$client->transactor->name($self->name);
return $client;
}
Expand Down Expand Up @@ -125,8 +124,7 @@ sub retry_tx ($self, $client, $tx, $retries = undef, $delay = undef) {
$tx = $client->start($tx);
my $res_code = $tx->res->code // 0;
return $self->handle_result($tx) unless $res_code =~ /50[23]/ && $retries > 0;
print encode('UTF-8',
"Request failed, hit error $res_code, retrying up to $retries more times after waiting …\n");
print encode('UTF-8', "Request failed, hit error $res_code, retrying up to $retries more times after waiting …\n");
sleep $delay;
}
}
Expand Down
5 changes: 2 additions & 3 deletions lib/OpenQA/Constants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use constant WORKER_STOP_REASONS => (
# Define error categories used alongside the reasons defined above for finer error handling where needed
use constant {
WORKER_EC_CACHE_FAILURE => 'cache failure', # the cache service made problems
WORKER_EC_ASSET_FAILURE => 'asset failure', # a problem occurred when handling assets, e.g. an asset was not found
WORKER_EC_ASSET_FAILURE => 'asset failure', # a problem occurred when handling assets, e.g. an asset was not found
};

# Time verification to use with the "worker_timeout" configuration.
Expand Down Expand Up @@ -98,8 +98,7 @@ use constant VIDEO_FILE_NAME_REGEX => qr/^.*\/video\.[^\/]*$/;

use constant FRAGMENT_REGEX => qr'(#([-?/:@.~!$&\'()*+,;=\w]|%[0-9a-fA-F]{2})*)*';

use constant JOBS_OVERVIEW_SEARCH_CRITERIA =>
(qw(distri version flavor build test modules modules_result module_re group groupid id));
use constant JOBS_OVERVIEW_SEARCH_CRITERIA => (qw(distri version flavor build test modules modules_result module_re group groupid id));

our @EXPORT_OK = qw(
WEBSOCKET_API_VERSION DEFAULT_WORKER_TIMEOUT
Expand Down
3 changes: 1 addition & 2 deletions lib/OpenQA/Files.pm
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ sub verify_chunks ($class, $chunk_path, $verify_file) {

$chunk->decode_content;
$sum = $chunk->total_cksum if !$sum;
return Mojo::Exception->new(
"Chunk: " . $chunk->id() . " differs in total checksum, expected $sum given " . $chunk->total_cksum)
return Mojo::Exception->new("Chunk: " . $chunk->id() . " differs in total checksum, expected $sum given " . $chunk->total_cksum)
if $sum ne $chunk->total_cksum;
return Mojo::Exception->new("Can't verify written data from chunk")
unless $chunk->verify_content($verify_file);
Expand Down
21 changes: 5 additions & 16 deletions lib/OpenQA/Jobs/Constants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ use constant {
USER_RESTARTED => 'user_restarted', # restarted by user via job_restart
TIMEOUT_EXCEEDED => 'timeout_exceeded', # killed by the worker after MAX_JOB_TIME has been exceeded
};
use constant RESULTS => (
NONE, PASSED, SOFTFAILED, FAILED, INCOMPLETE, SKIPPED,
OBSOLETED, PARALLEL_FAILED, PARALLEL_RESTARTED, USER_CANCELLED, USER_RESTARTED, TIMEOUT_EXCEEDED
);
use constant RESULTS =>
(NONE, PASSED, SOFTFAILED, FAILED, INCOMPLETE, SKIPPED, OBSOLETED, PARALLEL_FAILED, PARALLEL_RESTARTED, USER_CANCELLED, USER_RESTARTED, TIMEOUT_EXCEEDED);

# note: See the "Jobs" section of "GettingStarted.asciidoc" for the difference between SOFTFAILED and FAILED and
# further details.
Expand All @@ -75,8 +73,7 @@ use constant RESULTS => (
use constant COMPLETE_RESULTS => (PASSED, SOFTFAILED, FAILED);
use constant OK_RESULTS => (PASSED, SOFTFAILED);
use constant NOT_COMPLETE_RESULTS => (INCOMPLETE, TIMEOUT_EXCEEDED);
use constant ABORTED_RESULTS =>
(SKIPPED, OBSOLETED, PARALLEL_FAILED, PARALLEL_RESTARTED, USER_CANCELLED, USER_RESTARTED);
use constant ABORTED_RESULTS => (SKIPPED, OBSOLETED, PARALLEL_FAILED, PARALLEL_RESTARTED, USER_CANCELLED, USER_RESTARTED);
use constant NOT_OK_RESULTS => (FAILED, NOT_COMPLETE_RESULTS, ABORTED_RESULTS);
use constant {
COMPLETE => 'complete',
Expand Down Expand Up @@ -143,16 +140,8 @@ our @EXPORT = qw(
);

# mapping from any specific job state/result to a meta state/result
my %META_STATE_MAPPING = (
(map { $_ => PRE_EXECUTION } PRE_EXECUTION_STATES),
(map { $_ => EXECUTION } EXECUTION_STATES),
(map { $_ => FINAL } FINAL_STATES),
);
my %META_RESULT_MAPPING = (
(map { $_ => $_ } COMPLETE_RESULTS),
(map { $_ => NOT_COMPLETE } NOT_COMPLETE_RESULTS),
(map { $_ => ABORTED } ABORTED_RESULTS),
);
my %META_STATE_MAPPING = ((map { $_ => PRE_EXECUTION } PRE_EXECUTION_STATES), (map { $_ => EXECUTION } EXECUTION_STATES), (map { $_ => FINAL } FINAL_STATES),);
my %META_RESULT_MAPPING = ((map { $_ => $_ } COMPLETE_RESULTS), (map { $_ => NOT_COMPLETE } NOT_COMPLETE_RESULTS), (map { $_ => ABORTED } ABORTED_RESULTS),);
sub meta_state ($state) { $META_STATE_MAPPING{$state} // NONE }
sub meta_result ($result) { $META_RESULT_MAPPING{$result} // NONE }
sub is_ok_result ($result) {
Expand Down
6 changes: 2 additions & 4 deletions lib/OpenQA/LiveHandler.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ has secrets => sub { shift->schema->read_application_secrets };

# add attributes to store ws connections/transactions by job
# (see LiveViewHandler.pm for further descriptions of the paricular attributes)
has [qw(cmd_srv_transactions_by_job devel_java_script_transactions_by_job status_java_script_transactions_by_job)] =>
sub { {} };
has [qw(cmd_srv_transactions_by_job devel_java_script_transactions_by_job status_java_script_transactions_by_job)] => sub { {} };

# This method will run once at server start
sub startup ($self) {
Expand Down Expand Up @@ -53,8 +52,7 @@ sub startup ($self) {

# register API routes
my $job_r = $api_ro->any('/jobs/<testid:num>');
$job_r->post('/upload_progress')->name('developer_post_upload_progress')
->to('live_view_handler#post_upload_progress');
$job_r->post('/upload_progress')->name('developer_post_upload_progress')->to('live_view_handler#post_upload_progress');

OpenQA::Setup::setup_plain_exception_handler($self);
}
Expand Down
47 changes: 15 additions & 32 deletions lib/OpenQA/LiveHandler/Controller/LiveViewHandler.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ sub send_message_to_java_script_clients {
my ($self, $job_id, $type, $what, $data, $status_code_to_quit_on_finished) = @_;

my @all_java_script_transactions_for_job;
for my $java_script_transaction_container ($self->devel_java_script_transactions_by_job,
$self->status_java_script_transactions_by_job)
{
for my $java_script_transaction_container ($self->devel_java_script_transactions_by_job, $self->status_java_script_transactions_by_job) {
if (my $java_script_transactions_for_job = $java_script_transaction_container->{$job_id}) {
push(@all_java_script_transactions_for_job, @$java_script_transactions_for_job);
}
Expand Down Expand Up @@ -132,9 +130,7 @@ sub send_message_to_java_script_client_and_finish {
sub finish_all_connections {
my ($self, $job_id, $status_code) = @_;

for my $java_script_transaction_container ($self->devel_java_script_transactions_by_job,
$self->status_java_script_transactions_by_job)
{
for my $java_script_transaction_container ($self->devel_java_script_transactions_by_job, $self->status_java_script_transactions_by_job) {
my $java_script_transactions_for_current_job = delete $java_script_transaction_container->{$job_id} or next;
$_->finish($status_code) for (@$java_script_transactions_for_current_job);
}
Expand Down Expand Up @@ -317,10 +313,8 @@ sub handle_message_from_os_autoinst {
if ($is_status_message) {
if (!$self->check_os_autoinst_devel_mode_version($json)) {
my $actual_version_str = "$json->{devel_mode_major_version}.$json->{devel_mode_minor_version}";
my $required_version_str
= OS_AUTOINST_DEVEL_MODE_MAJOR_VERSION . '.' . OS_AUTOINST_DEVEL_MODE_MINOR_VERSION;
my $disconnect_reason
= "os-autoinst version \"$actual_version_str\" is incompatible, version \"$required_version_str\" is required";
my $required_version_str = OS_AUTOINST_DEVEL_MODE_MAJOR_VERSION . '.' . OS_AUTOINST_DEVEL_MODE_MINOR_VERSION;
my $disconnect_reason = "os-autoinst version \"$actual_version_str\" is incompatible, version \"$required_version_str\" is required";
$self->disconnect_from_os_autoinst($job_id, $disconnect_reason);
return;
}
Expand Down Expand Up @@ -353,14 +347,12 @@ sub connect_to_cmd_srv {
my ($self, $job_id, $cmd_srv_raw_url, $cmd_srv_url) = @_;

log_debug("connecting to os-autoinst command server for job $job_id at $cmd_srv_raw_url");
$self->send_message_to_java_script_clients($job_id,
info => 'connecting to os-autoinst command server at ' . $cmd_srv_raw_url);
$self->send_message_to_java_script_clients($job_id, info => 'connecting to os-autoinst command server at ' . $cmd_srv_raw_url);

# prevent opening the same connection to os-autoinst cmd srv twice
if (my $cmd_srv_tx = $self->cmd_srv_transactions_by_job->{$job_id}) {
$self->query_os_autoinst_status($job_id);
$self->send_message_to_java_script_clients($job_id,
info => 'reusing previous connection to os-autoinst command server at ' . $cmd_srv_raw_url);
$self->send_message_to_java_script_clients($job_id, info => 'reusing previous connection to os-autoinst command server at ' . $cmd_srv_raw_url);
return $cmd_srv_tx;
}

Expand All @@ -373,11 +365,10 @@ sub connect_to_cmd_srv {
my ($ua, $tx) = @_;

# upgrade to ws connection if not already a websocket connection
return $self->send_message_to_java_script_clients_and_finish($job_id,
error => 'unable to upgrade ws to command server')
return $self->send_message_to_java_script_clients_and_finish($job_id, error => 'unable to upgrade ws to command server')
unless $tx->is_websocket;

# assign transaction: don't do this before to prevent regular HTTP connections to be used in send_message_to_os_autoinst
# assign transaction: don't do this before to prevent regular HTTP connections to be used in send_message_to_os_autoinst
$self->cmd_srv_transactions_by_job->{$job_id} = $tx;

# instantly query the os-autoinst status
Expand Down Expand Up @@ -407,8 +398,7 @@ sub send_message_to_os_autoinst {

my $cmd_srv_tx = $self->cmd_srv_transactions_by_job->{$job_id};
if (!$cmd_srv_tx) {
$self->send_message_to_java_script_clients($job_id,
error => 'failed to pass message to os-autoinst command server because not connected yet');
$self->send_message_to_java_script_clients($job_id, error => 'failed to pass message to os-autoinst command server because not connected yet');
return;
}
$cmd_srv_tx->send({json => $msg});
Expand Down Expand Up @@ -496,27 +486,22 @@ sub ws_proxy {
my $developer_session = $developer_sessions->register($job_id, $user_id);
$app->log->debug('client connected: ' . $user->name);
if (!$developer_session) {
return $self->send_message_to_java_script_client_and_finish($java_script_tx,
error => 'unable to create (further) development session');
return $self->send_message_to_java_script_client_and_finish($java_script_tx, error => 'unable to create (further) development session');
}
# mark session as active
$developer_session->update({ws_connection_count => \'ws_connection_count + 1'}); #'restore syntax highlighting
$developer_session->update({ws_connection_count => \'ws_connection_count + 1'}); #'restore syntax highlighting
}

# add JavaScript transaction to the list of JavaScript transactions for this job
# (needed for broadcasting to all clients)
my $java_script_transaction_container
= $status_only ? $self->status_java_script_transactions_by_job : $self->devel_java_script_transactions_by_job;
my $java_script_transaction_container = $status_only ? $self->status_java_script_transactions_by_job : $self->devel_java_script_transactions_by_job;
my $java_script_transactions_for_current_job = ($java_script_transaction_container->{$job_id} //= []);
push(@$java_script_transactions_for_current_job, $java_script_tx);

# determine url to os-autoinst command server
my $cmd_srv_raw_url = $self->determine_os_autoinst_web_socket_url($job);
if (!$cmd_srv_raw_url) {
$app->log->debug('attempt to open ws proxy for job '
. $job->name . ' ('
. $job_id
. ') where URL to os-autoinst command server is unknown');
$app->log->debug('attempt to open ws proxy for job ' . $job->name . ' (' . $job_id . ') where URL to os-autoinst command server is unknown');
$self->send_message_to_java_script_clients_and_finish(
$job_id,
error => 'os-autoinst command server not available, job is likely not running',
Expand All @@ -534,8 +519,7 @@ sub ws_proxy {
$self->on(message => sub { });
return $self->on(
finish => sub {
$self->remove_java_script_transaction($job_id, $self->status_java_script_transactions_by_job,
$java_script_tx);
$self->remove_java_script_transaction($job_id, $self->status_java_script_transactions_by_job, $java_script_tx);
});
}

Expand Down Expand Up @@ -574,8 +558,7 @@ sub post_upload_progress {
$worker->update({upload_progress => $progress_info});

# broadcast the upload progress to all connected java script web socket clients for this job
my $broadcast_count
= $self->send_message_to_java_script_clients($job_id, info => 'upload progress', $progress_info);
my $broadcast_count = $self->send_message_to_java_script_clients($job_id, info => 'upload progress', $progress_info);
return $self->render(json => {broadcast_count => $broadcast_count}, status => 200);
}

Expand Down
Loading

0 comments on commit c3d8fe8

Please sign in to comment.