Skip to content

Commit

Permalink
fix: change redis lib
Browse files Browse the repository at this point in the history
and better (not perfect) handling of redis connection
  • Loading branch information
alexgarel committed Oct 13, 2022
1 parent 1c09ad5 commit 95e9192
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ requires 'JSON::Create';
requires 'JSON::Parse';
requires 'Data::DeepAccess';
requires 'XML::XML2JSON';
requires 'Redis::Client';
requires 'Redis';


# Mojolicious/Minion
Expand Down
4 changes: 2 additions & 2 deletions lib/ProductOpener/Products.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1179,11 +1179,11 @@ sub store_product($user_id, $product_ref, $comment) {
store("$new_data_root/products/$path/changes.sto", $changes_ref);
log_change($product_ref, $change_ref);

$log->debug("store_product - done", { code => $code, product_id => $product_id } ) if $log->is_debug();

# index for search service
push_to_search_service($product_ref);

$log->debug("store_product - done", { code => $code, product_id => $product_id } ) if $log->is_debug();

return 1;
}

Expand Down
104 changes: 86 additions & 18 deletions lib/ProductOpener/Redis.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@

=head1 NAME
ProductOpener::Redis - functions to push informations to redis
=head1 DESCRIPTION
C<ProductOpener::Redit> is handling pushing info to Redis
to communicate updates to openfoodfacts-search instance
=cut

package ProductOpener::Redis;

use ProductOpener::PerlStandards;
Expand All @@ -14,39 +25,96 @@ BEGIN {

use vars @EXPORT_OK;

use Log::Any qw($log);
use ProductOpener::Config2;
use Redis::Client;
use Log::Any qw/$log/;
use ProductOpener::Config2 qw/$redis_url/;
use Redis;

=head2 $redis_client
The connection to redis
=cut
my $redis_client;


sub init_redis() {
=head2 init_redis($is_reconnect=0)
$log->debug("init_redis", {redis_url_env => $ENV{REDIS_URL}, redis_url => $ProductOpener::Config2::redis_url})
init $redis_client or re-init it if we where disconnected
it is uses ProductOpener::Config2::redis_url
=head3 Arguments
=head4 bool $is_reconnect
This is informative.
If true we will display a warning if we have no redis_url.
=cut

sub init_redis($is_reconnect=0) {
$log->debug("init_redis", {redis_url => $redis_url})
if $log->is_debug();

$log->warn("REDIS_URL env", {redis_url => $ENV{REDIS_URL}}) if $log->is_warn();
if ($ProductOpener::Config2::redis_url eq "") {
$log->warn("Redis URL not provided for search indexing", {error => $@}) if $log->is_warn();
return;
if (((! defined $redis_url) || ($redis_url eq "")) && ! $is_reconnect) {
$log->warn("Redis URL not provided for search indexing") if $log->is_warn();
}
my $redis_client;
eval {$redis_client = Redis::Client->new(host => $ProductOpener::Config2::redis_url);};
eval {
$redis_client = Redis->new(
server => $redis_url,
# we don't want to sacrifice too much performance for redis problems
cnx_timeout => 1,
write_timeout => 1,
);
};
if ($@) {
$log->warn("Error connecting to Redis", {error => $@}) if $log->is_warn();
$redis_client = -1; # this ask for eventual reconnection
}
else {
return $redis_client;
}
return;
}

my $redis_client = init_redis();

=head2 push_to_search_service ($product_ref)
Inform openfoodfacts-search that a product was updated.
It uses Redis to do that.
=head3 Arguments
=head4 Product Object $product_ref
The product that was updated.
=cut

sub push_to_search_service ($product_ref) {
if (defined($redis_client)) {
eval {$redis_client->rpush('search_import_queue', $product_ref->{code});};

if (!$redis_url) {
# off search not activated
return;
}

my $error = "";
if ((!defined $redis_client) || ($redis_client == -1)) {
# we where deconnected, try again
$log->info("Trying to reconnect to redis");
init_redis(defined $redis_client);
}
if (defined($redis_client) && ($redis_client != -1)) {
eval {
$redis_client->rpush('search_import_queue', $product_ref->{code});
};
$error = $@;
} else {
$error = "Can't connect to redis";
}
if (!($error eq "")) {
$log->warn("Failed to push to redis",
{product_code=> $product_ref->{code}, error => $error}
) if $log->is_warn();
# ask for eventual reconnection for next call
$redis_client = -1;
}

return;
}


1;

0 comments on commit 95e9192

Please sign in to comment.