Compare commits
2 Commits
4e0696a0b4
...
35afe1c492
Author | SHA1 | Date | |
---|---|---|---|
35afe1c492 | |||
f75b21ad1e |
34
poller.pl
34
poller.pl
@@ -17,11 +17,6 @@ use URI::Escape;
|
|||||||
# --- Environment Variables ---
|
# --- Environment Variables ---
|
||||||
use constant LIDARR_BASE => $ENV{LIDARR_BASE} || 'http://localhost:8686';
|
use constant LIDARR_BASE => $ENV{LIDARR_BASE} || 'http://localhost:8686';
|
||||||
|
|
||||||
# ANSI escape sequences
|
|
||||||
sub ERASE_EOL { return "\033[K"; }
|
|
||||||
sub CURSOR_BACK { return "\033[${_[0]}D"; }
|
|
||||||
sub STATUS { return $_[0] . ERASE_EOL . CURSOR_BACK(length($_[0])) }
|
|
||||||
|
|
||||||
getopts('bfhv');
|
getopts('bfhv');
|
||||||
|
|
||||||
my $fresh_result = defined $::opt_f ? $::opt_f : 0; # vs STALE
|
my $fresh_result = defined $::opt_f ? $::opt_f : 0; # vs STALE
|
||||||
@@ -96,7 +91,7 @@ foreach my $id (@ids_to_process) {
|
|||||||
LIDARR_BASE . "/add/search?term=" . uri_escape("lidarr:$id") . "\n";
|
LIDARR_BASE . "/add/search?term=" . uri_escape("lidarr:$id") . "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
## subroutines (ping_api and validate remain the same as your original script)
|
## subroutines
|
||||||
|
|
||||||
sub ping_api {
|
sub ping_api {
|
||||||
my ($type, $id) = @_;
|
my ($type, $id) = @_;
|
||||||
@@ -110,24 +105,34 @@ sub ping_api {
|
|||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
$loops++;
|
$loops++;
|
||||||
print STDERR STATUS("- attempt $loops");
|
print STDERR "- attempt $loops...\n";
|
||||||
my $res = $ua->get($api);
|
my $res = $ua->get($api);
|
||||||
my $status = $res->code;
|
my $status = $res->code;
|
||||||
|
|
||||||
if ($res->is_success) {
|
if ($res->is_success) {
|
||||||
eval {
|
eval {
|
||||||
$json = decode_json($res->content);
|
$json = decode_json($res->content);
|
||||||
};
|
};
|
||||||
if ($@) {
|
if ($@) {
|
||||||
chomp $@;
|
chomp $@;
|
||||||
warn "Retrying: $@\n";
|
warn "Retrying: JSON decode failed: $@\n";
|
||||||
} elsif ($fresh_result and $res->header('cf-cache-status') eq 'STALE') {
|
} elsif ($fresh_result and $res->header('cf-cache-status') eq 'STALE') {
|
||||||
$status .= ' STALE';
|
$status .= ' STALE';
|
||||||
|
warn "Retrying: Response is STALE ($status)\n";
|
||||||
} else {
|
} else {
|
||||||
print STDERR ERASE_EOL;
|
# *** VALIDATION CHECK ***
|
||||||
last;
|
my @validation_warnings = validate($type, $json);
|
||||||
|
if (@validation_warnings) {
|
||||||
|
warn "Retrying: Validation failed: " . join(', ', @validation_warnings) . "\n";
|
||||||
|
} else {
|
||||||
|
# Success: HTTP 2xx, JSON parsed, and content passed validation
|
||||||
|
last;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
# Log failure if not success and retry
|
||||||
|
warn "Retrying: HTTP failed with status $status\n";
|
||||||
}
|
}
|
||||||
print STDERR STATUS("- attempt $loops: $status");
|
|
||||||
sleep 5;
|
sleep 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,6 +153,9 @@ sub validate {
|
|||||||
my @warnings;
|
my @warnings;
|
||||||
if ($type eq 'artist') {
|
if ($type eq 'artist') {
|
||||||
# make sure there are albums
|
# make sure there are albums
|
||||||
unless (exists $json->{Albums} and scalar @{$json->{Albums}}) {
|
unless (exists $json->{albums} and scalar @{$json->{albums}}) {
|
||||||
push(@warnings, 'no albums');
|
push(@warnings, 'no albums');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return @warnings;
|
||||||
|
}
|
Reference in New Issue
Block a user