1919import com .cronutils .model .definition .CronDefinitionBuilder ;
2020import com .cronutils .model .time .ExecutionTime ;
2121import com .cronutils .parser .CronParser ;
22- import com .google .common .annotations .VisibleForTesting ;
2322import io .trino .gateway .ha .config .ProxyBackendConfiguration ;
2423import io .trino .gateway .ha .config .ScheduleConfiguration ;
2524import io .trino .gateway .ha .router .GatewayBackendManager ;
@@ -44,7 +43,7 @@ public class ClusterScheduler
4443 implements AutoCloseable
4544{
4645 private static final Logger log = LoggerFactory .getLogger (ClusterScheduler .class );
47- private final ScheduledExecutorService scheduler = Executors .newScheduledThreadPool (1 );
46+ private ScheduledExecutorService scheduler = Executors .newScheduledThreadPool (1 );
4847 private final GatewayBackendManager backendManager ;
4948 private final ScheduleConfiguration config ;
5049 private final Map <String , ExecutionTime > executionTimes = new ConcurrentHashMap <>();
@@ -98,31 +97,28 @@ public void start()
9897 schedule .isActiveDuringCron ());
9998 }
10099 catch (Exception e ) {
101- log .error ("Failed to parse cron expression for cluster {} : {}" ,
100+ log .error ("Skipping cluster {} due to invalid cron expression '{}' : {}" ,
102101 schedule .getClusterName (),
103102 schedule .getCronExpression (),
104- e );
103+ e . getMessage () );
105104 }
106105 }
107106
108107 // Schedule the task
109- scheduler .scheduleWithFixedDelay (
110- this :: checkAndUpdateClusterStatus ,
108+ scheduler .scheduleAtFixedRate (
109+ () -> checkAndUpdateClusterStatus ( ZonedDateTime . now ( timezone )) ,
111110 0 ,
112- ( long ) config .getCheckInterval ().toMillis (),
111+ config .getCheckInterval ().toMillis (),
113112 TimeUnit .MILLISECONDS );
114-
115113 log .info ("Started cluster scheduler with check interval: {} (using {} timezone)" ,
116114 config .getCheckInterval (),
117115 timezone );
118116 }
119117
120- @ VisibleForTesting
121- void checkAndUpdateClusterStatus ()
118+ public void checkAndUpdateClusterStatus (ZonedDateTime currentTime )
122119 {
123120 try {
124- ZonedDateTime now = ZonedDateTime .now (timezone );
125- log .debug ("Checking cluster status at: {} ({})" , now , timezone );
121+ log .debug ("Checking cluster status at: {} ({})" , currentTime , timezone );
126122
127123 for (Map .Entry <String , ExecutionTime > entry : executionTimes .entrySet ()) {
128124 String clusterName = entry .getKey ();
@@ -139,7 +135,7 @@ void checkAndUpdateClusterStatus()
139135 }
140136
141137 ScheduleConfiguration .ClusterSchedule schedule = scheduleOpt .get ();
142- boolean cronMatches = executionTime .isMatch (now );
138+ boolean cronMatches = executionTime .isMatch (currentTime );
143139 boolean shouldBeActive = cronMatches == schedule .isActiveDuringCron ();
144140
145141 log .info ("Cluster: {}, cronMatches: {}, activeDuringCron: {}, shouldBeActive: {}" ,
0 commit comments