Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checks for active database #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 53 additions & 41 deletions etl/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,85 @@
class PGDatabase(DatabaseStore):
"""Implements the DatabaseStore interface using Minio as the backend service"""
def __init__(self):
self._database = PoolDatabase(f'postgres://{settings.database_user}:{settings.database_password}@{settings.database_host}/{settings.database_db}')
self._table_manager = TableManager(self._database, 'files', pk_field='id', hooks=None)
self._database = None
try:
self._database = PoolDatabase(f'postgres://{settings.database_user}:{settings.database_password}@{settings.database_host}/{settings.database_db}')
self._table_manager = TableManager(self._database, 'files', pk_field='id', hooks=None)
except(DatabaseError, InvalidPasswordError) as db_error:
return

async def create_table(self) -> bool:
""" Check for and create database table """
LOGGER.info('Creating DB table...')
try:
await self._database.init_pool()
conn = await self._database.get_connection()
await conn.execute(
"""
CREATE TABLE IF NOT EXISTS files (
id uuid PRIMARY KEY,
bucket_name text,
file_name text,
status text,
processing_status text,
original_filename text,
event_name text,
source_ip text,
size int,
etag text,
content_type text,
create_datetime timestamp with time zone,
update_datetime timestamp with time zone,
classification jsonb,
metadata jsonb
);
"""
)
await conn.close()
return True
if self._database:
await self._database.init_pool()
conn = await self._database.get_connection()
await conn.execute(
"""
CREATE TABLE IF NOT EXISTS files (
id uuid PRIMARY KEY,
bucket_name text,
file_name text,
status text,
processing_status text,
original_filename text,
event_name text,
source_ip text,
size int,
etag text,
content_type text,
create_datetime timestamp with time zone,
update_datetime timestamp with time zone,
classification jsonb,
metadata jsonb
);
"""
)
await conn.close()
return True
return False
except (DatabaseError, InvalidPasswordError) as db_error:
LOGGER.info('Database not active. Exception: %s', db_error)
return False

async def insert_file(self, filedata: FileObject):
""" Track a new file from Minio"""
LOGGER.info("Inserting file into DB...")
await self._database.insert('files', dict(filedata))
if self._database:
LOGGER.info("Inserting file into DB...")
await self._database.insert('files', dict(filedata))

async def move_file(self, rowid: str, new_name: str):
""" Track the moving of a file in Minio"""
rec_data = {}
rec_data['path'] = new_name
rec_data['update_datetime'] = f'{datetime.now().isoformat()}Z'
await self._table_manager.update(rowid, rec_data)
if self._database:
rec_data = {}
rec_data['path'] = new_name
rec_data['update_datetime'] = f'{datetime.now().isoformat()}Z'
await self._table_manager.update(rowid, rec_data)

async def delete_file(self, rowid: str):
""" Track the deleting of a file in Minio"""
await self._table_manager.delete(rowid)
if self._database:
await self._table_manager.delete(rowid)

async def list_files(self, metadata: Optional[Dict]):
""" List all tracked files by provided filter """
return await self._table_manager.list(filters=metadata)
if self._database:
return await self._table_manager.list(filters=metadata)

async def retrieve_file_metadata(self, rowid: str):
""" Retrieve a row based on ID """
return await self._table_manager.detail(rowid)
if self._database:
return await self._table_manager.detail(rowid)

async def update_status(self, rowid: str, new_status: str, new_filename: str):
""" Update the file status/state """
rec_data = {}
rec_data['status'] = new_status
rec_data['file_name'] = new_filename
rec_data['update_datetime'] = datetime.now()
await self._table_manager.update(rowid, rec_data)
if self._database:
rec_data = {}
rec_data['status'] = new_status
rec_data['file_name'] = new_filename
rec_data['update_datetime'] = datetime.now()
await self._table_manager.update(rowid, rec_data)

def parse_notification(self, evt_data: Any):
""" Parse a Minio notification to create a DB row """
Expand Down
9 changes: 6 additions & 3 deletions etl/event_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ async def _file_put(self, object_id: ObjectId, uuid: str) -> bool:
metadata = self._object_store.retrieve_object_metadata(object_id)
metadata['status'] = 'Processing'
self._object_store.move_object(object_id, processing_file, metadata)
await self._database.update_status(uuid, 'Processing', processing_file.path)
if self._database_active:
await self._database.update_status(uuid, 'Processing', processing_file.path)

with tempfile.TemporaryDirectory() as work_dir:
# Download to local temp working directory
Expand Down Expand Up @@ -295,13 +296,15 @@ async def _file_put(self, object_id: ObjectId, uuid: str) -> bool:
if archive_file:
metadata['status'] = 'Success'
self._object_store.move_object(processing_file, archive_file, metadata)
await self._database.update_status(uuid, 'Success', archive_file.path)
if self._database_active:
await self._database.update_status(uuid, 'Success', archive_file.path)
self._message_producer.job_evt_status(job_id, 'success')
else:
# Failure. mv to failed
metadata['status'] = 'Failed'
self._object_store.move_object(processing_file, error_file, metadata)
await self._database.update_status(uuid, 'Failed', error_file.path)
if self._database_active:
await self._database.update_status(uuid, 'Failed', error_file.path)
self._message_producer.job_evt_status(job_id, 'failure')

# Optionally save error log to failed, use same metadata as original file
Expand Down