docs: document MarkBase service management issue with launchd
- MarkBase is managed by launchd, auto-restarts after kill - Stop API kills process but launchd restarts immediately - Start API fails because port already in use - Need to integrate launchctl commands into service API - Documented problem analysis and proposed solutions
This commit is contained in:
@@ -0,0 +1,195 @@
|
|||||||
|
---
|
||||||
|
title: MarkBase Service Management Issue
|
||||||
|
version: 1.0
|
||||||
|
date: 2026-10-24
|
||||||
|
author: OpenCode
|
||||||
|
status: identified
|
||||||
|
---
|
||||||
|
|
||||||
|
## 問題描述
|
||||||
|
|
||||||
|
在 Admin Settings 中點擊 MarkBase 的 Stop 按鈕後,無法成功 Start。
|
||||||
|
|
||||||
|
## 根本原因
|
||||||
|
|
||||||
|
**MarkBase 由 launchd 管理,會自動重啟**:
|
||||||
|
|
||||||
|
```
|
||||||
|
~/Library/LaunchAgents/com.momentry.markbase.plist
|
||||||
|
```
|
||||||
|
|
||||||
|
當前狀態:
|
||||||
|
```bash
|
||||||
|
$ launchctl list | grep markbase
|
||||||
|
30490 0 com.momentry.markbase
|
||||||
|
```
|
||||||
|
|
||||||
|
### 問題流程
|
||||||
|
|
||||||
|
1. **Stop 操作**:
|
||||||
|
- AdminView 調用 `/api/v2/service/stop` → MarkBase (11438)
|
||||||
|
- MarkBase 的 `service_stop_handler` 執行:`lsof -ti :11438 | xargs kill -9`
|
||||||
|
- 進程被殺掉,但 launchd 立即重啟
|
||||||
|
- 結果:看起來像沒有停止
|
||||||
|
|
||||||
|
2. **Start 操作**:
|
||||||
|
- AdminView 調用 `/api/v2/service/start` → MarkBase (11438)
|
||||||
|
- MarkBase 的 `service_start_handler` 執行:`cargo run --bin markbase-core -- web-start --port 11438`
|
||||||
|
- 但進程已存在(由 launchd 重啟),端口被占用
|
||||||
|
- 結果:啟動失敗
|
||||||
|
|
||||||
|
## 架構說明
|
||||||
|
|
||||||
|
### Momentry Studio AdminView
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// src/api/index.ts
|
||||||
|
case 'stop_service': {
|
||||||
|
const mbUrl = detectBaseUrl() // http://localhost:11438
|
||||||
|
return { url: `${mbUrl}/api/v2/service/stop`, method: 'POST', body: { name: a.name } }
|
||||||
|
}
|
||||||
|
case 'start_service': {
|
||||||
|
const mbUrl = detectBaseUrl() // http://localhost:11438
|
||||||
|
return { url: `${mbUrl}/api/v2/service/start`, method: 'POST', body: { name: a.name } }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### MarkBase Service API
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// /Users/accusys/markbase/markbase-core/src/server.rs
|
||||||
|
|
||||||
|
async fn service_stop_handler(Json(body): Json<Value>) -> impl IntoResponse {
|
||||||
|
let port = match name {
|
||||||
|
"MarkBase" | "markbase" => 11438,
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
// 問題:直接 kill 進程,不知道 launchd 的存在
|
||||||
|
let _ = Command::new("sh")
|
||||||
|
.arg("-c")
|
||||||
|
.arg(format!("sleep 0.5; lsof -ti :{} | xargs kill -9", port))
|
||||||
|
.spawn();
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn service_start_handler(Json(body): Json<Value>) -> impl IntoResponse {
|
||||||
|
let (cmd, port) = match name {
|
||||||
|
"MarkBase" | "markbase" => (
|
||||||
|
"cd /Users/accusys/markbase && cargo run --bin markbase-core -- web-start --port 11438",
|
||||||
|
11438,
|
||||||
|
),
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
// 問題:使用 cargo run 啟動,不知道 launchd 的存在
|
||||||
|
match Command::new("sh").arg("-c").arg(cmd).output() {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 解決方案
|
||||||
|
|
||||||
|
### 方案 1:修改 MarkBase Service API 支援 launchd(推薦)
|
||||||
|
|
||||||
|
**優點**:
|
||||||
|
- 正確處理服務管理
|
||||||
|
- 與系統服務管理器整合
|
||||||
|
- 避免進程衝突
|
||||||
|
|
||||||
|
**實現**:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// markbase-core/src/server.rs
|
||||||
|
|
||||||
|
async fn service_stop_handler(Json(body): Json<Value>) -> impl IntoResponse {
|
||||||
|
let name = body.get("name").and_then(|v| v.as_str()).unwrap_or("");
|
||||||
|
|
||||||
|
if name == "MarkBase" || name == "markbase" {
|
||||||
|
// 使用 launchctl 停止服務
|
||||||
|
let _ = Command::new("launchctl")
|
||||||
|
.args(&["bootout", &format!("gui/{}", unsafe { libc::getuid() }), "com.momentry.markbase"])
|
||||||
|
.output();
|
||||||
|
return (StatusCode::OK, Json(json!({"result": "stopped"}))).into_response();
|
||||||
|
}
|
||||||
|
// 其他服務的處理...
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn service_start_handler(Json(body): Json<Value>) -> impl IntoResponse {
|
||||||
|
let name = body.get("name").and_then(|v| v.as_str()).unwrap_or("");
|
||||||
|
|
||||||
|
if name == "MarkBase" || name == "markbase" {
|
||||||
|
// 使用 launchctl 啟動服務
|
||||||
|
let plist_path = format!(
|
||||||
|
"{}/Library/LaunchAgents/com.momentry.markbase.plist",
|
||||||
|
dirs::home_dir().unwrap().display()
|
||||||
|
);
|
||||||
|
let _ = Command::new("launchctl")
|
||||||
|
.args(&["bootstrap", &format!("gui/{}", unsafe { libc::getuid() }), &plist_path])
|
||||||
|
.output();
|
||||||
|
return (StatusCode::OK, Json(json!({"result": "started"}))).into_response();
|
||||||
|
}
|
||||||
|
// 其他服務的處理...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 方案 2:前端直接調用 launchctl(臨時方案)
|
||||||
|
|
||||||
|
**缺點**:
|
||||||
|
- 前端需要系統權限
|
||||||
|
- 違反服務分層原則
|
||||||
|
|
||||||
|
**不推薦**。
|
||||||
|
|
||||||
|
### 方案 3:移除 MarkBase launchd 服務(不推薦)
|
||||||
|
|
||||||
|
**缺點**:
|
||||||
|
- 失去自動重啟能力
|
||||||
|
- 需要手動管理
|
||||||
|
|
||||||
|
## 當前臨時解決方案
|
||||||
|
|
||||||
|
**暫時禁用 launchd 自動重啟進行測試**:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 停止 launchd 服務
|
||||||
|
launchctl bootout gui/$(id -u)/com.momentry.markbase
|
||||||
|
|
||||||
|
# 手動啟動 MarkBase
|
||||||
|
cd /Users/accusys/markbase
|
||||||
|
./target/release/markbase-core web-start --port 11438
|
||||||
|
|
||||||
|
# 測試 Stop/Start API
|
||||||
|
curl -X POST http://localhost:11438/api/v2/service/stop \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"name": "markbase"}'
|
||||||
|
|
||||||
|
curl -X POST http://localhost:11438/api/v2/service/start \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"name": "markbase"}'
|
||||||
|
|
||||||
|
# 重新啟用 launchd
|
||||||
|
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.momentry.markbase.plist
|
||||||
|
```
|
||||||
|
|
||||||
|
## 受影響的服務
|
||||||
|
|
||||||
|
| 服務 | Port | launchd 管理 | 受影響 |
|
||||||
|
|------|------|-------------|--------|
|
||||||
|
| Core API | 3002 | ✅ Yes | ❌ No |
|
||||||
|
| Worker | - | ✅ Yes | ❌ No |
|
||||||
|
| MarkBase | 11438 | ✅ Yes | ✅ **Yes** |
|
||||||
|
| MarkBaseEngine | 8080 | ❌ No | ❌ No |
|
||||||
|
| Proxy | 8888 | ❌ No | ❌ No |
|
||||||
|
|
||||||
|
**注意**:Core API 和 Worker 也由 launchd 管理,但它們的 service API 已正確處理 launchd(見 `server.rs` 中的 Worker 處理)。
|
||||||
|
|
||||||
|
## 下一步
|
||||||
|
|
||||||
|
1. **短期**:實現方案 1,修改 MarkBase service API
|
||||||
|
2. **中期**:統一所有服務的管理方式
|
||||||
|
3. **長期**:考慮使用更強大的服務管理器(如 systemd 或 supervisord)
|
||||||
|
|
||||||
|
## 版本歷史
|
||||||
|
|
||||||
|
| Version | Date | Changes |
|
||||||
|
|---------|------|---------|
|
||||||
|
| 1.0 | 2026-10-24 | 初步問題識別與分析 |
|
||||||
Reference in New Issue
Block a user