mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-10-13 19:49:12 +00:00
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package job
|
|
|
|
import (
|
|
"github.com/mhsanaei/3x-ui/v2/logger"
|
|
"github.com/mhsanaei/3x-ui/v2/web/service"
|
|
)
|
|
|
|
// Period represents the time period for traffic resets.
|
|
type Period string
|
|
|
|
// PeriodicTrafficResetJob resets traffic statistics for inbounds based on their configured reset period.
|
|
type PeriodicTrafficResetJob struct {
|
|
inboundService service.InboundService
|
|
period Period
|
|
}
|
|
|
|
// NewPeriodicTrafficResetJob creates a new periodic traffic reset job for the specified period.
|
|
func NewPeriodicTrafficResetJob(period Period) *PeriodicTrafficResetJob {
|
|
return &PeriodicTrafficResetJob{
|
|
period: period,
|
|
}
|
|
}
|
|
|
|
// Run resets traffic statistics for all inbounds that match the configured reset period.
|
|
func (j *PeriodicTrafficResetJob) Run() {
|
|
inbounds, err := j.inboundService.GetInboundsByTrafficReset(string(j.period))
|
|
if err != nil {
|
|
logger.Warning("Failed to get inbounds for traffic reset:", err)
|
|
return
|
|
}
|
|
|
|
if len(inbounds) == 0 {
|
|
return
|
|
}
|
|
logger.Infof("Running periodic traffic reset job for period: %s (%d matching inbounds)", j.period, len(inbounds))
|
|
|
|
resetCount := 0
|
|
|
|
for _, inbound := range inbounds {
|
|
if err := j.inboundService.ResetAllClientTraffics(inbound.Id); err != nil {
|
|
logger.Warning("Failed to reset traffic for inbound", inbound.Id, ":", err)
|
|
continue
|
|
}
|
|
|
|
resetCount++
|
|
logger.Infof("Reset traffic for inbound %d (%s)", inbound.Id, inbound.Remark)
|
|
}
|
|
|
|
if resetCount > 0 {
|
|
logger.Infof("Periodic traffic reset completed: %d inbounds reset", resetCount)
|
|
}
|
|
}
|