1
0

periodic_traffic_reset_job.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package job
  2. import (
  3. "github.com/mhsanaei/3x-ui/v3/logger"
  4. "github.com/mhsanaei/3x-ui/v3/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. clientService service.ClientService
  12. period Period
  13. }
  14. // NewPeriodicTrafficResetJob creates a new periodic traffic reset job for the specified period.
  15. func NewPeriodicTrafficResetJob(period Period) *PeriodicTrafficResetJob {
  16. return &PeriodicTrafficResetJob{
  17. period: period,
  18. }
  19. }
  20. // Run resets traffic statistics for all inbounds that match the configured reset period.
  21. func (j *PeriodicTrafficResetJob) Run() {
  22. inbounds, err := j.inboundService.GetInboundsByTrafficReset(string(j.period))
  23. if err != nil {
  24. logger.Warning("Failed to get inbounds for traffic reset:", err)
  25. return
  26. }
  27. if len(inbounds) == 0 {
  28. return
  29. }
  30. logger.Infof("Running periodic traffic reset job for period: %s (%d matching inbounds)", j.period, len(inbounds))
  31. resetCount := 0
  32. for _, inbound := range inbounds {
  33. resetInboundErr := j.inboundService.ResetInboundTraffic(inbound.Id)
  34. if resetInboundErr != nil {
  35. logger.Warning("Failed to reset traffic for inbound", inbound.Id, ":", resetInboundErr)
  36. }
  37. resetClientErr := j.clientService.ResetAllClientTraffics(&j.inboundService, inbound.Id)
  38. if resetClientErr != nil {
  39. logger.Warning("Failed to reset traffic for all users of inbound", inbound.Id, ":", resetClientErr)
  40. }
  41. if resetInboundErr == nil && resetClientErr == nil {
  42. resetCount++
  43. }
  44. }
  45. if resetCount > 0 {
  46. logger.Infof("Periodic traffic reset completed: %d inbounds reset", resetCount)
  47. }
  48. }