GaOrtiga commented on code in PR #13939:
URL: https://github.com/apache/cloudstack/pull/13939#discussion_r3845364807


##########
server/src/main/java/org/apache/cloudstack/backup/BackupReportServiceImpl.java:
##########
@@ -0,0 +1,463 @@
+//Licensed to the Apache Software Foundation (ASF) under one
+//or more contributor license agreements.  See the NOTICE file
+//distributed with this work for additional information
+//regarding copyright ownership.  The ASF licenses this file
+//to you under the Apache License, Version 2.0 (the
+//"License"); you may not use this file except in compliance
+//the License.  You may obtain a copy of the License at
+//
+//http://www.apache.org/licenses/LICENSE-2.0
+//
+//Unless required by applicable law or agreed to in writing,
+//software distributed under the License is distributed on an
+//"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+//KIND, either express or implied.  See the License for the
+//specific language governing permissions and limitations
+//under the License.
+package org.apache.cloudstack.backup;
+
+import com.cloud.dc.DataCenterVO;
+import com.cloud.dc.dao.DataCenterDao;
+import com.cloud.domain.DomainVO;
+import com.cloud.domain.dao.DomainDao;
+import com.cloud.projects.ProjectVO;
+import com.cloud.projects.dao.ProjectDao;
+import com.cloud.user.Account;
+import com.cloud.user.AccountVO;
+import com.cloud.user.dao.AccountDao;
+import com.cloud.utils.DateUtil;
+import com.cloud.utils.UuidUtils;
+import com.cloud.utils.component.ManagerBase;
+import com.cloud.utils.concurrency.NamedThreadFactory;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.db.TransactionCallbackNoReturn;
+import com.cloud.utils.db.TransactionLegacy;
+import com.cloud.utils.db.TransactionStatus;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.vm.VMInstanceVO;
+import com.cloud.vm.dao.UserVmDao;
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+import freemarker.template.TemplateException;
+import org.apache.cloudstack.api.response.BackupReportAccountResponse;
+import org.apache.cloudstack.api.response.BackupReportDomainResponse;
+import org.apache.cloudstack.api.response.BackupReportResponse;
+import org.apache.cloudstack.api.response.BackupResponse;
+import org.apache.cloudstack.api.response.BackupScheduleResponse;
+import org.apache.cloudstack.backup.dao.BackupReportDao;
+import org.apache.cloudstack.backup.dao.BackupReportJoinDao;
+import org.apache.cloudstack.backup.dao.BackupScheduleDao;
+import org.apache.cloudstack.email.template.EmailTemplateVO;
+import org.apache.cloudstack.email.template.dao.EmailTemplateDao;
+import org.apache.cloudstack.framework.config.ConfigKey;
+import org.apache.cloudstack.framework.config.Configurable;
+import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
+import org.apache.cloudstack.utils.mailing.MailAddress;
+import org.apache.cloudstack.utils.mailing.SMTPMailProperties;
+import org.apache.cloudstack.utils.mailing.SMTPMailSender;
+import org.apache.logging.log4j.ThreadContext;
+
+import javax.inject.Inject;
+import javax.naming.ConfigurationException;
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.time.temporal.ChronoUnit;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TimeZone;
+import java.util.UUID;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+public class BackupReportServiceImpl extends ManagerBase implements 
Configurable, BackupReportService {
+
+    protected ConfigKey<Boolean> backupReportTaskEnabled = new 
ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Boolean.class,
+            "backup.report.task.enabled", "false", "Whether the backup report 
task should run or not.", true, ConfigKey.Scope.Zone);
+
+    protected ConfigKey<Integer> backupReportPeriod = new 
ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Integer.class,
+            "backup.report.period", "1440", "The period of time, in minutes, 
between two executions of the backup report task. The report will always 
contain all information" +
+            " regarding backups between the last execution and the current 
execution. If the task was disabled, the report will contain information up to 
the period configured.",
+            true, ConfigKey.Scope.Global);
+
+    protected ConfigKey<Integer> backupReportTimeout = new 
ConfigKey<>(ConfigKey.CATEGORY_ADVANCED, Integer.class, 
"backup.report.timeout", "10", "Timeout, in minutes, of the " +
+            "backup report task.", true, ConfigKey.Scope.Global);
+
+    private static final String LOCK = "backup_report_lock";
+    private static final String TEMPLATE_NAME = "backup_report_template";
+    protected static final String LOGCONTEXTID = "logcontextid";
+    private static final double GIB = (1024 * 1024 * 1024);
+
+    private final List<Backup.Status> aliveBackupStates = 
List.of(Backup.Status.BackedUp, Backup.Status.Restoring);
+    private final List<Backup.Status> errorBackupStates = 
List.of(Backup.Status.Error, Backup.Status.Failed);
+
+    private ScheduledExecutorService scheduledExecutor;
+
+    @Inject
+    private BackupReportDao backupReportDao;
+
+    @Inject
+    private DomainDao domainDao;
+
+    @Inject
+    private AccountDao accountDao;
+
+    @Inject
+    private ProjectDao projectDao;
+
+    @Inject
+    private UserVmDao userVmDao;
+
+    @Inject
+    private DataCenterDao dataCenterDao;
+
+    @Inject
+    private BackupReportJoinDao backupReportJoinDao;
+
+    @Inject
+    private BackupScheduleDao backupScheduleDao;
+
+    @Inject
+    private ConfigurationDao configurationDao;
+
+    @Inject
+    private EmailTemplateDao emailTemplateDao;
+
+    @Inject
+    private BackupManager backupManager;
+
+    private SMTPMailSender mailSender;
+    private String senderAddress;
+    private String[] recipients;
+    private Configuration freemarkerConfig;
+
+    public BackupReportServiceImpl () {
+    };
+
+    @Override
+    public boolean configure(String name, Map<String, Object> params) throws 
ConfigurationException {
+        super.configure(name, params);
+        this.freemarkerConfig = new 
Configuration(Configuration.VERSION_2_3_34);
+
+        Map<String, String> configs = 
configurationDao.getConfiguration("management-server", params);
+
+        senderAddress = configs.get("alert.email.sender");
+        String emailAddressList = configs.get("alert.email.addresses");
+        recipients = null;
+        if (emailAddressList != null) {
+            recipients = emailAddressList.split(",");
+        } else {
+            logger.warn("No recipients set in global setting 
'alert.email.addresses', skipping running backup report task.");
+            return true;
+        }
+
+        String namespace = "alert.smtp";
+
+        mailSender = new SMTPMailSender(configs, namespace);
+
+        scheduledExecutor = Executors.newSingleThreadScheduledExecutor(new 
NamedThreadFactory("BackupCompressionScheduler"));
+        scheduledExecutor.schedule(this::run, 60, TimeUnit.SECONDS);
+        return true;
+    }
+
+    protected void run() {
+        ThreadContext.put(LOGCONTEXTID, 
UuidUtils.first(UUID.randomUUID().toString()));
+        logger.info("Starting backup report task.");

Review Comment:
   This runs even if the task is disabled. Could we add the disable check 
before this? can be a little confusing for the operator



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to