用 cmdbook 按順序啟動開發服務,等候各項服務就緒
Start your dev stack in order, waiting until each service is ready
cmdbook 讓用戶設定服務之間的依賴關係,按順序啟動開發服務,並等候各項服務達到指定就緒條件後才啟動下一項。每項服務的輸出會分開顯示;服務未能就緒時,cmdbook 會停止啟動流程,資料庫停止後,依賴它的服務亦會停止。依賴功能屬於 cmdbook Pro。
Your project needs four things running: a database, a job server, an API, and the web app. In that order.
How do you do it?
You open four terminal tabs and start them one by one. Start the API too early, and it fails because the database isn't ready yet. Then you spend ten minutes reading the wrong error.
So you ask an AI for a start.sh. It gives you four lines with & and a sleep 5 between them. Some days 5 seconds is too short. Some days it's 20 seconds of waiting for nothing.
You ask for real waiting, and the script grows: until nc -z localhost 17233 for the job server, until curl -sf localhost:18787/health for the API, a grep on a log file for the database. All four logs go to the same terminal, mixed together. And when you press Ctrl-C, the background services keep running, because a script's background jobs ignore Ctrl-C. Next morning, port 15173 is taken.
With cmdbook, each service is its own command, and the order is something you declare, not something you script.
Here's how
1. Save each service once. Four commands in one folder: postgres, jobs, api, web. Each is just the line that starts it.
2. Declare what each one waits for. In each command's dependencies, pick the command it needs and how to tell it's ready:
web
depends on → api ready when: http://127.0.0.1:18787/health returns 200
api
depends on → postgres ready when: the output says "ready to accept connections"
depends on → jobs ready when: port 17233 is open
jobs
depends on → postgres ready when: the output says "ready to accept connections"
3. Click Run on web. cmdbook starts postgres first, waits for its log line, then jobs, waits for the port, then api, waits for /health, and only then starts web. Whatever is already running is reused, not started twice.
4. Read each log on its own. Every service has its own output tab. If one doesn't come up in time, cmdbook stops there and shows you which one, instead of starting the web app against a dead API. If the database dies later, the services that depend on it are stopped too.
From a terminal, it's the same thing:
cmdbook run "Dev stack/web"
Stopping is one click too, and nothing is left holding a port.
The previous parts of this series use cmdbook's free features. Dependencies are part of cmdbook Pro.
Try cmdbook here: printf.app/cmdbook
來源:DEV Community · dev.to
