use of net.petafuel.styx.keepalive.entities.TaskRetryFailureException in project styx by petafuel.
the class ConsentPoll method execute.
public void execute() {
LOG.debug("Executing Task id:{} signature:{}", getId(), getSignature());
Consent currentConsent = persistentConsent.get(consent);
if (currentConsent == null) {
throw new TaskFinalFailureException("Consent queued for polling does not exist in the styx database, cannot poll", TaskFinalFailureCode.POLL_ON_NOT_EXISTING_CONSENT);
} else if (currentConsent.getState() == ConsentStatus.VALID) {
throw new TaskFinalFailureException("Consent with id " + currentConsent.getId() + " is already on state valid, no polling required", TaskFinalFailureCode.POLL_ON_ALREADY_VALID_CONSENT);
}
// TODO make the request type as a parameter
StatusConsentRequest statusConsentRequest = new StatusConsentRequest(null, consent.getId(), null, null);
statusConsentRequest.setPsu(consent.getPsu());
Iterator<Integer> retryIterator = IntStream.range(0, maxRequestRetries).iterator();
while (retryIterator.hasNext()) {
if (Thread.interrupted()) {
throw new TaskFinalFailureException("Task Thread was interrupted");
}
try {
ConsentStatus currentStatus = this.csInterface.getStatus(statusConsentRequest);
if (currentStatus == ConsentStatus.VALID) {
LOG.debug("Consent is valid, SCA was successful");
break;
} else if (!(currentStatus.equals(ConsentStatus.RECEIVED) || currentStatus.equals(ConsentStatus.PARTIALLY_AUTHORISED))) {
currentConsent.setState(currentStatus);
persistentConsent.update(currentConsent);
throw new TaskFinalFailureException("Consent cannot be polled anymore due to unrecoverable status: " + currentStatus.toString(), TaskFinalFailureCode.UNRECOVERABLE_STATUS);
}
LOG.debug("Consent status was not valid: {}", currentStatus);
} catch (BankRequestFailedException e) {
LOG.warn("Trying to poll consent resulted in an error: {} status: {} retry-iteration: {}", e.getMessage(), e.getHttpStatusCode(), retryIterator.next());
}
try {
Thread.sleep(timeoutBetweenRetries);
} catch (InterruptedException e) {
LOG.error("Task {} execution was interrupted: {}", getId(), e.getMessage());
Thread.currentThread().interrupt();
}
retryIterator.next();
}
// TODO make GetConsentRequest class as a parameter
GetConsentRequest getConsentRequest = new GetConsentRequest(null, consent.getId(), null, null);
try {
Consent aspspConsent = csInterface.getConsent(getConsentRequest);
if (!aspspConsent.getState().equals(ConsentStatus.VALID)) {
throw new TaskRetryFailureException("Upon Consent Poll completion, consent is still not authorized by PSU");
}
currentConsent.setFrequencyPerDay(aspspConsent.getFrequencyPerDay());
currentConsent.setValidUntil(aspspConsent.getValidUntil());
currentConsent.setState(aspspConsent.getState());
persistentConsent.update(currentConsent);
LOG.info("Successfully updated consent");
} catch (BankRequestFailedException e) {
LOG.error("Unable to get consent information after polling was successful -> task marked as failed");
throw new TaskRetryFailureException("Unable to get consent information after polling was successful", e);
}
}
use of net.petafuel.styx.keepalive.entities.TaskRetryFailureException in project styx by petafuel.
the class CoreWorker method run.
// try-catch required for error handling
@SuppressWarnings("java:S3776")
@Override
public void run() {
Thread.currentThread().setName("KeepAlive-Worker-" + getType().toString() + "-" + getId().toString());
LOG.info("Started CoreWorker id: {}", this.getId());
setRunning(true);
while (running.get()) {
if (ThreadManager.getInstance().getCoreQueue().isEmpty()) {
LOG.info("No polling from queue: Queue is empty -> ideling/waiting");
try {
synchronized (ThreadManager.getInstance().getCoreQueue()) {
ThreadManager.getInstance().getCoreQueue().wait();
}
} catch (InterruptedException e) {
LOG.error("CoreWorker {} was interrupted", this.getId());
Thread.currentThread().interrupt();
}
}
WorkableTask task;
synchronized (ThreadManager.getInstance().getCoreQueue()) {
task = ThreadManager.getInstance().getCoreQueue().poll();
}
if (task == null) {
continue;
}
currentTask.set(task);
LOG.info("Task id:{} signature:{} polled from queue", task.getId(), task.getSignature());
try {
TaskRecoveryDB.updateState(task.getId(), TaskState.RUNNING);
currentTaskStartTime.set(new Date().getTime());
task.execute();
LOG.info("Task id:{} signature: {} finished successfully", task.getId(), task.getSignature());
currentTaskStartTime.set(0);
TaskRecoveryDB.updateState(task.getId(), TaskState.DONE);
} catch (TaskRetryFailureException retryFailure) {
LOG.warn("Task id: {} signature: {} failed but will be requeued as RETRY_FAILURE -> {}", task.getId(), task.getSignature(), retryFailure.getMessage());
TaskRecoveryDB.updateWorker(task.getId(), WorkerType.RETRY_FAILURE);
ThreadManager.getInstance().queueTask(task, WorkerType.RETRY_FAILURE);
} catch (TaskFinalFailureException finalFailure) {
LOG.error("Task id: {} signature: {} finally failed with code:{} -> {}", task.getId(), task.getSignature(), finalFailure.getCode(), finalFailure.getMessage());
TaskRecoveryDB.setFinallyFailed(task.getId(), finalFailure.getMessage(), finalFailure.getCode());
} catch (Throwable throwable) {
LOG.error("Task id: {} signature: {} encountered an unexpected error", task.getId(), task.getSignature(), throwable);
TaskRecoveryDB.setFinallyFailed(task.getId(), throwable.getMessage(), TaskFinalFailureCode.UNKNOWN);
}
}
LOG.info("Terminated CoreWorker id: {}", this.getId());
}
use of net.petafuel.styx.keepalive.entities.TaskRetryFailureException in project styx by petafuel.
the class RetryFailureWorker method run.
// try-catch required for error handling
@SuppressWarnings("java:S3776")
@Override
public void run() {
Thread.currentThread().setName("KeepAlive-Worker-" + getType().toString() + "-" + getId().toString());
LOG.info("Started RetryFailureWorker id: {}", this.getId());
this.setRunning(true);
while (running.get()) {
if (ThreadManager.getInstance().getRetryFailureQueue().isEmpty()) {
LOG.info("No polling from queue: Queue is empty -> ideling/waiting");
try {
synchronized (ThreadManager.getInstance().getRetryFailureQueue()) {
ThreadManager.getInstance().getRetryFailureQueue().wait();
}
} catch (InterruptedException e) {
LOG.error("RetryFailure Worker {} was interrupted", this.getId());
Thread.currentThread().interrupt();
}
}
WorkableTask task;
synchronized (ThreadManager.getInstance().getRetryFailureQueue()) {
task = ThreadManager.getInstance().getRetryFailureQueue().poll();
}
if (task == null) {
continue;
}
currentTask.set(task);
LOG.info("Task id:{} signature:{} polled from queue", task.getId(), task.getSignature());
try {
if (TaskRecoveryDB.incrementExecutionCounter(task.getId()) >= maxRetriesPerTask) {
throw new TaskFinalFailureException("Maximum amount of executions by a RetryFailureWorker was reached for task: " + task.getId(), TaskFinalFailureCode.EXCEEDED_MAX_RETRIES_THROUGH_RETRYFAILUREWORKER);
}
TaskRecoveryDB.updateState(task.getId(), TaskState.RUNNING);
currentTaskStartTime.set(new Date().getTime());
task.execute();
LOG.info("Task id:{} signature: {} finished successfully", task.getId(), task.getSignature());
currentTaskStartTime.set(0);
TaskRecoveryDB.updateState(task.getId(), TaskState.DONE);
} catch (TaskRetryFailureException retryFailure) {
LOG.warn("Task id: {} signature: {} failed but will be requeued as RETRY_FAILURE -> {}", task.getId(), task.getSignature(), retryFailure.getMessage());
TaskRecoveryDB.updateWorker(task.getId(), WorkerType.RETRY_FAILURE);
ThreadManager.getInstance().queueTask(task, WorkerType.RETRY_FAILURE);
} catch (TaskFinalFailureException finalFailure) {
LOG.error("Task id: {} signature: {} finally failed with code:{} -> {}", task.getId(), task.getSignature(), finalFailure.getCode(), finalFailure.getMessage());
TaskRecoveryDB.setFinallyFailed(task.getId(), finalFailure.getMessage(), finalFailure.getCode());
} catch (Throwable throwable) {
Throwable cause = throwable.getCause();
String causeMessage = cause != null ? cause.getMessage() : "";
LOG.error("Task id: {} signature: {} encountered an unexpected error exception={}, message={}, cause={}, causeMessage={}", task.getId(), task.getSignature(), throwable.getClass().getSimpleName(), throwable.getMessage(), cause, causeMessage);
TaskRecoveryDB.setFinallyFailed(task.getId(), throwable.getMessage(), TaskFinalFailureCode.UNKNOWN);
}
}
LOG.info("Terminated RetryFailureWorker id: {}", this.getId());
}
Aggregations