-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/net/dancier/kikeriki/adapter/out/infomail/InfoMailCheckJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
22 changes: 21 additions & 1 deletion
22
src/main/java/net/dancier/kikeriki/adapter/out/infomail/InfomailAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/net/dancier/kikeriki/adapter/out/infomail/ScheduledInfoMailCheckJpaEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...n/java/net/dancier/kikeriki/adapter/out/infomail/ScheduledInfoMailCheckJpaRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/net/dancier/kikeriki/adapter/out/userinfo/UserInfoAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/net/dancier/kikeriki/application/port/UserInfoDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/net/dancier/kikeriki/application/port/UserInfoPort.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters