Skip to content

Commit d03a584

Browse files
authored
feat(cli): add more hints for live mode and clarify existing ones (#702)
1 parent 98e4c73 commit d03a584

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

python/cocoindex/cli.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import atexit
2+
import asyncio
23
import datetime
34
import importlib.util
45
import os
@@ -18,6 +19,7 @@
1819

1920
from . import flow, lib, setting
2021
from .setup import flow_names_with_setup
22+
from .runtime import execution_context
2123

2224
# Create ServerSettings lazily upon first call, as environment variables may be loaded from files, etc.
2325
COCOINDEX_HOST = "https://cocoindex.io"
@@ -267,6 +269,21 @@ def _setup_flows(
267269
setup_bundle.apply(report_to_stdout=not quiet)
268270

269271

272+
def _show_no_live_update_hint() -> None:
273+
click.secho(
274+
"NOTE: No change capture mechanism exists. See https://cocoindex.io/docs/core/flow_methods#live-update for more details.\n",
275+
fg="yellow",
276+
)
277+
278+
279+
async def _update_all_flows_with_hint_async(
280+
options: flow.FlowLiveUpdaterOptions,
281+
) -> None:
282+
await flow.update_all_flows_async(options)
283+
if options.live_mode:
284+
_show_no_live_update_hint()
285+
286+
270287
@cli.command()
271288
@click.argument("app_target", type=str)
272289
@click.option(
@@ -398,7 +415,7 @@ def update(
398415
setup: bool, # pylint: disable=redefined-outer-name
399416
force: bool,
400417
quiet: bool,
401-
) -> Any:
418+
) -> None:
402419
"""
403420
Update the index to reflect the latest data from data sources.
404421
@@ -410,8 +427,8 @@ def update(
410427

411428
if live:
412429
click.secho(
413-
"NOTE: Flow code changes will NOT be reflected in the server until you restart it.",
414-
fg="red",
430+
"NOTE: Flow code changes will NOT be reflected until you restart to load the new code.\n",
431+
fg="yellow",
415432
)
416433

417434
options = flow.FlowLiveUpdaterOptions(live_mode=live, print_stats=not quiet)
@@ -422,14 +439,15 @@ def update(
422439
force=force,
423440
quiet=quiet,
424441
)
425-
return flow.update_all_flows(options)
442+
execution_context.run(_update_all_flows_with_hint_async(options))
426443
else:
427444
fl = flow.flow_by_name(flow_name)
428445
if setup:
429446
_setup_flows((fl,), force=force, quiet=quiet)
430447
with flow.FlowLiveUpdater(fl, options) as updater:
431448
updater.wait()
432-
return updater.update_stats()
449+
if options.live_mode:
450+
_show_no_live_update_hint()
433451

434452

435453
@cli.command()
@@ -604,8 +622,8 @@ def server(
604622
)
605623
else:
606624
click.secho(
607-
"NOTE: Flow code changes will NOT be reflected in the server until you restart it. Use --reload to enable auto-reload.",
608-
fg="red",
625+
"NOTE: Flow code changes will NOT be reflected until you restart to load the new code. Use --reload to enable auto-reload.\n",
626+
fg="yellow",
609627
)
610628
_run_server(*args)
611629

@@ -650,17 +668,19 @@ def _run_server(
650668
quiet=quiet,
651669
)
652670

653-
if live_update:
654-
options = flow.FlowLiveUpdaterOptions(live_mode=True, print_stats=not quiet)
655-
flow.update_all_flows(options)
656-
657671
lib.start_server(server_settings)
658672

659673
if COCOINDEX_HOST in cors_origins:
660674
click.echo(f"Open CocoInsight at: {COCOINDEX_HOST}/cocoinsight")
661675

662676
click.secho("Press Ctrl+C to stop the server.", fg="yellow")
663677

678+
if live_update:
679+
options = flow.FlowLiveUpdaterOptions(live_mode=True, print_stats=not quiet)
680+
asyncio.run_coroutine_threadsafe(
681+
_update_all_flows_with_hint_async(options), execution_context.event_loop
682+
)
683+
664684
shutdown_event = threading.Event()
665685

666686
def handle_signal(signum: int, frame: FrameType | None) -> None:

0 commit comments

Comments
 (0)