1 Commits

Author SHA1 Message Date
1cef92c449 feat: add cron job to resend unsent emails 2025-10-23 11:04:53 +01:00

View File

@ -0,0 +1,81 @@
/*
* Axelor Business Solutions
*
* Copyright (C) 2019 Axelor (<http://axelor.com>).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.axelor.apps.message.job;
import com.axelor.apps.message.db.Message;
import com.axelor.apps.message.db.repo.MessageRepository;
import com.axelor.apps.message.service.MessageService;
import com.axelor.exception.service.TraceBackService;
import com.google.inject.Inject;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.List;
import javax.mail.MessagingException;
import com.axelor.exception.AxelorException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** An example {@link Job} class that prints a some messages to the stderr. */
public class SendEmailJob implements Job {
private final Logger log = LoggerFactory.getLogger(SendEmailJob.class);
@Inject private MessageService messageService;
@Inject private MessageRepository messageRepository;
@Override
public void execute(JobExecutionContext context) {
LocalDate today = LocalDate.now();
LocalDateTime startOfDay = today.atStartOfDay();
List<Message> draftMessages = messageRepository
.all()
.filter("self.statusSelect = 1 AND self.createdOn >= :startOfDay")
.bind("startOfDay", startOfDay)
.fetch();
log.debug("Total of draft messages : {}", draftMessages.size());
for (Message message : draftMessages) {
try {
Message m = messageService.sendMessage(message);
} catch (AxelorException e) {
TraceBackService.trace(e);
}
}
List<Message> inProgressMessages = messageRepository
.all()
.filter("self.statusSelect = 2 AND self.createdOn >= :startOfDay")
.bind("startOfDay", startOfDay)
.fetch();
log.debug("Total of in progress messages : {}", inProgressMessages.size());
for (Message message : inProgressMessages) {
try {
Message m = messageService.sendMessage(message);
} catch (AxelorException e) {
TraceBackService.trace(e);
}
}
}
}