@@ -107,30 +107,27 @@ public void setAsyncJobDispatcher(final AsyncJobDispatcher dispatcher) {
107107
108108 @ Override
109109 public boolean configure (String name , Map <String , Object > params ) throws ConfigurationException {
110- try {
111- vmLeaseExecutor = Executors .newSingleThreadScheduledExecutor (new NamedThreadFactory ("VMLeasePollExecutor" ));
112- vmLeaseAlertExecutor = Executors .newSingleThreadScheduledExecutor (new NamedThreadFactory ("VMLeaseAlertPollExecutor" ));
113- } catch (final Exception e ) {
114- throw new ConfigurationException ("Unable to to configure VMLeaseManagerImpl" );
110+ if (InstanceLeaseEnabled .value ()) {
111+ scheduleLeaseExecutors ();
115112 }
116113 return true ;
117114 }
118115
119116 @ Override
120117 public boolean start () {
121- vmLeaseExecutor .scheduleAtFixedRate (new VMLeaseSchedulerTask (),5L , InstanceLeaseSchedulerInterval .value (), TimeUnit .SECONDS );
122- vmLeaseAlertExecutor .scheduleAtFixedRate (new VMLeaseAlertSchedulerTask (), 5L , InstanceLeaseAlertSchedule .value (), TimeUnit .SECONDS );
123118 return true ;
124119 }
125120
126121 @ Override
127122 public boolean stop () {
128- vmLeaseExecutor .shutdown ();
129- vmLeaseAlertExecutor .shutdown ();
123+ shutDownLeaseExecutors ();
130124 return true ;
131125 }
132126
133- @ Override
127+ /**
128+ * This method will cancel lease on instances running under lease
129+ * will be primarily used when feature gets disabled
130+ */
134131 public void cancelLeaseOnExistingInstances () {
135132 List <UserVmJoinVO > leaseExpiringForInstances = userVmJoinDao .listLeaseInstancesExpiringInDays (-1 );
136133 logger .debug ("Total instances found for lease cancellation: {}" , leaseExpiringForInstances .size ());
@@ -142,6 +139,45 @@ public void cancelLeaseOnExistingInstances() {
142139 }
143140 }
144141
142+ @ Override
143+ public void onLeaseFeatureToggle () {
144+ boolean isLeaseFeatureEnabled = VMLeaseManagerImpl .InstanceLeaseEnabled .value ();
145+ if (isLeaseFeatureEnabled ) {
146+ scheduleLeaseExecutors ();
147+ } else {
148+ cancelLeaseOnExistingInstances ();
149+ shutDownLeaseExecutors ();
150+ }
151+ }
152+
153+ private void scheduleLeaseExecutors () {
154+ if (vmLeaseExecutor == null || vmLeaseExecutor .isShutdown ()) {
155+ logger .debug ("Scheduling lease executor" );
156+ vmLeaseExecutor = Executors .newSingleThreadScheduledExecutor (new NamedThreadFactory ("VMLeasePollExecutor" ));
157+ vmLeaseExecutor .scheduleAtFixedRate (new VMLeaseSchedulerTask (),5L , InstanceLeaseSchedulerInterval .value (), TimeUnit .SECONDS );
158+ }
159+
160+ if (vmLeaseAlertExecutor == null || vmLeaseAlertExecutor .isShutdown ()) {
161+ logger .debug ("Scheduling lease alert executor" );
162+ vmLeaseAlertExecutor = Executors .newSingleThreadScheduledExecutor (new NamedThreadFactory ("VMLeaseAlertPollExecutor" ));
163+ vmLeaseAlertExecutor .scheduleAtFixedRate (new VMLeaseAlertSchedulerTask (), 5L , InstanceLeaseAlertSchedule .value (), TimeUnit .SECONDS );
164+ }
165+ }
166+
167+ private void shutDownLeaseExecutors () {
168+ if (vmLeaseExecutor != null ) {
169+ logger .debug ("Shutting down lease executor" );
170+ vmLeaseExecutor .shutdown ();
171+ vmLeaseExecutor = null ;
172+ }
173+
174+ if (vmLeaseAlertExecutor != null ) {
175+ logger .debug ("Shutting down lease alert executor" );
176+ vmLeaseAlertExecutor .shutdown ();
177+ vmLeaseAlertExecutor = null ;
178+ }
179+ }
180+
145181 class VMLeaseSchedulerTask extends ManagedContextRunnable {
146182 @ Override
147183 protected void runInContext () {
0 commit comments