outbound_subscription_job.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package job
  2. import (
  3. "github.com/mhsanaei/3x-ui/v3/logger"
  4. "github.com/mhsanaei/3x-ui/v3/web/service"
  5. "github.com/mhsanaei/3x-ui/v3/web/websocket"
  6. )
  7. // OutboundSubscriptionJob periodically re-fetches enabled outbound subscriptions,
  8. // updates the stored outbounds (with stable tags), and signals that xray
  9. // should be reloaded so the new outbounds take effect.
  10. type OutboundSubscriptionJob struct {
  11. subService *service.OutboundSubscriptionService
  12. xraySvc *service.XrayService
  13. }
  14. // NewOutboundSubscriptionJob creates the job (zero-value services are populated
  15. // on first Run via method calls, same pattern as other jobs).
  16. func NewOutboundSubscriptionJob() *OutboundSubscriptionJob {
  17. return &OutboundSubscriptionJob{
  18. subService: &service.OutboundSubscriptionService{},
  19. xraySvc: &service.XrayService{},
  20. }
  21. }
  22. // Run is invoked by the cron scheduler.
  23. func (j *OutboundSubscriptionJob) Run() {
  24. if j.subService == nil {
  25. j.subService = &service.OutboundSubscriptionService{}
  26. }
  27. if j.xraySvc == nil {
  28. j.xraySvc = &service.XrayService{}
  29. }
  30. count, err := j.subService.RefreshAllEnabled()
  31. if err != nil {
  32. logger.Warning("outbound subscription auto-update error:", err)
  33. return
  34. }
  35. if count > 0 {
  36. logger.Infof("Refreshed %d outbound subscription(s)", count)
  37. // Ask the xray manager to restart/reload on the next 30s check.
  38. j.xraySvc.SetToNeedRestart()
  39. // Also broadcast an invalidate so the UI can refresh the xray setting
  40. // view (new outbounds will be visible after the reload cycle).
  41. websocket.BroadcastInvalidate(websocket.MessageTypeOutbounds)
  42. }
  43. }