use of com.aws.greengrass.lifecyclemanager.exceptions.ServiceException in project aws-greengrass-nucleus by aws-greengrass.
the class LifecycleIPCEventStreamAgentTest method GIVEN_pause_component_request_WHEN_failure_THEN_return_service_error.
@Test
@EnabledOnOs(OS.LINUX)
void GIVEN_pause_component_request_WHEN_failure_THEN_return_service_error() throws AuthorizationException, ServiceException {
when(kernel.locate(TEST_TARGET_COMPONENT)).thenReturn(targetComponent);
when(targetComponent.getState()).thenReturn(State.RUNNING);
doThrow(new ServiceException("Failed to pause")).when(targetComponent).pause();
when(authorizationHandler.isAuthorized(any(), any())).thenReturn(true);
PauseComponentRequest request = new PauseComponentRequest();
request.setComponentName(TEST_TARGET_COMPONENT);
assertThrows(ServiceError.class, () -> lifecycleIPCEventStreamAgent.getPauseComponentHandler(mockContext).handleRequest(request));
ArgumentCaptor<Permission> permissionArg = ArgumentCaptor.forClass(Permission.class);
verify(authorizationHandler).isAuthorized(eq(LIFECYCLE_SERVICE_NAME), permissionArg.capture());
Permission permission = permissionArg.getValue();
assertThat(permission.getOperation(), is(GreengrassCoreIPCService.PAUSE_COMPONENT));
assertThat(permission.getPrincipal(), is(TEST_SERVICE));
assertThat(permission.getResource(), is(TEST_TARGET_COMPONENT));
verify(kernel).locate(TEST_TARGET_COMPONENT);
verify(targetComponent).getState();
verify(targetComponent).pause();
}
use of com.aws.greengrass.lifecyclemanager.exceptions.ServiceException in project aws-greengrass-nucleus by aws-greengrass.
the class LifecycleIPCEventStreamAgentTest method GIVEN_resume_component_request_WHEN_failure_THEN_return_service_error.
@Test
@EnabledOnOs(OS.LINUX)
void GIVEN_resume_component_request_WHEN_failure_THEN_return_service_error() throws AuthorizationException, ServiceException {
when(kernel.locate(TEST_TARGET_COMPONENT)).thenReturn(targetComponent);
when(targetComponent.isPaused()).thenReturn(true);
doThrow(new ServiceException("Failed to resume")).when(targetComponent).resume();
when(authorizationHandler.isAuthorized(any(), any())).thenReturn(true);
ResumeComponentRequest request = new ResumeComponentRequest();
request.setComponentName(TEST_TARGET_COMPONENT);
assertThrows(ServiceError.class, () -> lifecycleIPCEventStreamAgent.getResumeComponentHandler(mockContext).handleRequest(request));
ArgumentCaptor<Permission> permissionArg = ArgumentCaptor.forClass(Permission.class);
verify(authorizationHandler).isAuthorized(eq(LIFECYCLE_SERVICE_NAME), permissionArg.capture());
Permission permission = permissionArg.getValue();
assertThat(permission.getOperation(), is(GreengrassCoreIPCService.RESUME_COMPONENT));
assertThat(permission.getPrincipal(), is(TEST_SERVICE));
assertThat(permission.getResource(), is(TEST_TARGET_COMPONENT));
verify(kernel).locate(TEST_TARGET_COMPONENT);
verify(targetComponent).isPaused();
verify(targetComponent).resume();
}
use of com.aws.greengrass.lifecyclemanager.exceptions.ServiceException in project aws-greengrass-nucleus by aws-greengrass.
the class GenericExternalService method pause.
/**
* Pause a running component.
*
* @throws ServiceException Error processing pause request.
*/
public synchronized void pause() throws ServiceException {
logger.atDebug().log("Pausing running component");
if (paused.get()) {
return;
}
try {
List<Process> processes = lifecycleProcesses.stream().map(Exec::getProcess).collect(Collectors.toList());
systemResourceController.pauseComponentProcesses(this, processes);
paused.set(true);
logger.atDebug().log("Paused component");
} catch (IOException e) {
logger.atError().setCause(e).log("Error pausing component");
throw new ServiceException(String.format("Error pausing component %s", getServiceName()), e);
}
}
use of com.aws.greengrass.lifecyclemanager.exceptions.ServiceException in project aws-greengrass-nucleus by aws-greengrass.
the class GenericExternalService method resume.
private synchronized void resume(boolean restartOnFail, boolean retryOnFail) throws ServiceException {
logger.atDebug().log("Resuming component");
if (paused.get()) {
int retryAttempts = 3;
while (true) {
retryAttempts--;
try {
systemResourceController.resumeComponentProcesses(this);
paused.set(false);
logger.atDebug().log("Resumed component");
return;
} catch (IOException e) {
if (retryOnFail && retryAttempts > 0) {
logger.atInfo().setCause(e).log("Error resuming component, retrying");
} else {
logger.atError().setCause(e).log("Error resuming component and all retried exhausted, " + "restarting");
if (restartOnFail) {
// Reset tracking flag
paused.set(false);
requestRestart();
}
throw new ServiceException(String.format("Error resuming component %s", getServiceName()), e);
}
}
}
}
}
Aggregations