mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-03 03:35:04 +03:00
fix: keep panel restarts from hanging on the CPU job and Telegram (#7029)
Restarting the panel left it unreachable for up to a minute. CheckCpuJob sampled cpu.Percent over its whole one-minute period, so cron.Stop nearly always waited on it; it now diffs cpu.Times against its previous run (iowait counted as idle, as before). With Telegram unreachable, bus.Stop waited on an in-flight send and tgbot.Start ran getMe/setMyCommands inline; each bot call now returns once StopBot closes the bot's stop channel, the bot stops before the bus, and the artwork/commands warm-up runs in the background.
This commit is contained in:
@@ -1,25 +1,38 @@
|
||||
package job
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/eventbus"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/util/sys"
|
||||
|
||||
"github.com/shirou/gopsutil/v4/cpu"
|
||||
)
|
||||
|
||||
// CheckCpuJob monitors CPU usage and publishes events when threshold is exceeded.
|
||||
type CheckCpuJob struct{}
|
||||
type CheckCpuJob struct {
|
||||
readTimes func(percpu bool) ([]cpu.TimesStat, error)
|
||||
last cpu.TimesStat
|
||||
hasLast bool
|
||||
}
|
||||
|
||||
// NewCheckCpuJob creates a new CPU monitoring job instance.
|
||||
func NewCheckCpuJob() *CheckCpuJob {
|
||||
return new(CheckCpuJob)
|
||||
return &CheckCpuJob{readTimes: cpu.Times}
|
||||
}
|
||||
|
||||
// Run checks CPU usage and publishes a cpu.high event with raw metric data.
|
||||
// Run publishes a cpu.high event with the CPU usage since the previous run. It
|
||||
// must not sample in place: cron.Stop, and so every panel restart, waits on it.
|
||||
func (j *CheckCpuJob) Run() {
|
||||
percent, err := cpu.Percent(1*time.Minute, false)
|
||||
if err != nil || len(percent) == 0 {
|
||||
times, err := j.readTimes(false)
|
||||
if err != nil || len(times) == 0 {
|
||||
return
|
||||
}
|
||||
prev, hadPrev := j.last, j.hasLast
|
||||
j.last, j.hasLast = times[0], true
|
||||
if !hadPrev {
|
||||
return
|
||||
}
|
||||
percent, ok := sys.CPUWorkPercent(prev, times[0])
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -27,7 +40,7 @@ func (j *CheckCpuJob) Run() {
|
||||
EventBus.Publish(eventbus.Event{
|
||||
Type: eventbus.EventCPUHigh,
|
||||
Data: &eventbus.SystemMetricData{
|
||||
Percent: percent[0],
|
||||
Percent: percent,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user