查询主程序是否运行,可以恢复git
Some checks are pending
CI / host-build (push) Waiting to run
CI / rk3588-cross-build (push) Waiting to run

This commit is contained in:
sladro 2026-01-10 17:54:21 +08:00
parent d0a1abb2aa
commit f8d8c6aba7
4 changed files with 68 additions and 1 deletions

View File

@ -242,7 +242,11 @@ Response 200`{"ok":true}`
>
> agent 启动 media-server 时会显式传入:`--config <resolved_config_path>`,因此不依赖 media-server 内部默认配置。
>
> 路径说明:`agent.media_server_process` 下的 `exec_path/work_dir/configs_dir/pid_file` 支持绝对或相对路径;若为相对路径,则以 agent 启动参数 `--config <agent_config_path>` 的**配置文件所在目录**为基准解析。
> 路径说明:
> - `agent.media_server_process` 下的 `exec_path/work_dir/configs_dir/pid_file` 支持绝对或相对路径;若为相对路径,则以 agent 启动参数 `--config <agent_config_path>` 的**配置文件所在目录**为基准解析。
> - `agent.config_path` 建议使用**绝对路径**;若使用相对路径:
> - `PUT /v1/config` 写盘时,以 **agent 进程当前工作目录CWD** 为基准;
> - 通过 `/v1/media-server/start|restart`(缺省 config启动时会把该相对路径原样传给 media-server最终以 **media_server_process.work_dir** 为基准解析。
### 6.1 `POST /v1/media-server/start`
用途:启动本机 media-server若已运行则幂等返回 ok若已运行但 config 不同则 409
@ -307,6 +311,20 @@ Response 200
- 501未启用进程控制
- 500停止失败
### 6.4 `GET /v1/media-server/status`
用途:查询本机 media-server 是否在运行(以 agent 的 pidfile 记录为准)。
**Auth**:读接口(见 0.2
Response 200
```json
{"ok":true,"running":true,"pid":1234,"config_path":"/etc/rk3588sys/config.json"}
```
失败:
- 501未启用进程控制
- 500读取 pidfile/进程存活检测失败
## 7. 只读代理接口agent 对外,推荐管理端统一走 agent
### 7.1 `GET /v1/graphs`

View File

@ -77,6 +77,7 @@ func New(agentCfg config.AgentConfig, baseDir string, ms *mediaserver.Client, st
mux.HandleFunc("/v1/media-server/start", s.handleMediaStart)
mux.HandleFunc("/v1/media-server/restart", s.handleMediaRestart)
mux.HandleFunc("/v1/media-server/stop", s.handleMediaStop)
mux.HandleFunc("/v1/media-server/status", s.handleMediaStatus)
mux.HandleFunc("/v1/graphs", s.handleGraphs)
mux.HandleFunc("/v1/graphs/", s.handleGraphDetail)
mux.HandleFunc("/v1/logs/recent", s.handleLogsRecent)
@ -384,6 +385,31 @@ func (s *Server) handleMediaStop(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "running": st.Running, "pid": st.Pid, "config_path": st.ConfigPath})
}
func (s *Server) handleMediaStatus(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
errorJSON(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
if !s.authorize(r, false) {
errorJSON(w, http.StatusUnauthorized, "unauthorized")
return
}
if s.proc == nil || !s.proc.Enabled() {
errorJSON(w, http.StatusNotImplemented, "not supported")
return
}
st, err := s.proc.Status()
if err != nil {
if errors.Is(err, procctl.ErrNotSupported) {
errorJSON(w, http.StatusNotImplemented, "not supported")
return
}
errorJSON(w, http.StatusInternalServerError, "internal error: "+err.Error())
return
}
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "running": st.Running, "pid": st.Pid, "config_path": st.ConfigPath})
}
func readOptionalJSON[T any](w http.ResponseWriter, r *http.Request, maxBytes int64) (T, error) {
var zero T
if r.Body == nil {

View File

@ -58,6 +58,29 @@ func New(agentCfg config.AgentConfig, baseDir string) *Controller {
func (c *Controller) Enabled() bool { return c != nil && c.proc.Enable }
func (c *Controller) Status() (Status, error) {
c.mu.Lock()
defer c.mu.Unlock()
pf, err := c.readPidFile()
if err != nil {
return Status{}, err
}
if pf == nil {
return Status{Running: false}, nil
}
alive, aerr := isAlive(pf.Pid)
if errors.Is(aerr, ErrNotSupported) {
return Status{}, ErrNotSupported
}
if !alive {
_ = os.Remove(c.proc.PidFile)
return Status{Running: false, Pid: pf.Pid, ConfigPath: pf.ConfigPath}, nil
}
return Status{Running: true, Pid: pf.Pid, ConfigPath: pf.ConfigPath}, nil
}
func (c *Controller) Start(configName string) (Status, error) {
c.mu.Lock()
defer c.mu.Unlock()

Binary file not shown.