Skip to content

Commit

Permalink
feat(Django): daily DB backup task (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Aug 8, 2024
1 parent 886a2d0 commit ecd3c5d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
31 changes: 31 additions & 0 deletions open_prices/common/tasks.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
import os
from pathlib import Path

from django.conf import settings
from django_q.models import Schedule
from django_q.tasks import schedule
from openfoodfacts import Flavor

from open_prices.api.locations.serializers import LocationSerializer
from open_prices.api.prices.serializers import PriceSerializer
from open_prices.api.proofs.serializers import ProofSerializer
from open_prices.common.openfoodfacts import import_product_db
from open_prices.common.utils import export_model_to_jsonl_gz
from open_prices.locations.models import Location
from open_prices.prices.models import Price
from open_prices.proofs.models import Proof


def import_product_db_task():
for flavor in [Flavor.off, Flavor.obf, Flavor.opff, Flavor.opf]:
import_product_db(flavor=flavor)


def dump_db_task():
output_dir = Path(os.path.join(settings.BASE_DIR, "static/data"))

for table_name, model_class, schema_class in (
("prices", Price, PriceSerializer),
("proofs", Proof, ProofSerializer),
("locations", Location, LocationSerializer),
):
export_model_to_jsonl_gz(table_name, model_class, schema_class, output_dir)


# sync product database with Open Food Facts daily at 15:00
# https://cron.help/#0_15_*_*_*
schedule(
Expand All @@ -18,3 +40,12 @@ def import_product_db_task():
schedule_type=Schedule.CRON,
cron="0 15 * * *",
)

# dump the database as JSONL files to the data directory daily at 23:00
# https://cron.help/#0_23_*_*_*
schedule(
"open_prices.common.tasks.dump_db_task",
name="dump_db_task",
schedule_type=Schedule.CRON,
cron="0 23 * * *",
)
14 changes: 14 additions & 0 deletions open_prices/common/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import gzip
import json

import tqdm


def is_float(string):
try:
float(string)
Expand All @@ -19,3 +25,11 @@ def add_validation_error(dict, key, value):
if type(dict[key]) is str:
dict[key] = [dict[key], value]
return dict


def export_model_to_jsonl_gz(table_name, model_class, schema_class, output_dir):
output_path = output_dir / f"{table_name}.jsonl.gz"
with gzip.open(output_path, "wt") as f:
for item in tqdm.tqdm(model_class.objects.all(), desc=table_name):
f.write(json.dumps(schema_class(item).data))
f.write("\n")

0 comments on commit ecd3c5d

Please sign in to comment.