periodic_traffic_reset_job.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package job
  2. import (
  3. "github.com/mhsanaei/3x-ui/v2/logger"
  4. "github.com/mhsanaei/3x-ui/v2/web/service"
  5. )
  6. // Period represents the time period for traffic resets.
  7. type Period string
  8. // PeriodicTrafficResetJob resets traffic statistics for inbounds based on their configured reset period.
  9. type PeriodicTrafficResetJob struct {
  10. inboundService service.InboundService
  11. period Period
  12. }
  13. // NewPeriodicTrafficResetJob creates a new periodic traffic reset job for the specified period.
  14. func NewPeriodicTrafficResetJob(period Period) *PeriodicTrafficResetJob {
  15. return &PeriodicTrafficResetJob{
  16. period: period,
  17. }
  18. }
  19. // Run resets traffic statistics for all inbounds that match the configured reset period.
  20. func (j *PeriodicTrafficResetJob) Run() {
  21. inbounds, err := j.inboundService.GetInboundsByTrafficReset(string(j.period))
  22. if err != nil {
  23. logger.Warning("Failed to get inbounds for traffic reset:", err)
  24. return
  25. }
  26. if len(inbounds) == 0 {
  27. return
  28. }
  29. logger.Infof("Running periodic traffic reset job for period: %s (%d matching inbounds)", j.period, len(inbounds))
  30. resetCount := 0
  31. for _, inbound := range inbounds {
  32. if err := j.inboundService.ResetAllClientTraffics(inbound.Id); err != nil {
  33. logger.Warning("Failed to reset traffic for inbound", inbound.Id, ":", err)
  34. continue
  35. }
  36. resetCount++
  37. logger.Infof("Reset traffic for inbound %d (%s)", inbound.Id, inbound.Remark)
  38. }
  39. if resetCount > 0 {
  40. logger.Infof("Periodic traffic reset completed: %d inbounds reset", resetCount)
  41. }
  42. }