Skip to content

Commit f2cdca7

Browse files
Address comments in PR #373
* Use logger.error when handling custom bulk exceptions * Call ctx.exit in bulk_update_embeddings when BulkOperationError is raisd * Update method to log elapsed time * Clean up log when refreshing index
1 parent 67c064b commit f2cdca7

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

tim/cli.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,13 @@ def main(ctx: click.Context, url: str, *, verbose: bool) -> None:
6363
ctx.obj["CLIENT"] = tim_os.configure_opensearch_client(url)
6464
logger.info("OpenSearch client configured for endpoint '%s'", url)
6565

66+
def _log_command_elapsed_time() -> None:
67+
elapsed_time = perf_counter() - ctx.obj["start_time"]
68+
logger.info(
69+
"Total time to complete process: %s", str(timedelta(seconds=elapsed_time))
70+
)
6671

67-
@main.result_callback()
68-
@click.pass_context
69-
def log_process_time(ctx: click.Context, _result: object, **_kwargs: dict) -> None:
70-
elapsed_time = perf_counter() - ctx.obj["START_TIME"]
71-
logger.info(
72-
"Total time to complete process: %s", str(timedelta(seconds=elapsed_time))
73-
)
72+
ctx.call_on_close(_log_command_elapsed_time)
7473

7574

7675
# Cluster commands
@@ -310,7 +309,7 @@ def bulk_update(
310309
try:
311310
index_results.update(tim_os.bulk_index(client, index, records_to_index))
312311
except BulkIndexingError as exception:
313-
logger.info(f"Bulk indexing failed: {exception}")
312+
logger.error(f"Bulk indexing failed: {exception}") # noqa: TRY400
314313

315314
# bulk delete records
316315
records_to_delete = td.read_dicts_iter(
@@ -343,7 +342,7 @@ def bulk_update(
343342
"records with embeddings."
344343
),
345344
)
346-
@click.option("-rid", "--run-id", help="Run ID.")
345+
@click.option("-rid", "--run-id", required=True, help="Run ID.")
347346
@click.argument("dataset_path", type=click.Path())
348347
@click.pass_context
349348
def bulk_update_embeddings(
@@ -391,7 +390,8 @@ def bulk_update_embeddings(
391390
try:
392391
update_results.update(tim_os.bulk_update(client, index, embeddings_to_index))
393392
except BulkOperationError as exception:
394-
logger.info(f"Bulk update with embeddings failed: {exception}")
393+
logger.error(f"Bulk update with embeddings failed: {exception}") # noqa: TRY400
394+
ctx.exit(1)
395395

396396
logger.info(f"Bulk update with embeddings complete: {json.dumps(update_results)}")
397397

@@ -471,7 +471,3 @@ def reindex_source(
471471

472472
summary_results = {"index": index_results}
473473
logger.info(f"Reindex source complete: {json.dumps(summary_results)}")
474-
475-
476-
if __name__ == "__main__":
477-
main()

tim/opensearch.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def bulk_delete(
352352
result["total"] += 1
353353
if result["total"] % int(os.getenv("STATUS_UPDATE_INTERVAL", "1000")) == 0:
354354
logger.info("Status update: %s records deleted so far!", result["total"])
355-
logger.info("All records deleted, refreshing index.")
355+
logger.info("Refreshing index.")
356356
response = client.indices.refresh(
357357
index=index,
358358
)
@@ -412,7 +412,7 @@ def bulk_index(client: OpenSearch, index: str, records: Iterator[dict]) -> dict[
412412
result["total"] += 1
413413
if result["total"] % int(os.getenv("STATUS_UPDATE_INTERVAL", "1000")) == 0:
414414
logger.info("Status update: %s records indexed so far!", result["total"])
415-
logger.info("All records ingested, refreshing index.")
415+
logger.info("Refreshing index.")
416416
response = client.indices.refresh(
417417
index=index,
418418
)
@@ -467,4 +467,9 @@ def bulk_update(
467467
result["total"] += 1
468468
if result["total"] % int(os.getenv("STATUS_UPDATE_INTERVAL", "1000")) == 0:
469469
logger.info("Status update: %s records updated so far!", result["total"])
470+
logger.info("Refreshing index.")
471+
response = client.indices.refresh(
472+
index=index,
473+
)
474+
logger.debug(response)
470475
return result

0 commit comments

Comments
 (0)