from datetime import datetime, timedelta from time import sleep import click from click import secho from delphi_es_utils.core.utils import printf_code, printf_keyval from delphi_es_utils.errors.exceptions import IndexManagerException from delphi_es_utils.services import IndexManager from delphi_es_utils.settings import ENVIRONMENT, ES_REINDEX_SCROLL_SIZE @click.command('reindex', short_help=f'Start a reindexing task.') @click.option('--name', required=True, help='Name of the source alias.') @click.option('--env', default=ENVIRONMENT, help='Environment override') def reindex_cmd(name: str, env=ENVIRONMENT): secho(f'Running reindex with settings:') printf_keyval('Scroll Size', ES_REINDEX_SCROLL_SIZE) reindexer = IndexManager(name) try: reindexer.reindex() except IndexManagerException as e: secho('**✘ Reindexing task failed to start!**') secho(str(e)) exit(1) except Exception as e: secho('**✘ Reindexing task failed to start!**') printf_code(e) exit(1) secho('**✔︎ Reindexing task started successfully 🍻**\n') # sleep a few seconds to get a better estimate for completion sleep(5) result = reindexer.task_result if result: task_id = reindexer.full_task_id secho(f'Here is the current task summary for task ID `{task_id}`') printf_keyval('Total', result.total) printf_keyval('Processed', result.processed) printf_keyval('Progress', f'{result.progress}%') printf_keyval('Completed?', result.completed) if not result.completed: time_remaining = timedelta(seconds=result.time_remaining) estimated_completion = datetime.utcnow() + time_remaining printf_keyval('Est. Time Remaining', str(time_remaining)) printf_keyval('Est. Completion (UTC)', estimated_completion.strftime('%c')) secho('\n') secho(f'To get the **status** of this running task again, run the following command:') command_str = f'task-status --task {task_id} --env {env}' printf_code(command_str) secho(f'To **cancel** this reindexing task, run the following command:') cancel_cmd = f'cancel-reindex --task {task_id} --env {env}' printf_code(cancel_cmd) secho(f'To **finalize** the reindexing process after completion, run the following:') post_cmd = f'post-reindex --name {name} --task {task_id} --env {env}' printf_code(post_cmd) exit(0)