-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(services/systat): 将 handlers/monitor 改为 services/systat
不再与 SSE 绑定,而是变为一个服务,由用户自行订阅数据。
- Loading branch information
Showing
8 changed files
with
105 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// SPDX-FileCopyrightText: 2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Package services 提供一些常用的服务 | ||
package services |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-FileCopyrightText: 2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Package systat 系统状态检测 | ||
package systat | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/issue9/events" | ||
"github.com/issue9/web" | ||
) | ||
|
||
// 系统状态监视服务 | ||
type service struct { | ||
events *events.Event[*Stats] | ||
} | ||
|
||
// Init 初始化监视系统状态的服务 | ||
// | ||
// 这将返回一个用于订阅状态变化的接口,用户可根据该接口订阅信息。 | ||
// | ||
// dur 为监视数据的频率; | ||
// interval 为每次监视数据的时间; | ||
func Init(s web.Server, dur, interval time.Duration) events.Subscriber[*Stats] { | ||
srv := &service{ | ||
events: events.New[*Stats](), | ||
} | ||
|
||
job := func(now time.Time) error { | ||
if srv.events.Len() == 0 { // 没有订阅,就没必要计算状态了。 | ||
return nil | ||
} | ||
|
||
stat, err := calcState(interval, now) | ||
if err == nil { | ||
srv.events.Publish(true, stat) | ||
} | ||
return err | ||
} | ||
|
||
s.Services().AddTicker(web.Phrase("monitor system stat"), job, dur, true, true) | ||
|
||
return srv | ||
} | ||
|
||
// Subscribe 订阅状态变化的通知 | ||
func (s *service) Subscribe(f events.SubscribeFunc[*Stats]) context.CancelFunc { | ||
return s.events.Subscribe(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters