Skip to content

Commit

Permalink
#19 - CodexiaModule: transforming sendReview to saveReview && ext…
Browse files Browse the repository at this point in the history
…racting SendReviews cron (#38)
  • Loading branch information
iakunin authored Apr 17, 2020
1 parent 95ad97f commit c743679
Show file tree
Hide file tree
Showing 28 changed files with 497 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void run() {
)
.forEach(
review -> {
this.codexiaModule.sendReview(review);
this.codexiaModule.saveReview(review);
this.codexiaModule.sendMeta(
review.getCodexiaProject(),
"hacker-news-id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void run() {
)
.forEach(
review -> {
this.codexiaModule.sendReview(review);
this.codexiaModule.saveReview(review);
this.codexiaModule.sendMeta(
review.getCodexiaProject(),
"reddit-id",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/iakunin/codexiabot/bot/StarsUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected void processStatList(Deque<GithubRepoStat> deque) {
.setGithubRepo(deque.getLast().getGithubRepo())
.setGithubRepoStat(deque.getLast())
);
this.codexiaModule.sendReview(review);
this.codexiaModule.saveReview(review);
this.codexiaModule.sendMeta(
review.getCodexiaProject(),
"stars-count",
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/dev/iakunin/codexiabot/codexia/CodexiaModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@

public interface CodexiaModule {

// @todo #6 split codexiaModule.sendReview() to codexiaModule.createReview() and codexiaModule.sendReview()
// - codexiaModule.createReview() - just a saving to DB.
// - codexiaModule.sendReview() - real sending review to Codexia api.
// - codexiaModule.sendReview() must be used ONLY inside a cron-job.
// - In other code there should be only a codexiaModule.createReview() calls.
// - All the codexiaModule.createReview() calls MUST be inside a transaction/
void sendReview(CodexiaReview review);
// @todo #19 All the codexiaModule.createReview() calls MUST be inside a transaction
void saveReview(CodexiaReview review);

// @todo #19 sending meta should also be asynchronous (via cron)
// here should be only a saving Meta (as saving Review)
void sendMeta(CodexiaProject codexiaProject, String metaKey, String metaValue);

Optional<CodexiaProject> findCodexiaProject(GithubRepo repo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,35 @@

import dev.iakunin.codexiabot.codexia.entity.CodexiaProject;
import dev.iakunin.codexiabot.codexia.entity.CodexiaReview;
import dev.iakunin.codexiabot.codexia.entity.CodexiaReviewNotification;
import dev.iakunin.codexiabot.codexia.repository.CodexiaProjectRepository;
import dev.iakunin.codexiabot.codexia.repository.CodexiaReviewNotificationRepository;
import dev.iakunin.codexiabot.codexia.repository.CodexiaReviewRepository;
import dev.iakunin.codexiabot.codexia.sdk.CodexiaClient;
import dev.iakunin.codexiabot.github.GithubModule;
import dev.iakunin.codexiabot.github.entity.GithubRepo;
import feign.FeignException;
import java.util.List;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@AllArgsConstructor(onConstructor_={@Autowired})
public final class CodexiaModuleImpl implements CodexiaModule {

private static final int REVIEW_ALREADY_EXISTS_STATUS = 404;

private final CodexiaProjectRepository codexiaProjectRepository;

private final CodexiaReviewRepository codexiaReviewRepository;

private final CodexiaReviewNotificationRepository codexiaReviewNotificationRepository;

private final CodexiaClient codexiaClient;

private final GithubModule githubModule;

@Override
public void sendReview(CodexiaReview review) {
log.info("Got a review: {}", review);

final CodexiaReview savedReview = this.codexiaReviewRepository.save(review);

final CodexiaReviewNotification savedNotification = this.codexiaReviewNotificationRepository.save(
new CodexiaReviewNotification()
.setCodexiaReview(savedReview)
.setStatus(CodexiaReviewNotification.Status.NEW)
);

final ResponseEntity<String> response;
try {
response = this.codexiaClient.createReview(
savedReview.getCodexiaProject().getExternalId(),
savedReview.getText(),
savedReview.getUuid().toString()
);
} catch (FeignException e) {
log.warn(
"Exception occurred during review creation in Codexia; externalId='{}'",
review.getCodexiaProject().getExternalId(),
e
);
this.codexiaReviewNotificationRepository.save(
savedNotification
.setStatus(
e.status() == REVIEW_ALREADY_EXISTS_STATUS
? CodexiaReviewNotification.Status.SUCCESS
: CodexiaReviewNotification.Status.ERROR
)
.setResponseCode(e.status())
.setResponse(e.content() != null ? e.contentUTF8() : "")
);
return;
}

this.codexiaReviewNotificationRepository.save(
savedNotification
.setStatus(CodexiaReviewNotification.Status.SUCCESS)
.setResponseCode(response.getStatusCodeValue())
.setResponse(response.toString())
);
public void saveReview(CodexiaReview review) {
log.info("Saving a review: {}", review);
this.codexiaReviewRepository.save(review);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void run() {

this.codexiaReviewNotificationRepository.findAllByLastStatus(CodexiaReviewNotification.Status.ERROR)
.forEach(
reviewNotification -> this.codexiaModule.sendReview(reviewNotification.getCodexiaReview())
reviewNotification -> this.codexiaModule.saveReview(reviewNotification.getCodexiaReview())
);

log.info("Exiting from {}", this.getClass().getName());
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/dev/iakunin/codexiabot/codexia/cron/SendReviews.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package dev.iakunin.codexiabot.codexia.cron;

import dev.iakunin.codexiabot.codexia.entity.CodexiaReview;
import dev.iakunin.codexiabot.codexia.entity.CodexiaReviewNotification;
import dev.iakunin.codexiabot.codexia.repository.CodexiaReviewNotificationRepository;
import dev.iakunin.codexiabot.codexia.repository.CodexiaReviewRepository;
import dev.iakunin.codexiabot.codexia.sdk.CodexiaClient;
import feign.FeignException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@AllArgsConstructor(onConstructor_={@Autowired})
public final class SendReviews {

private static final int REVIEW_ALREADY_EXISTS_STATUS = 404;

private final CodexiaReviewRepository codexiaReviewRepository;

private final CodexiaReviewNotificationRepository codexiaReviewNotificationRepository;

private final CodexiaClient codexiaClient;

@Scheduled(cron="${app.cron.codexia.send-reviews:-}")
public void run() {
log.info("Running {}", this.getClass().getName());

this.codexiaReviewRepository
.findAllWithoutNotifications()
.forEach(this::process);

log.info("Exiting from {}", this.getClass().getName());
}

private void process(CodexiaReview review) {
final CodexiaReviewNotification savedNotification = this.saveNewNotification(review);
final ResponseEntity<String> response;
try {
response = this.codexiaClient.createReview(
review.getCodexiaProject().getExternalId(),
review.getText(),
review.getUuid().toString()
);
} catch (FeignException e) {
log.warn(
"Exception occurred during review creation in Codexia; externalId='{}'",
review.getCodexiaProject().getExternalId(),
e
);
this.saveExceptionalNotification(savedNotification, e);
return;
}
this.saveSuccessfulNotification(savedNotification, response);
}

private CodexiaReviewNotification saveNewNotification(CodexiaReview review) {
return this.codexiaReviewNotificationRepository.save(
new CodexiaReviewNotification()
.setCodexiaReview(review)
.setStatus(CodexiaReviewNotification.Status.NEW)
);
}

private void saveSuccessfulNotification(
CodexiaReviewNotification notification,
ResponseEntity<String> response
) {
this.codexiaReviewNotificationRepository.save(
notification
.setStatus(CodexiaReviewNotification.Status.SUCCESS)
.setResponseCode(response.getStatusCodeValue())
.setResponse(response.toString())
);
}

private void saveExceptionalNotification(
CodexiaReviewNotification notification,
FeignException e
) {
this.codexiaReviewNotificationRepository.save(
notification
.setStatus(
// @todo #19 rewrite via custom Feign exceptions
e.status() == REVIEW_ALREADY_EXISTS_STATUS
? CodexiaReviewNotification.Status.SUCCESS
: CodexiaReviewNotification.Status.ERROR
)
.setResponseCode(e.status())
.setResponse(e.content() != null ? e.contentUTF8() : "")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
import dev.iakunin.codexiabot.codexia.entity.CodexiaReview;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface CodexiaReviewRepository extends JpaRepository<CodexiaReview, Long> {

boolean existsByCodexiaProjectAndAuthorAndReason(CodexiaProject codexiaProject, String author, String reason);

List<CodexiaReview> findAllByCodexiaProjectAndAuthorOrderByIdAsc(CodexiaProject codexiaProject, String author);

@Query(
"select cr " +
"from CodexiaReview cr " +
"left join CodexiaReviewNotification crn " +
" on (cr = crn.codexiaReview) " +
"where crn.id is null " +
"order by cr.id asc"
)
List<CodexiaReview> findAllWithoutNotifications();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public void run() {

this.githubModule
.findAllInCodexia()
.forEach(this::updateStat)
;
.forEach(this::updateStat);

log.info("Exiting from {}", this.getClass().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
"type": "java.lang.String",
"description": "Cron expression for codexia.missing-filler."
},
{
"name": "app.cron.codexia.send-reviews",
"type": "java.lang.String",
"description": "Cron expression for codexia.send-reviews."
},
{
"name": "app.cron.codexia.resend-erroneous-reviews",
"type": "java.lang.String",
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-prod.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ app.cron.bot.stars-up=0 30 * * * *

app.cron.codexia.codexia-parser=45 * * * * *
app.cron.codexia.missing-filler=0 0 0 * * *
app.cron.codexia.send-reviews=55 * * * * *
app.cron.codexia.resend-erroneous-reviews=25 * * * * *
app.cron.codexia.projects-health-check=35 * * * * *

Expand Down
Loading

4 comments on commit c743679

@0pdd
Copy link

@0pdd 0pdd commented on c743679 Apr 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 6-02060b89 disappeared from src/main/java/dev/iakunin/codexiabot/codexia/CodexiaModule.java, that's why I closed #19. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on c743679 Apr 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 19-703b27a3 discovered in src/main/java/dev/iakunin/codexiabot/codexia/CodexiaModule.java and submitted as #39. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on c743679 Apr 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 19-ec246873 discovered in src/main/java/dev/iakunin/codexiabot/codexia/CodexiaModule.java and submitted as #40. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on c743679 Apr 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 19-799ed7e1 discovered in src/main/java/dev/iakunin/codexiabot/codexia/cron/SendReviews.java and submitted as #41. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.