From 674efac4310c620b2744bcb1c6b58d7aefcf17ab Mon Sep 17 00:00:00 2001 From: eleksir Date: Sat, 12 Feb 2022 23:04:52 +0300 Subject: [PATCH] Perlcritic overhaul, update cpanfile --- .perlcriticrc | 18 +++++++++++++++++- bin/aleesa-misc | 27 ++++++++++++++------------- cpanfile | 8 +++++--- lib/Conf.pm | 22 +++++++++++++++------- lib/Misc.pm | 27 +++++++++++++-------------- 5 files changed, 64 insertions(+), 38 deletions(-) diff --git a/.perlcriticrc b/.perlcriticrc index 4045ea6..3103b92 100644 --- a/.perlcriticrc +++ b/.perlcriticrc @@ -94,4 +94,20 @@ lib_sections = NAME | VERSION | DESCRIPTION | AUTHOR | LICENSE AND COPYRIGHT # Have to use it, so fuckoff [-Variables::ProhibitPackageVars] -# fuck off \ No newline at end of file +# fuck off + +[-Editor::RequireEmacsFileVariables] + +[-TooMuchCode::ProhibitUnnecessaryUTF8Pragma] + +[-CodeLayout::RequireASCII] + +[-TooMuchCode::ProhibitDuplicateLiteral] + +[-ErrorHandling::RequireUseOfExceptions] + +[-ValuesAndExpressions::RequireConstantOnLeftSideOfEquality] + +[-Lax::RequireConstantOnLeftSideOfEquality::ExceptEq] + +[-Miscellanea::ProhibitUnrestrictedNoCritic] diff --git a/bin/aleesa-misc b/bin/aleesa-misc index f01fc02..e533ecd 100755 --- a/bin/aleesa-misc +++ b/bin/aleesa-misc @@ -1,6 +1,6 @@ #!/usr/bin/perl -use 5.018; +use 5.018; ## no critic (ProhibitImplicitImport) use strict; use warnings; use utf8; @@ -29,9 +29,10 @@ no Cwd; use Cwd::utf8 qw (chdir abs_path); use File::Basename qw (basename dirname); use File::Path qw (make_path); +use File::Spec (); use Log::Any qw ($log); -use Log::Any::Adapter; -use Log::Any::For::Std; +use Log::Any::Adapter (); +use Log::Any::For::Std (); use POSIX qw (setgid setuid setsid); use Conf qw (LoadConf); use Misc qw (RunMisc); @@ -69,8 +70,8 @@ if ($UID == 0) { make_path ( $piddir, { uid => $uid, - group => $gid - } + group => $gid, + }, ); } @@ -81,8 +82,8 @@ if ($UID == 0) { make_path ( $logdir, { uid => $uid, - group => $gid - } + group => $gid, + }, ) or die "Unable to create $logdir: $OS_ERROR\n"; } } @@ -97,7 +98,7 @@ if ($UID == 0) { if (defined $logfile) { Log::Any::Adapter->set ('File', $logfile, log_level => $loglevel); } else { - Log::Any::Adapter->set ('File', '/dev/null'); + Log::Any::Adapter->set ('File', File::Spec->devnull); } # По-умолчанию, демонизируемся, если явно не указано, что этого делать не надо @@ -113,7 +114,7 @@ my $apath = abs_path $PROGRAM_NAME; unless (defined $apath) { $log->fatal ("[FATAL] Unable to get abs_path: $OS_ERROR\n"); exit 1; -}; +} our $pidfile = sprintf '%s/%s.pid', $piddir, basename $apath; $log->info ("[INFO] Creating pidfile at $pidfile"); @@ -141,7 +142,7 @@ $PROGRAM_NAME = abs_path $PROGRAM_NAME; ## no critic (Variables::Re # И запустим уже наш сервис RunMisc (); -if (-f $pidfile) { +if (-e $pidfile) { unlink $pidfile; } @@ -149,8 +150,8 @@ exit 0; sub daemonize { __fork (); - POSIX::setsid (); - open STDIN, '<', '/dev/null'; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen) + setsid (); + open STDIN, '<', File::Spec->devnull; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen) __fork (); umask 0; return; @@ -174,7 +175,7 @@ sub __fork { sub __signal_handler { $log->info ("[INFO] Caught a signal $OS_ERROR"); - if (-f $pidfile) { + if (-e $pidfile) { unlink $pidfile; } diff --git a/cpanfile b/cpanfile index 3b8dbff..e687c07 100644 --- a/cpanfile +++ b/cpanfile @@ -1,3 +1,4 @@ +## no critic requires 'Carp', '==1.50'; requires 'Clone', '==0.45'; requires 'Cwd', '==3.75'; @@ -7,8 +8,9 @@ requires 'Encode', '==3.16'; requires 'English', '==1.11'; requires 'EV', '==4.33'; requires 'Exporter', '==5.74'; -requires 'File::Basename', '0'; +requires 'File::Basename', '==2.85'; requires 'File::Path', '==2.18'; +requires 'File::Spec', '==3.75'; requires 'IO::Socket::SSL', '==2.072'; requires 'JSON', '==4.03'; requires 'JSON::XS', '==4.03'; @@ -20,8 +22,8 @@ requires 'Math::Random::Secure', '==0.080001'; requires 'Mojolicious', '==9.22'; requires 'Mojo::Redis', '==3.27'; requires 'Mojo::IOLoop::Signal', '==0.003'; -requires 'open', '0'; +requires 'open', '==1.12'; requires 'POSIX', '0'; requires 'Protocol::Redis::XS', '0'; requires 'utf8', '0'; -requires 'version', '0'; \ No newline at end of file +requires 'version', '==0.9929'; \ No newline at end of file diff --git a/lib/Conf.pm b/lib/Conf.pm index 9f6928c..d9a167a 100644 --- a/lib/Conf.pm +++ b/lib/Conf.pm @@ -1,14 +1,13 @@ package Conf; -# loads config +# Загружает конфиг файл -use 5.018; +use 5.018; ## no critic (ProhibitImplicitImport) use strict; use warnings; use utf8; use open qw (:std :utf8); use English qw ( -no_match_vars ); -use Encode qw (encode); -use JSON::XS; +use JSON::XS (); use version; our $VERSION = qw (1.0); use Exporter qw (import); @@ -16,17 +15,26 @@ our @EXPORT_OK = qw (LoadConf); sub LoadConf { my $c = 'data/config.json'; - open my $CH, '<', $c or die "[FATAL] No conf at $c: $OS_ERROR\n"; - my $json; + my $CH; + + unless (open $CH, '<', $c) { + die "[FATAL] No conf at $c: $OS_ERROR\n"; + } + my $len = (stat $c) [7]; + my $json; my $readlen = read $CH, $json, $len; - $json = encode 'UTF-8', $json; unless ($readlen) { close $CH; ## no critic (InputOutput::RequireCheckedSyscalls die "[FATAL] Unable to read $c: $OS_ERROR\n"; } + if ($readlen != $len) { + close $CH; ## no critic (InputOutput::RequireCheckedSyscalls + die "[FATAL] File $c is $len bytes on disk, but we read only $readlen bytes\n"; + } + close $CH; ## no critic (InputOutput::RequireCheckedSyscalls my $j = JSON::XS->new->utf8->relaxed; return $j->decode ($json); diff --git a/lib/Misc.pm b/lib/Misc.pm index 2f74bba..9b418d6 100644 --- a/lib/Misc.pm +++ b/lib/Misc.pm @@ -1,7 +1,7 @@ package Misc; # Общие модули - синтаксис, кодировки итд -use 5.018; +use 5.018; ## no critic (ProhibitImplicitImport) use strict; use warnings; use utf8; @@ -10,13 +10,12 @@ use open qw (:std :utf8); use Clone qw (clone); # Модули для работы приложения use Log::Any qw ($log); -use Math::Random::Secure qw (irand); # Чтобы "уж точно" использовать hiredis-биндинги, загрузим этот модуль перед Mojo::Redis -use Protocol::Redis::XS; -use Mojo::Redis; -use Mojo::IOLoop; -use Mojo::IOLoop::Signal; -use Data::Dumper; +use Protocol::Redis::XS (); +use Mojo::Redis (); +use Mojo::IOLoop (); +use Mojo::IOLoop::Signal (); +use Data::Dumper qw (Dumper); use Conf qw (LoadConf); @@ -136,7 +135,7 @@ my $parse_message = sub { $log->debug ("[DEBUG] Sending message to channel $send_to " . Dumper ($answer)); $self->json ($send_to)->notify ( - $send_to => $answer + $send_to => $answer, ); return; @@ -146,7 +145,7 @@ my $__signal_handler = sub { my ($self, $name) = @_; $log->info ("[INFO] Caught a signal $name"); - if (defined $main::pidfile && -f $main::pidfile) { + if (defined $main::pidfile && -e $main::pidfile) { unlink $main::pidfile; } @@ -159,7 +158,7 @@ sub RunMisc { $log->info ("[INFO] Connecting to $c->{server}, $c->{port}"); my $redis = Mojo::Redis->new ( - sprintf 'redis://%s:%s/1', $c->{server}, $c->{port} + sprintf 'redis://%s:%s/1', $c->{server}, $c->{port}, ); $log->info ('[INFO] Registering connection-event callback'); @@ -176,11 +175,11 @@ sub RunMisc { my ($conn, $error) = @_; $log->error ("[ERROR] Redis connection error: $error"); return; - } + }, ); return; - } + }, ); my $pubsub = $redis->pubsub; @@ -191,13 +190,13 @@ sub RunMisc { $log->debug ("[DEBUG] Subscribing to $channel"); $sub->{$channel} = $pubsub->json ($channel)->listen ( - $channel => sub { $parse_message->(@_); } + $channel => sub { $parse_message->(@_); }, ); } Mojo::IOLoop::Signal->on ( TERM => $__signal_handler, - INT => $__signal_handler + INT => $__signal_handler, ); do { Mojo::IOLoop->start } until Mojo::IOLoop->is_running;