check_memory_usage.go 732 B

1234567891011121314151617181920212223242526272829303132
  1. package job
  2. import (
  3. "github.com/mhsanaei/3x-ui/v3/internal/eventbus"
  4. "github.com/shirou/gopsutil/v4/mem"
  5. )
  6. // CheckMemJob monitors memory usage and publishes events when threshold is exceeded.
  7. type CheckMemJob struct{}
  8. // NewCheckMemJob creates a new memory monitoring job instance.
  9. func NewCheckMemJob() *CheckMemJob {
  10. return new(CheckMemJob)
  11. }
  12. // Run checks memory usage and publishes a memory.high event with raw metric data.
  13. func (j *CheckMemJob) Run() {
  14. memInfo, err := mem.VirtualMemory()
  15. if err != nil || memInfo == nil {
  16. return
  17. }
  18. if EventBus != nil {
  19. EventBus.Publish(eventbus.Event{
  20. Type: eventbus.EventMemoryHigh,
  21. Data: &eventbus.SystemMetricData{
  22. Percent: memInfo.UsedPercent,
  23. },
  24. })
  25. }
  26. }