Skip to content

Commit

Permalink
Connect to Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
simonj2 committed Aug 23, 2022
1 parent da08d72 commit 1e7d429
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ MONGODB_CACHE_SIZE=8 # GB
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=test
ROBOTOFF_URL=http://api:5500 # connect to Robotoff running in separate docker-compose deployment
REDIS_URL=searchredis:6379 # Redis runs in a separate docker-compose deployment
GOOGLE_CLOUD_VISION_API_KEY=
CROWDIN_PROJECT_IDENTIFIER=
CROWDIN_PROJECT_KEY=
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/container-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ jobs:
echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> .env
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> .env
echo "ROBOTOFF_URL=${{ secrets.ROBOTOFF_URL }}" >> .env
echo "REDIS_URL=${{ secrets.REDIS_URL }}" >> .env
echo "GOOGLE_CLOUD_VISION_API_KEY=${{ secrets.GOOGLE_CLOUD_VISION_API_KEY }}" >> .env
echo "CROWDIN_PROJECT_IDENTIFIER=${{ secrets.CROWDIN_PROJECT_IDENTIFIER }}" >> .env
echo "CROWDIN_PROJECT_KEY=${{ secrets.CROWDIN_PROJECT_KEY }}" >> .env
Expand Down
1 change: 1 addition & 0 deletions conf/apache.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ PerlPassEnv PRODUCT_OPENER_DOMAIN
PerlPassEnv PRODUCT_OPENER_PORT
PerlPassEnv PRODUCERS_PLATFORM
PerlPassEnv ROBOTOFF_URL
PerlPassEnv REDIS_URL
PerlPassEnv MONGODB_HOST
PerlPassEnv GOOGLE_CLOUD_VISION_API_KEY
PerlPassEnv CROWDIN_PROJECT_IDENTIFIER
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ requires 'JSON::Create';
requires 'JSON::Parse';
requires 'Data::DeepAccess';
requires 'XML::XML2JSON';
requires 'Redis::Client';


# Mojolicious/Minion
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ x-backend-conf: &backend-conf
- POSTGRES_USER
- POSTGRES_PASSWORD
- ROBOTOFF_URL
- REDIS_URL
- GOOGLE_CLOUD_VISION_API_KEY
- CROWDIN_PROJECT_IDENTIFIER
- CROWDIN_PROJECT_KEY
Expand Down
1 change: 1 addition & 0 deletions docs/introduction/dev-environment-quick-start-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ The `.env` file contains ProductOpener default settings:
| `PRODUCT_OPENER_FLAVOR_SHORT` | can be modified to run different flavors of OpenFoodFacts, amongst `off` (default), `obf`, `oppf`, `opf`.|
| `PRODUCERS_PLATFORM` | can be set to `1` to build / run the **producer platform**.|
| `ROBOTOFF_URL` | can be set to **connect with a Robotoff instance**.|
| `REDIS_URL` | can be set to **connect with a Redis instance for populating the search index**.|
| `GOOGLE_CLOUD_VISION_API_KEY` | can be set to **enable OCR using Google Cloud Vision**.|
| `CROWDIN_PROJECT_IDENTIFIER` and `CROWDIN_PROJECT_KEY` | can be set to **run translations**.|
| `GEOLITE2_PATH`, `GEOLITE2_ACCOUNT_ID` and `GEOLITE2_LICENSE_KEY` | can be set to **enable Geolite2**.|
Expand Down
4 changes: 4 additions & 0 deletions lib/ProductOpener/Config2_docker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ BEGIN
$crowdin_project_identifier
$crowdin_project_key
$robotoff_url
$redis_url
%server_options
);
%EXPORT_TAGS = (all => [@EXPORT_OK]);
Expand Down Expand Up @@ -93,6 +94,9 @@ my $postgres_url = "postgresql://${postgres_user}:${postgres_password}\@${postgr
# enable an in-site robotoff-asker in the product page
$robotoff_url = $ENV{ROBOTOFF_URL};

# Set this to your instance of the search service to enable writes to it
$redis_url = $ENV{REDIS_URL};

%server_options = (
private_products => $producers_platform, # 1 to make products visible only to the owner (producer platform)
producers_platform => $producers_platform,
Expand Down
3 changes: 3 additions & 0 deletions lib/ProductOpener/Config2_sample.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ BEGIN
$crowdin_project_identifier
$crowdin_project_key
$robotoff_url
$redis_url
%server_options
Expand Down Expand Up @@ -74,6 +75,8 @@ $crowdin_project_key = '';
# enable an in-site robotoff-asker in the product page
$robotoff_url = '';

$redis_url = '';

%server_options = (

cookie_domain => "openfoodfacts.dev", # if not set, default to $server _domain
Expand Down
15 changes: 15 additions & 0 deletions lib/ProductOpener/Products.pm
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ use Data::DeepAccess qw(deep_get);
use LWP::UserAgent;
use Storable qw(dclone);

use Redis::Client;

use Algorithm::CheckDigits;
my $ean_check = CheckDigits('ean');

Expand Down Expand Up @@ -1175,6 +1177,9 @@ sub store_product($user_id, $product_ref, $comment) {
store("$new_data_root/products/$path/changes.sto", $changes_ref);
log_change($product_ref, $change_ref);

# index for search service
index_search_service($product_ref);

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

return 1;
Expand Down Expand Up @@ -2411,6 +2416,16 @@ sub index_product($product_ref)
return;
}

sub index_search_service($product_ref) {
# Now send the barcode to Redis so that it can be indexed
eval {
my $redis_client = Redis::Client->new(host => $ProductOpener::Config2::redis_url);
$redis_client->rpush('search_import_queue', $product_ref->{code});
};
if ($@) {
$log->warn("Error connecting to Redis", { error => $@ }) if $log->is_warn();
}
}

sub compute_codes($product_ref) {

Expand Down

0 comments on commit 1e7d429

Please sign in to comment.