JoaoJandre commented on code in PR #11223:
URL: https://github.com/apache/cloudstack/pull/11223#discussion_r2236481247


##########
server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java:
##########
@@ -654,19 +638,104 @@ public boolean createBackup(final Long vmId, final Long 
scheduleId) throws Resou
             Backup backup = result.second();
             if (backup != null) {
                 BackupVO vmBackup = 
backupDao.findById(result.second().getId());
-                vmBackup.setBackupIntervalType((short) type.ordinal());
+                vmBackup.setBackupScheduleId(backupScheduleId);
                 backupDao.update(vmBackup.getId(), vmBackup);
                 resourceLimitMgr.incrementResourceCount(vm.getAccountId(), 
Resource.ResourceType.backup);
                 resourceLimitMgr.incrementResourceCount(vm.getAccountId(), 
Resource.ResourceType.backup_storage, backup.getSize());
             }
-            if (type != Backup.Type.MANUAL) {
-                postCreateScheduledBackup(type, vm.getId());
+            if (isScheduledBackup) {
+                deleteOldestBackupFromScheduleIfRequired(vmId, 
backupScheduleId);
             }
             return true;
         }
         throw new CloudRuntimeException("Failed to create VM backup");
     }
 
+    /**
+     * Sends an alert when the backup limit has been exceeded for a given 
account.
+     *
+     * @param ownerUuid The UUID of the account owner that exceeded the limit
+     * @param resourceType The type of resource limit that was exceeded 
(either {@link Resource.ResourceType#backup} or {@link 
Resource.ResourceType#backup_storage})
+     *
+     */
+    protected void sendExceededBackupLimitAlert(String ownerUuid, 
Resource.ResourceType resourceType) {
+        String message = String.format("Failed to create backup: backup %s 
limit exceeded for account with ID: %s.",
+                resourceType == Resource.ResourceType.backup ? "resource" : 
"storage space resource" , ownerUuid);
+        logger.warn(message);
+        
alertManager.sendAlert(AlertManager.AlertType.ALERT_TYPE_UPDATE_RESOURCE_COUNT, 
0L, 0L,
+                message, message + " Please, use the 'updateResourceLimit' API 
to increase the backup limit.");
+    }
+
+    /**
+     * Gets the backup schedule ID from the async job's payload.
+     *
+     * @param job The asynchronous job associated with the creation of the 
backup
+     * @return The backup schedule ID. Returns null if the backup has been 
manually created
+     */
+    protected Long getBackupScheduleId(Object job) {
+        AsyncJobVO asyncJob = (AsyncJobVO) job;

Review Comment:
   Just to be sure, we could check if the object really is an instance of 
AsyncJobVO before casting it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to