@@ -54,14 +54,35 @@ const (
5454
5555type UnitName string
5656
57+ // lastoreAutoCheck: automatic update check scheduled task unit
58+ //
59+ // Complete workflow:
60+ // 1. System startup phase
61+ // startOfflineTask() -> getLastoreSystemUnitMap()
62+ // -> Create lastoreAutoCheck.timer
63+ // -> Delay time = getNextAutoCheckDelay()
64+ //
65+ // ↓
66+ //
67+ // 2. Timer trigger phase
68+ // systemd-run executes dbus-send
69+ // -> Call Manager.HandleSystemEvent("AutoCheck")
70+ //
71+ // ↓
72+ //
73+ // 3. Event processing phase
74+ // delHandleSystemEvent() -> handleAutoCheckEvent()
75+ // -> Execute actual update check operations (UpdateSource/platform version request)
76+ // -> Update LastCheckTime
77+ // -> Call updateAutoCheckSystemUnit() to reset timer
78+
5779const (
5880 lastoreAutoClean UnitName = "lastoreAutoClean"
5981 lastoreAutoCheck UnitName = "lastoreAutoCheck"
6082 lastoreAutoUpdateToken UnitName = "lastoreAutoUpdateToken"
6183 watchOsVersion UnitName = "watchOsVersion"
6284 lastoreInitIdleDownload UnitName = "lastoreInitIdleDownload"
63- lastoreRegularlyUpdate UnitName = "lastoreRegularlyUpdate" // 到触发时间后开始检查更新->下载更新->安装更新
64- lastoreCronCheck UnitName = "lastoreCronCheck"
85+ lastoreRegularlyUpdate UnitName = "lastoreRegularlyUpdate"
6586 lastorePostUpgrade UnitName = "lastorePostUpgrade"
6687 lastoreRetryPostMsg UnitName = "lastoreRetryPostMsg"
6788)
@@ -79,8 +100,7 @@ func (m *Manager) getLastoreSystemUnitMap() lastoreUnitMap {
79100 unitMap := make (lastoreUnitMap )
80101 if (m .config .GetLastoreDaemonStatus ()& config .DisableUpdate ) == 0 && ! m .ImmutableAutoRecovery { // 更新禁用未开启且无忧还原未开启时
81102 unitMap [lastoreAutoCheck ] = genHandleEventCmdArgs ([]string {
82- // 随机数范围1800-21600,时间为0.5~6小时
83- fmt .Sprintf ("--on-active=%d" , int (m .getNextUpdateDelay ()/ time .Second )+ rand .New (rand .NewSource (time .Now ().UnixNano ())).Intn (m .config .StartCheckRange [1 ]- m .config .StartCheckRange [0 ])+ m .config .StartCheckRange [0 ])}, AutoCheck ) // 根据上次检查时间,设置下一次自动检查时间
103+ fmt .Sprintf ("--on-active=%d" , m .getNextAutoCheckDelay ())}, AutoCheck )
84104 }
85105 unitMap [lastoreAutoClean ] = genHandleEventCmdArgs ([]string {
86106 "--on-active=600" }, AutoClean ) // 10分钟后自动检查是否需要清理
@@ -260,31 +280,6 @@ func (m *Manager) updateTimerUnit(unitName UnitName) error {
260280 return nil
261281}
262282
263- func (m * Manager ) startCheckPolicyTask () {
264- if m .config .CheckPolicyInterval == 0 {
265- logger .Info ("config: not CheckPolicyInterval" )
266- return
267- }
268-
269- args := []string {
270- fmt .Sprintf ("--unit=%s" , lastoreCronCheck ),
271- fmt .Sprintf ("--on-active=%d" , m .config .CheckPolicyInterval ),
272- fmt .Sprintf (`--on-unit-active=%d` , m .config .CheckPolicyInterval ),
273- "--uid=root" ,
274- "/usr/bin/lastore-tools" ,
275- "checkpolicy" ,
276- }
277- cmd := exec .Command (run , args ... )
278- logger .Info (cmd .String ())
279- var errBuffer bytes.Buffer
280- cmd .Stderr = & errBuffer
281- err := cmd .Run ()
282- if err != nil {
283- logger .Warning (err )
284- logger .Warning (errBuffer .String ())
285- }
286- }
287-
288283func (m * Manager ) handleAutoDownload () {
289284 if m .ImmutableAutoRecovery {
290285 logger .Debug ("Immutable auto recovery is enabled, don't allow to auto download" )
@@ -332,6 +327,24 @@ func (m *Manager) getNextUpdateDelay() time.Duration {
332327 return remained + _minDelayTime
333328}
334329
330+ func (m * Manager ) getNextAutoCheckDelay () int {
331+ checkInterval := m .config .CheckInterval
332+ if checkInterval < 0 {
333+ checkInterval = 0
334+ }
335+
336+ elapsed := time .Since (m .config .LastCheckTime )
337+ remained := int ((checkInterval - elapsed ) / time .Second )
338+ if remained < 0 {
339+ remained = 0
340+ }
341+
342+ randomDelay := rand .New (rand .NewSource (time .Now ().UnixNano ())).Intn (m .config .StartCheckRange [1 ]- m .config .StartCheckRange [0 ]) + m .config .StartCheckRange [0 ]
343+ autoCheckDelay := remained + randomDelay
344+ logger .Infof ("get next auto check delay, StartCheckRange=%v, randomDelay=%d, autoCheckDelay=%d" , m .config .StartCheckRange , randomDelay , autoCheckDelay )
345+ return autoCheckDelay
346+ }
347+
335348// isAllowedToTriggerSystemEvent checks if the uid is allowed to trigger system events
336349func isAllowedToTriggerSystemEvent (uid uint32 , eventType systemdEventType ) bool {
337350 // Allow regular users to trigger OsVersionChanged event
@@ -384,7 +397,6 @@ func (m *Manager) delHandleSystemEvent(sender dbus.Sender, eventType string) err
384397 if err != nil {
385398 logger .Warning (err )
386399 }
387- m .startCheckPolicyTask () // 在第一次自动检查更新后再加任务
388400 }()
389401 case AutoClean :
390402 go func () {
0 commit comments