-
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.
Persisting all incoming events. Updating error ones.
- Loading branch information
1 parent
18523f0
commit c1d6ad1
Showing
6 changed files
with
121 additions
and
102 deletions.
There are no files selected for viewing
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
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
77 changes: 0 additions & 77 deletions
77
src/main/java/com/impactupgrade/nucleus/service/logic/FailedRequestService.java
This file was deleted.
Oops, something went wrong.
90 changes: 90 additions & 0 deletions
90
src/main/java/com/impactupgrade/nucleus/service/logic/WebhookRequestService.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,90 @@ | ||
package com.impactupgrade.nucleus.service.logic; | ||
|
||
import com.google.common.base.Strings; | ||
import com.impactupgrade.nucleus.dao.WebhookRequestDao; | ||
import com.impactupgrade.nucleus.environment.Environment; | ||
import com.impactupgrade.nucleus.model.WebhookRequest; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.json.JSONObject; | ||
|
||
import java.util.Date; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class WebhookRequestService { | ||
|
||
private static final Logger log = LogManager.getLogger(WebhookRequestService.class); | ||
|
||
private Environment env; | ||
private WebhookRequestDao webhookRequestDao; | ||
|
||
public WebhookRequestService(Environment env) { | ||
this.env = env; | ||
this.webhookRequestDao = new WebhookRequestDao(); | ||
} | ||
|
||
public void persist(String id, String payloadType, JSONObject jsonObject) { | ||
if (Strings.isNullOrEmpty(id) || Strings.isNullOrEmpty(payloadType) || Objects.isNull(jsonObject)) { | ||
return; | ||
} | ||
try { | ||
upsert(newWebhookRequest(id, payloadType, jsonObject)); | ||
} catch (Exception e) { | ||
log.error("Failed to persist webhook request!", e); | ||
} | ||
} | ||
|
||
public void updateWithErrorMessage(String id, String errorMessage) { | ||
if (Strings.isNullOrEmpty(id)) { | ||
return; | ||
} | ||
try { | ||
Optional<WebhookRequest> existingRecord = webhookRequestDao.get(id); | ||
if (existingRecord.isPresent()) { | ||
WebhookRequest webhookRequest = existingRecord.get(); | ||
webhookRequest.setErrorMessage(errorMessage); | ||
webhookRequestDao.update(webhookRequest); | ||
} | ||
} catch (Exception e) { | ||
log.error("Failed to update webhook request!", e); | ||
} | ||
} | ||
|
||
public void delete(String id) { | ||
if (Strings.isNullOrEmpty(id)) { | ||
return; | ||
} | ||
try { | ||
webhookRequestDao.delete(id); | ||
} catch (Exception e) { | ||
log.error("Failed to delete failed request!", e); | ||
} | ||
} | ||
|
||
// Utils | ||
private WebhookRequest newWebhookRequest(String id, String payloadType, JSONObject payload) { | ||
WebhookRequest webhookRequest = new WebhookRequest(); | ||
webhookRequest.setId(id); | ||
webhookRequest.setPayloadType(payloadType); | ||
webhookRequest.setPayload(payload.toString()); | ||
webhookRequest.setAttemptCount(1); | ||
webhookRequest.setFirstAttemptTime(new Date()); | ||
webhookRequest.setLastAttemptTime(new Date()); | ||
return webhookRequest; | ||
} | ||
|
||
private void upsert(WebhookRequest webhookRequest) { | ||
String id = webhookRequest.getId(); | ||
Optional<WebhookRequest> existingRecord = webhookRequestDao.get(id); | ||
if (existingRecord.isEmpty()) { | ||
webhookRequestDao.create(webhookRequest); | ||
} else { | ||
int attempts = existingRecord.get().getAttemptCount() + 1; | ||
webhookRequest.setAttemptCount(attempts); | ||
webhookRequest.setLastAttemptTime(new Date()); | ||
webhookRequestDao.update(webhookRequest); | ||
} | ||
} | ||
|
||
} |