Search in sources :

Example 1 with ServiceException

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();
}
Also used : PauseComponentRequest(software.amazon.awssdk.aws.greengrass.model.PauseComponentRequest) ServiceException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceException) Permission(com.aws.greengrass.authorization.Permission) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test)

Example 2 with ServiceException

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();
}
Also used : ServiceException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceException) Permission(com.aws.greengrass.authorization.Permission) ResumeComponentRequest(software.amazon.awssdk.aws.greengrass.model.ResumeComponentRequest) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test)

Example 3 with ServiceException

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);
    }
}
Also used : ServiceException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceException) IOException(java.io.IOException)

Example 4 with ServiceException

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);
                }
            }
        }
    }
}
Also used : ServiceException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceException) IOException(java.io.IOException)

Aggregations

ServiceException (com.aws.greengrass.lifecyclemanager.exceptions.ServiceException)4 Permission (com.aws.greengrass.authorization.Permission)2 IOException (java.io.IOException)2 Test (org.junit.jupiter.api.Test)2 EnabledOnOs (org.junit.jupiter.api.condition.EnabledOnOs)2 PauseComponentRequest (software.amazon.awssdk.aws.greengrass.model.PauseComponentRequest)1 ResumeComponentRequest (software.amazon.awssdk.aws.greengrass.model.ResumeComponentRequest)1