Skip to content

Commit

Permalink
In Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyrsy committed Oct 4, 2021
1 parent 98ae0e7 commit 3d3e173
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ public Response webhook(String json, @Context HttpServletRequest request) throws

// TODO: remove once done testing
FailedRequestService failedRequestService = env.failedRequestService();
log.info("Persisting event...");
failedRequestService.persist(event, Event::getId, JSONObject::new);
log.info("Persisted!");
failedRequestService.persist(event, Event::getId, JSONObject::new, "test");

EventDataObjectDeserializer dataObjectDeserializer = event.getDataObjectDeserializer();
StripeObject stripeObject;
Expand All @@ -101,7 +99,7 @@ public Response webhook(String json, @Context HttpServletRequest request) throws
} catch (Exception e) {
log.error("failed to process the Stripe event", e);
// TODO: email notification?
failedRequestService.persist(event, Event::getId, JSONObject::new);
failedRequestService.persist(event, Event::getId, JSONObject::new, e.getMessage());
}
};
new Thread(thread).start();
Expand Down
27 changes: 12 additions & 15 deletions src/main/java/com/impactupgrade/nucleus/dao/FailedRequestDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.impactupgrade.nucleus.model.FailedRequest;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.springframework.util.StringUtils;
Expand All @@ -22,9 +23,9 @@ public FailedRequestDao() {
@Override
public FailedRequest create(FailedRequest failedRequest, Optional<Long> ttl) {
final Session session = sessionFactory.openSession();
session.beginTransaction();
Transaction transaction = session.beginTransaction();
session.save(failedRequest);
session.getTransaction().commit();
transaction.commit();
session.close();

// session.getTransaction().begin();
Expand Down Expand Up @@ -68,9 +69,9 @@ public Optional<FailedRequest> get(String id) {
@Override
public FailedRequest update(FailedRequest failedRequest) {
final Session session = sessionFactory.openSession();
session.beginTransaction();
Transaction transaction = session.beginTransaction();
session.update(failedRequest);
session.getTransaction().commit();
transaction.commit();
session.close();

// session.getTransaction().begin();
Expand All @@ -93,16 +94,12 @@ public FailedRequest update(FailedRequest failedRequest) {

@Override
public Optional<FailedRequest> delete(String id) {
Optional<FailedRequest> failedRequestO = get(id);
if (failedRequestO.isPresent()) {
FailedRequest failedRequest = failedRequestO.get();
final Session session = sessionFactory.openSession();
session.beginTransaction();
session.delete(failedRequest);
session.getTransaction().commit();
session.flush();
session.close();
}
final Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
FailedRequest failedRequest = session.get(FailedRequest.class, id);
session.delete(failedRequest);
transaction.commit();
session.close();

// Session session = sessionFactory.openSession();
// session.beginTransaction();
Expand All @@ -122,6 +119,6 @@ public Optional<FailedRequest> delete(String id) {
// session.getTransaction().commit();
// session.close();

return failedRequestO;
return Optional.ofNullable(failedRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class FailedRequest {
private String id;
@Column(name = "payload", nullable = false)
private String payload;
@Column(name = "error_message", nullable = false)
private String errorMessage;
@Column(name = "attempt_count")
private int attemptCount;
@Column(name = "first_attempt_time", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public FailedRequestService(Environment env) {

public <T> void persist(T t,
Function<T, String> getUniqueKeyFunction,
Function<T, JSONObject> toJSONObjectFunction) {
Function<T, JSONObject> toJSONObjectFunction,
String errorMessage) {
if (Objects.isNull(t) || Objects.isNull(getUniqueKeyFunction)) {
return;
}
Expand All @@ -35,7 +36,7 @@ public <T> void persist(T t,
JSONObject jsonPayload = getJsonPayload(t, toJSONObjectFunction);
// and save
try {
upsert(newFailedRequest(id, jsonPayload));
upsert(newFailedRequest(id, jsonPayload, errorMessage));
} catch (Exception e) {
log.info("Failed to persist!" + e);
}
Expand Down Expand Up @@ -83,7 +84,7 @@ private <T> JSONObject getJsonPayload(T t, Function<T, JSONObject> toJSONObjectF
return jsonObject;
}

private FailedRequest newFailedRequest(String id, JSONObject payload) {
private FailedRequest newFailedRequest(String id, JSONObject payload, String errorMessage) {
FailedRequest failedRequest = new FailedRequest();
failedRequest.setId(id);
failedRequest.setPayload(payload.toString());
Expand All @@ -93,22 +94,22 @@ private FailedRequest newFailedRequest(String id, JSONObject payload) {
return failedRequest;
}

private void upsert(FailedRequest failedResuest) {
String id = failedResuest.getId();
private void upsert(FailedRequest failedRequest) {
String id = failedRequest.getId();
log.info("Checking if record exists for id {}...", id);
Optional<FailedRequest> existingRecord = failedRequestDao.get(id);
if (existingRecord.isEmpty()) {
log.info("Will create a new record...");
failedRequestDao.create(failedResuest, Optional.empty());
failedRequestDao.create(failedRequest, Optional.empty());
log.info("Created!");
} else {
log.info("Will update existing id {}...", id);

int attempts = existingRecord.get().getAttemptCount() + 1;
failedResuest.setAttemptCount(attempts);
failedResuest.setLastAttemptTime(new Date());
failedRequest.setAttemptCount(attempts);
failedRequest.setLastAttemptTime(new Date());

failedRequestDao.update(failedResuest);
failedRequestDao.update(failedRequest);
log.info("Updated!");
}
}
Expand Down

0 comments on commit 3d3e173

Please sign in to comment.