2323import com .cloud .api .query .dao .UserVmJoinDao ;
2424import com .cloud .api .query .vo .UserVmJoinVO ;
2525import com .cloud .event .ActionEventUtils ;
26+ import com .cloud .user .Account ;
2627import com .cloud .user .User ;
28+ import com .cloud .utils .DateUtil ;
2729import com .cloud .utils .StringUtils ;
2830import com .cloud .utils .component .ComponentContext ;
2931import com .cloud .utils .component .ManagerBase ;
3941import org .apache .cloudstack .framework .jobs .AsyncJobManager ;
4042import org .apache .cloudstack .framework .jobs .impl .AsyncJobVO ;
4143import org .apache .cloudstack .managed .context .ManagedContextTimerTask ;
44+ import org .apache .commons .lang3 .time .DateUtils ;
4245
4346import javax .inject .Inject ;
4447import java .util .ArrayList ;
4548import java .util .Arrays ;
49+ import java .util .Calendar ;
4650import java .util .Date ;
4751import java .util .HashMap ;
4852import java .util .List ;
@@ -138,17 +142,32 @@ protected void alert() {
138142 return ;
139143 }
140144
141- List <UserVmJoinVO > leaseExpiringForInstances = userVmJoinDao .listExpiringInstancesInDays (InstanceLeaseAlertStartsAt .value ().intValue ());
142- for (UserVmJoinVO instance : leaseExpiringForInstances ) {
143- alertManager .sendAlert (AlertManager .AlertType .ALERT_TYPE_USERVM , instance .getDataCenterId (), instance .getPodId (),
144- "Lease expiring for instance id: " + instance .getUuid (), "Lease expiring for instance" );
145+ GlobalLock scanLock = GlobalLock .getInternLock ("VMLeaseAlertScheduler" );
146+ try {
147+ if (scanLock .lock (ACQUIRE_GLOBAL_LOCK_TIMEOUT_FOR_COOPERATION )) {
148+ try {
149+ List <UserVmJoinVO > leaseExpiringForInstances = userVmJoinDao .listLeaseInstancesExpiringInDays (InstanceLeaseAlertStartsAt .value ().intValue ());
150+ for (UserVmJoinVO instance : leaseExpiringForInstances ) {
151+ alertManager .sendAlert (AlertManager .AlertType .ALERT_TYPE_USERVM , instance .getDataCenterId (), instance .getPodId (),
152+ "Lease expiring for instance id: " + instance .getUuid (), "Lease expiring for instance" );
153+ }
154+ } finally {
155+ scanLock .unlock ();
156+ }
157+ }
158+ } finally {
159+ scanLock .releaseRef ();
145160 }
146161 }
147162
148163 @ Override
149- public void poll (Date currentTimestamp ) {
150- // as feature is disabled, no action is required
164+ public void poll (Date timestamp ) {
165+ Date currentTimestamp = DateUtils .round (timestamp , Calendar .MINUTE );
166+ String displayTime = DateUtil .displayDateInTimezone (DateUtil .GMT_TIMEZONE , currentTimestamp );
167+ logger .debug ("VMLeaseScheduler.poll is being called at {}" , displayTime );
168+
151169 if (!InstanceLeaseEnabled .value ()) {
170+ logger .debug ("Instance lease feature is disabled, no action is required" );
152171 return ;
153172 }
154173
@@ -168,16 +187,15 @@ public void poll(Date currentTimestamp) {
168187
169188 protected void reallyRun () {
170189 // fetch user_instances having leaseDuration configured and has expired
171- List <UserVmJoinVO > leaseExpiredInstances = userVmJoinDao .listExpiredInstancesIds ();
190+ List <UserVmJoinVO > leaseExpiredInstances = userVmJoinDao .listLeaseExpiredInstances ();
172191 List <Long > actionableInstanceIds = new ArrayList <>();
173- // iterate over them and ignore if delete protection is enabled
192+ // iterate over and skip ones with delete protection
174193 for (UserVmJoinVO userVmVO : leaseExpiredInstances ) {
175194 if (userVmVO .isDeleteProtection () != null && userVmVO .isDeleteProtection ()) {
176195 logger .debug ("Ignoring instance with id: {} as deleteProtection is enabled" , userVmVO .getUuid ());
177196 continue ;
178197 }
179198 // state check, include instances not yet stopped or destroyed
180- // it can be done in fetch_user_instances as well
181199 if (!Arrays .asList (Destroyed , Expunging , Unknown , VirtualMachine .State .Error ).contains (userVmVO .getState ())) {
182200 actionableInstanceIds .add (userVmVO .getId ());
183201 }
@@ -208,7 +226,7 @@ protected void reallyRun() {
208226 failedToSubmitInstanceIds .add (instanceId );
209227 }
210228 }
211- logger .debug ("Successfully submitted jobs for ids: {}" , submittedJobIds );
229+ logger .debug ("Successfully submitted lease expiry jobs with ids: {}" , submittedJobIds );
212230 if (!failedToSubmitInstanceIds .isEmpty ()) {
213231 logger .debug ("Lease scheduler failed to submit jobs for instance ids: {}" , failedToSubmitInstanceIds );
214232 }
@@ -227,7 +245,7 @@ Long executeExpiryAction(UserVmJoinVO instance, ExpiryAction expiryAction, long
227245 }
228246 default : {
229247 logger .error ("Invalid configuration for instance.lease.expiryaction for vm id: {}, " +
230- "valid values are: \" stop \" and \" destroy \" " , instance .getUuid ());
248+ "valid values are: \" STOP \" and \" DESTROY \" " , instance .getUuid ());
231249 }
232250 }
233251 return null ;
@@ -236,8 +254,8 @@ Long executeExpiryAction(UserVmJoinVO instance, ExpiryAction expiryAction, long
236254 long executeStopInstanceJob (UserVmJoinVO vm , boolean isForced , long eventId ) {
237255 final Map <String , String > params = new HashMap <>();
238256 params .put (ApiConstants .ID , String .valueOf (vm .getId ()));
239- params .put ("ctxUserId" , "1" );
240- params .put ("ctxAccountId" , String .valueOf (vm . getAccountId () ));
257+ params .put ("ctxUserId" , String . valueOf ( User . UID_SYSTEM ) );
258+ params .put ("ctxAccountId" , String .valueOf (Account . ACCOUNT_ID_SYSTEM ));
241259 params .put (ApiConstants .CTX_START_EVENT_ID , String .valueOf (eventId ));
242260 params .put (ApiConstants .FORCED , String .valueOf (isForced ));
243261 final StopVMCmd cmd = new StopVMCmd ();
@@ -252,8 +270,8 @@ long executeStopInstanceJob(UserVmJoinVO vm, boolean isForced, long eventId) {
252270 long executeDestroyInstanceJob (UserVmJoinVO vm , boolean isForced , long eventId ) {
253271 final Map <String , String > params = new HashMap <>();
254272 params .put (ApiConstants .ID , String .valueOf (vm .getId ()));
255- params .put ("ctxUserId" , "1" );
256- params .put ("ctxAccountId" , String .valueOf (vm . getAccountId () ));
273+ params .put ("ctxUserId" , String . valueOf ( User . UID_SYSTEM ) );
274+ params .put ("ctxAccountId" , String .valueOf (Account . ACCOUNT_ID_SYSTEM ));
257275 params .put (ApiConstants .CTX_START_EVENT_ID , String .valueOf (eventId ));
258276 params .put (ApiConstants .FORCED , String .valueOf (isForced ));
259277
0 commit comments