add
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package cronjob
|
||||
|
||||
import (
|
||||
"github.com/alireza0/s-ui/database"
|
||||
"github.com/alireza0/s-ui/logger"
|
||||
)
|
||||
|
||||
type WALCheckpointJob struct{}
|
||||
|
||||
func NewWALCheckpointJob() *WALCheckpointJob {
|
||||
return &WALCheckpointJob{}
|
||||
}
|
||||
|
||||
func (s *WALCheckpointJob) Run() {
|
||||
db := database.GetDB()
|
||||
if err := db.Exec("PRAGMA wal_checkpoint(FULL)").Error; err != nil {
|
||||
logger.Error("Error checkpointing WAL: ", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cronjob
|
||||
|
||||
import (
|
||||
"github.com/alireza0/s-ui/service"
|
||||
)
|
||||
|
||||
type CheckCoreJob struct {
|
||||
service.ConfigService
|
||||
}
|
||||
|
||||
func NewCheckCoreJob() *CheckCoreJob {
|
||||
return &CheckCoreJob{}
|
||||
}
|
||||
|
||||
func (s *CheckCoreJob) Run() {
|
||||
s.ConfigService.StartCore()
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package cronjob
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
type CronJob struct {
|
||||
cron *cron.Cron
|
||||
}
|
||||
|
||||
func NewCronJob() *CronJob {
|
||||
return &CronJob{}
|
||||
}
|
||||
|
||||
func (c *CronJob) Start(loc *time.Location, trafficAge int) error {
|
||||
c.cron = cron.New(cron.WithLocation(loc), cron.WithSeconds())
|
||||
c.cron.Start()
|
||||
|
||||
go func() {
|
||||
// Start stats job
|
||||
c.cron.AddJob("@every 10s", NewStatsJob(trafficAge > 0))
|
||||
// Start expiry job
|
||||
c.cron.AddJob("@every 1m", NewDepleteJob())
|
||||
// Start deleting old stats
|
||||
if trafficAge > 0 {
|
||||
c.cron.AddJob("@daily", NewDelStatsJob(trafficAge))
|
||||
}
|
||||
// Start core if it is not running
|
||||
c.cron.AddJob("@every 5s", NewCheckCoreJob())
|
||||
// database WAL checkpoint
|
||||
c.cron.AddJob("@every 10m", NewWALCheckpointJob())
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CronJob) Stop() {
|
||||
if c.cron != nil {
|
||||
c.cron.Stop()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cronjob
|
||||
|
||||
import (
|
||||
"github.com/alireza0/s-ui/logger"
|
||||
"github.com/alireza0/s-ui/service"
|
||||
)
|
||||
|
||||
type DelStatsJob struct {
|
||||
service.StatsService
|
||||
trafficAge int
|
||||
}
|
||||
|
||||
func NewDelStatsJob(ta int) *DelStatsJob {
|
||||
return &DelStatsJob{
|
||||
trafficAge: ta,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DelStatsJob) Run() {
|
||||
err := s.StatsService.DelOldStats(s.trafficAge)
|
||||
if err != nil {
|
||||
logger.Warning("Deleting old statistics failed: ", err)
|
||||
return
|
||||
}
|
||||
logger.Debug("Stats older than ", s.trafficAge, " days were deleted")
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package cronjob
|
||||
|
||||
import (
|
||||
"github.com/alireza0/s-ui/database"
|
||||
"github.com/alireza0/s-ui/logger"
|
||||
"github.com/alireza0/s-ui/service"
|
||||
)
|
||||
|
||||
type DepleteJob struct {
|
||||
service.ClientService
|
||||
service.InboundService
|
||||
}
|
||||
|
||||
func NewDepleteJob() *DepleteJob {
|
||||
return new(DepleteJob)
|
||||
}
|
||||
|
||||
func (s *DepleteJob) Run() {
|
||||
inboundIds, err := s.ClientService.DepleteClients()
|
||||
if err != nil {
|
||||
logger.Warning("Disable depleted users failed: ", err)
|
||||
return
|
||||
}
|
||||
if len(inboundIds) > 0 {
|
||||
err := s.InboundService.RestartInbounds(database.GetDB(), inboundIds)
|
||||
if err != nil {
|
||||
logger.Error("unable to restart inbounds: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cronjob
|
||||
|
||||
import (
|
||||
"github.com/alireza0/s-ui/logger"
|
||||
"github.com/alireza0/s-ui/service"
|
||||
)
|
||||
|
||||
type StatsJob struct {
|
||||
service.StatsService
|
||||
enableTraffic bool
|
||||
}
|
||||
|
||||
func NewStatsJob(saveTraffic bool) *StatsJob {
|
||||
return &StatsJob{
|
||||
enableTraffic: saveTraffic,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StatsJob) Run() {
|
||||
err := s.StatsService.SaveStats(s.enableTraffic)
|
||||
if err != nil {
|
||||
logger.Warning("Get stats failed: ", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user