Skip to content

Commit

Permalink
Feature/recheck (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
gorzala authored Mar 31, 2024
2 parents 375ca11 + 56f50fd commit 9c1ede4
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
package net.dancier.kikeriki.adapter.out.infomail;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import net.dancier.kikeriki.application.CheckAndSendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
@RequiredArgsConstructor
public class InfoMailCheckJob {
private static final Logger log = LoggerFactory.getLogger(InfoMailCheckJob.class);

private final ScheduledInfoMailCheckJpaRepository scheduledInfoMailCheckJpaRepository;

private final CheckAndSendService checkAndSendService;

@Scheduled(fixedRate = 5000L)
public void check() {
log.info("checking");
Collection<ScheduledInfoMailCheckJpaEntity> scheduledEntities = scheduledInfoMailCheckJpaRepository.lockAndList();
log.info("After the check..." + scheduledEntities);
for(ScheduledInfoMailCheckJpaEntity scheduledInfoMailCheckJpaEntity: scheduledEntities) {
checkAndSend(scheduledInfoMailCheckJpaEntity);
}
}

private void checkAndSend(ScheduledInfoMailCheckJpaEntity scheduledInfoMailCheckJpaEntity) {
try {
checkAndSendService.checkAndSend(scheduledInfoMailCheckJpaEntity.getDancerId());
log.info("Sucess setting status to done");
scheduledInfoMailCheckJpaEntity.setStatus(ScheduledInfoMailCheckJpaEntity.STATUS.DONE);
} catch (Exception exception) {
log.info("Failure setting status to failed: " + exception);
scheduledInfoMailCheckJpaEntity.setStatus(ScheduledInfoMailCheckJpaEntity.STATUS.FINALLY_FAILED);
} finally {
scheduledInfoMailCheckJpaRepository.save(scheduledInfoMailCheckJpaEntity);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
package net.dancier.kikeriki.adapter.out.infomail;

import lombok.RequiredArgsConstructor;
import net.dancier.kikeriki.application.port.ScheduleInfomailCheckPort;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

@Component
@RequiredArgsConstructor
public class InfomailAdapter implements ScheduleInfomailCheckPort {

Logger log = LoggerFactory.getLogger(InfomailAdapter.class);
private final static Logger log = LoggerFactory.getLogger(InfomailAdapter.class);

private final ScheduledInfoMailCheckJpaRepository scheduledInfoMailCheckJpaRepository;

@Override
public void schedule(LocalDateTime when, String dancerId) {
Objects.requireNonNull(when);
Objects.requireNonNull(dancerId);
log.info("Scheduling check!!!");

if (scheduledInfoMailCheckJpaRepository.findByDancerIdAndStatus(dancerId, ScheduledInfoMailCheckJpaEntity.STATUS.NEW).isEmpty()) {
log.info("Adding new check...");
ScheduledInfoMailCheckJpaEntity scheduledInfoMailCheckJpaEntity = new ScheduledInfoMailCheckJpaEntity();
scheduledInfoMailCheckJpaEntity.setStatus(ScheduledInfoMailCheckJpaEntity.STATUS.NEW);
scheduledInfoMailCheckJpaEntity.setDancerId(dancerId);
scheduledInfoMailCheckJpaEntity.setCheckAt(when);

scheduledInfoMailCheckJpaRepository.save(scheduledInfoMailCheckJpaEntity);
} else {
log.info("Skip adding a new check, as we have one already");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.dancier.kikeriki.adapter.out.infomail;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.UUID;

@Data
@Entity
@Table(name = "scheduled_at")
@NoArgsConstructor
public class ScheduledInfoMailCheckJpaEntity {

@Id
@GeneratedValue
private UUID id;

@NotNull
@Enumerated(EnumType.STRING)
private STATUS status;

@NotNull
private String dancerId;

@NotNull
private LocalDateTime checkAt;

public static enum STATUS {
NEW,
IN_PROGRESS,
TEMPORARY_FAILED,
FINALLY_FAILED,
DONE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.dancier.kikeriki.adapter.out.infomail;

import net.dancier.kikeriki.adapter.out.mail.MailOutboxJpaEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Collection;
import java.util.List;
import java.util.UUID;

public interface ScheduledInfoMailCheckJpaRepository extends JpaRepository<ScheduledInfoMailCheckJpaEntity, UUID> {

List<ScheduledInfoMailCheckJpaEntity> findByDancerIdAndStatus(String dancerId, ScheduledInfoMailCheckJpaEntity.STATUS status);

@Query(
value = """
UPDATE scheduled_at
SET status = 'IN_PROGRESS'
WHERE id IN (
SELECT id
FROM scheduled_at
WHERE status = 'NEW'
LIMIT 1
FOR UPDATE
)
RETURNING *;
""",
nativeQuery = true
)
Collection<ScheduledInfoMailCheckJpaEntity> lockAndList();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.dancier.kikeriki.adapter.out.userinfo;

import net.dancier.kikeriki.application.port.UserInfoDto;
import net.dancier.kikeriki.application.port.UserInfoPort;
import org.springframework.stereotype.Component;

@Component
public class UserInfoAdapter implements UserInfoPort {
@Override
public UserInfoDto loadByDancerId(String dancerId) {
UserInfoDto userInfoDto = new UserInfoDto();
userInfoDto.setDancerId(dancerId);
userInfoDto.setEmailAddress("dancer@dancier.net");
return userInfoDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import net.dancier.kikeriki.adapter.out.userinfo.UserInfoAdapter;
import net.dancier.kikeriki.application.domain.model.messages.EmailSendingRequestedCommand;
import net.dancier.kikeriki.application.domain.model.state.State;
import net.dancier.kikeriki.application.port.StatePort;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.UUID;

@Component
@RequiredArgsConstructor
Expand All @@ -17,6 +21,10 @@ public class CheckAndSendService {

private final StatePort statePort;

private final UserInfoAdapter userInfoAdapter;

private final ApplicationEventPublisher applicationEventPublisher;

@Transactional
public void checkAndSend(String dancerId) {
log.info("CheckAndSend for {}", dancerId);
Expand All @@ -28,5 +36,15 @@ public void checkAndSend(String dancerId) {
}
public void sendMail(String dancerId) {
log.info("Sending InfoMail...");
String mailAddress = userInfoAdapter.loadByDancerId(dancerId).getEmailAddress();
EmailSendingRequestedCommand emailSendingRequestedCommand =
new EmailSendingRequestedCommand.EmailSendingRequestedCommandBuilder()
.setFrom("marc@gorzala.de")
.setId(UUID.randomUUID().toString())
.setTo(new String[]{mailAddress})
.setSubject("Infomail")
.setText("Hallo Welt")
.build();
applicationEventPublisher.publishEvent(emailSendingRequestedCommand);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.dancier.kikeriki.application.port;

import lombok.Data;

@Data
public class UserInfoDto {

private String dancerId;

private String emailAddress;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.dancier.kikeriki.application.port;

public interface UserInfoPort {

UserInfoDto loadByDancerId(String dancerId);

}
10 changes: 10 additions & 0 deletions src/main/resources/liquibase-changeLog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,15 @@
);
</sql>
</changeSet>
<changeSet id="2024-03-28" author="Marc Gorzala">
<sql>
CREATE TABLE scheduled_at (
id UUID PRIMARY KEY,
status VARCHAR(256) NOT NULL,
dancer_id VARCHAR(256) NOT NULL,
check_at TIMESTAMP WITHOUT TIME ZONE
);
</sql>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public void newUnreadMessage() {
assertThat(resultingState.getLastTimeOfInfoMail()).isEmpty();
assertThat(resultingState.isCandidateForSendingMail(LocalDateTime.now())).isTrue();


}

}

0 comments on commit 9c1ede4

Please sign in to comment.