check_memory_usage.go 826 B

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