Goal: Based on the current logseq-cli and db-worker-node implementations, refactor db-worker-node to be repo-bound with locking, make logseq-cli fully manage db-worker-node lifecycle, and add server subcommands for management.
db-worker-node currently accepts --repo but it is optional; it can open/switch graphs via thread-api/create-or-open-db at runtime. Entrypoint: src/main/frontend/worker/db_worker_node.cljs.logseq-cli connects to an existing db-worker-node via base-url; it does not start/stop the server. Entrypoint: src/main/logseq/cli/main.cljs, src/main/logseq/cli/commands.cljs, src/main/logseq/cli/transport.cljs.src/test/logseq/cli/integration_test.cljs, src/test/frontend/worker/db_worker_node_test.cljs).db-worker-node: startup must require --repo; on startup it must open or create that graph; it must not switch graphs at runtime; it must create a lock file so a graph can be served by only one db-worker-node instance; it only needs to bind to localhost.logseq-cli, all commands requiring --repo or any graph operations must connect to or create the corresponding db-worker-node server.server subcommand(s) to logseq-cli for managing db-worker-node servers.server list|status|start|stop|restart (or similar) to manage servers explicitly.Files:
src/main/frontend/worker/db_worker_node.cljssrc/main/frontend/worker/platform/node.cljs (for data-dir / repo dir resolution)src/main/frontend/worker/db_worker_node_lock.cljsKey changes:
--repo becomes required. If missing, print help and exit non-zero. Host binding is restricted to localhost (e.g., 127.0.0.1) regardless of input.<maybe-open-repo! with a required create-or-open-db for the repo; store bound-repo./v1/invoke, for repo-scoped thread APIs, validate args[0] matches bound-repo.thread-api/create-or-open-db, thread-api/unsafe-unlink-db, etc. when repo differs.:repo-mismatch error shape.~/.logseq/db-worker/.logseq-pool-<graph>/db-worker.lock).{repo, pid, host, port, startedAt}.fs.open with wx) or atomic temp + rename. If exists, fail with “graph already locked”.stop!) and on SIGINT/SIGTERM.Files:
src/main/logseq/cli/commands.cljssrc/main/logseq/cli/main.cljssrc/main/logseq/cli/config.cljssrc/main/logseq/cli/server.cljs (process management + lock handling)Key changes:
--repo or resolved repo from config; otherwise error.ensure-server!):
/healthz + /readyz.config :base-url dynamically.child_process.spawn: node ./static/db-worker-node.js --repo <repo> --data-dir <...> --host 127.0.0.1 --port 0.--base-url is provided, decide if it is ignored or overrides orchestration (see Compatibility section).server subcommandsSuggested command group:
server list: list servers from lock files (repo, pid, port, status).server start --repo <name>: start server for repo.server stop --repo <name>: stop server (SIGTERM or /v1/shutdown).server restart --repo <name>: stop + start.Implementation notes:
start|stop|restart require --repo.list scans data-dir for repo directories, reads lock files, and verifies status./v1/shutdown in db-worker-node for graceful stop.src/test/logseq/cli/*).src/test/frontend/worker/db_worker_node_test.cljs).src/test/logseq/cli/integration_test.cljs).ensure-server! with lock/health checks and spawning.server subcommands.docs/cli/logseq-cli.md).graph list require --repo? If not, define a “global” server or out-of-band access to data-dir.
db-worker.lock,~/.logseq/db-worker/.logseq-pool-<graph>/db-worker.lock)./v1/shutdown for graceful stop vs. SIGTERM from CLI?logseq-cli server stop is invoked or the process exits unexpectedly; in the latter case, CLI should handle lockfile cleanup on restart.