Skip to content

Commit

Permalink
Wait and retry when sqlite database is locked
Browse files Browse the repository at this point in the history
Signed-off-by: Joonas Rautiola <[email protected]>
  • Loading branch information
joinemm committed Jul 26, 2024
1 parent 0b19e05 commit d35e16c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
18 changes: 16 additions & 2 deletions src/sbomnix/cpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

# pylint: disable=too-few-public-methods

""" Generate CPE (Common Platform Enumeration) identifiers"""
"""Generate CPE (Common Platform Enumeration) identifiers"""

import sys
import string
import time
from sqlite3 import OperationalError
from dfdiskcache import DataFrameDiskCache
from common.utils import (
LOG,
Expand Down Expand Up @@ -43,7 +45,19 @@ def __init__(self):
"Failed downloading cpedict: CPE information might not be accurate"
)
else:
self.cache.set(_CPE_CSV_URL, self.df_cpedict, ttl=_CPE_CSV_CACHE_TTL)
waiting = True
while waiting:
try:
self.cache.set(
_CPE_CSV_URL, self.df_cpedict, ttl=_CPE_CSV_CACHE_TTL
)
waiting = False
except OperationalError:
LOG.warning(
"CPE Sqlite database is locked! Retrying in 1 second"
)
time.sleep(1)

if self.df_cpedict is not None:
# Verify the loaded cpedict contains at least the following columns
required_cols = {"vendor", "product"}
Expand Down
12 changes: 11 additions & 1 deletion src/sbomnix/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

"""Cache nixpkgs meta information"""

import time
import os
import re
import logging

from sqlite3 import OperationalError
import pandas as pd
from dfdiskcache import DataFrameDiskCache
from nixmeta.scanner import NixMetaScanner, nixref_to_nixpkgs_path
Expand All @@ -33,7 +35,15 @@ class Meta:
"""Cache nixpkgs meta information"""

def __init__(self):
self.cache = DataFrameDiskCache(cache_dir_path=DFCACHE_PATH)
waiting = True
while waiting:
try:
self.cache = DataFrameDiskCache(cache_dir_path=DFCACHE_PATH)
waiting = False
except OperationalError:
LOG.warning("DFCACHE Sqlite database is locked! Retrying in 1 second")
time.sleep(1)

# df_nixmeta includes the meta-info from _NIXMETA_CSV_URL
self.df_nixmeta = self.cache.get(_NIXMETA_CSV_URL)
if self.df_nixmeta is not None and not self.df_nixmeta.empty:
Expand Down

0 comments on commit d35e16c

Please sign in to comment.