23
23
import com .cloud .api .query .dao .UserVmJoinDao ;
24
24
import com .cloud .api .query .vo .UserVmJoinVO ;
25
25
import com .cloud .event .ActionEventUtils ;
26
+ import com .cloud .user .Account ;
26
27
import com .cloud .user .User ;
28
+ import com .cloud .utils .DateUtil ;
27
29
import com .cloud .utils .StringUtils ;
28
30
import com .cloud .utils .component .ComponentContext ;
29
31
import com .cloud .utils .component .ManagerBase ;
39
41
import org .apache .cloudstack .framework .jobs .AsyncJobManager ;
40
42
import org .apache .cloudstack .framework .jobs .impl .AsyncJobVO ;
41
43
import org .apache .cloudstack .managed .context .ManagedContextTimerTask ;
44
+ import org .apache .commons .lang3 .time .DateUtils ;
42
45
43
46
import javax .inject .Inject ;
44
47
import java .util .ArrayList ;
45
48
import java .util .Arrays ;
49
+ import java .util .Calendar ;
46
50
import java .util .Date ;
47
51
import java .util .HashMap ;
48
52
import java .util .List ;
@@ -138,17 +142,32 @@ protected void alert() {
138
142
return ;
139
143
}
140
144
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 ();
145
160
}
146
161
}
147
162
148
163
@ 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
+
151
169
if (!InstanceLeaseEnabled .value ()) {
170
+ logger .debug ("Instance lease feature is disabled, no action is required" );
152
171
return ;
153
172
}
154
173
@@ -168,16 +187,15 @@ public void poll(Date currentTimestamp) {
168
187
169
188
protected void reallyRun () {
170
189
// fetch user_instances having leaseDuration configured and has expired
171
- List <UserVmJoinVO > leaseExpiredInstances = userVmJoinDao .listExpiredInstancesIds ();
190
+ List <UserVmJoinVO > leaseExpiredInstances = userVmJoinDao .listLeaseExpiredInstances ();
172
191
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
174
193
for (UserVmJoinVO userVmVO : leaseExpiredInstances ) {
175
194
if (userVmVO .isDeleteProtection () != null && userVmVO .isDeleteProtection ()) {
176
195
logger .debug ("Ignoring instance with id: {} as deleteProtection is enabled" , userVmVO .getUuid ());
177
196
continue ;
178
197
}
179
198
// state check, include instances not yet stopped or destroyed
180
- // it can be done in fetch_user_instances as well
181
199
if (!Arrays .asList (Destroyed , Expunging , Unknown , VirtualMachine .State .Error ).contains (userVmVO .getState ())) {
182
200
actionableInstanceIds .add (userVmVO .getId ());
183
201
}
@@ -208,7 +226,7 @@ protected void reallyRun() {
208
226
failedToSubmitInstanceIds .add (instanceId );
209
227
}
210
228
}
211
- logger .debug ("Successfully submitted jobs for ids: {}" , submittedJobIds );
229
+ logger .debug ("Successfully submitted lease expiry jobs with ids: {}" , submittedJobIds );
212
230
if (!failedToSubmitInstanceIds .isEmpty ()) {
213
231
logger .debug ("Lease scheduler failed to submit jobs for instance ids: {}" , failedToSubmitInstanceIds );
214
232
}
@@ -227,7 +245,7 @@ Long executeExpiryAction(UserVmJoinVO instance, ExpiryAction expiryAction, long
227
245
}
228
246
default : {
229
247
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 ());
231
249
}
232
250
}
233
251
return null ;
@@ -236,8 +254,8 @@ Long executeExpiryAction(UserVmJoinVO instance, ExpiryAction expiryAction, long
236
254
long executeStopInstanceJob (UserVmJoinVO vm , boolean isForced , long eventId ) {
237
255
final Map <String , String > params = new HashMap <>();
238
256
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 ));
241
259
params .put (ApiConstants .CTX_START_EVENT_ID , String .valueOf (eventId ));
242
260
params .put (ApiConstants .FORCED , String .valueOf (isForced ));
243
261
final StopVMCmd cmd = new StopVMCmd ();
@@ -252,8 +270,8 @@ long executeStopInstanceJob(UserVmJoinVO vm, boolean isForced, long eventId) {
252
270
long executeDestroyInstanceJob (UserVmJoinVO vm , boolean isForced , long eventId ) {
253
271
final Map <String , String > params = new HashMap <>();
254
272
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 ));
257
275
params .put (ApiConstants .CTX_START_EVENT_ID , String .valueOf (eventId ));
258
276
params .put (ApiConstants .FORCED , String .valueOf (isForced ));
259
277
0 commit comments