Search in sources :

Example 6 with IpcSenderService

use of com.nextdoor.bender.ipc.IpcSenderService in project bender by Nextdoor.

the class BaseHandler method init.

/**
 * Loads @{link com.nextdoor.bender.config.Configuration} from a resource file and initializes
 * classes.
 *
 * @param ctx function context as specified when function is invoked by lambda.
 * @throws HandlerException error while loading the @{link
 *         com.nextdoor.bender.config.Configuration}.
 */
public void init(Context ctx) throws HandlerException {
    /*
     * Function alias is the last part of the Function ARN
     */
    String alias = null;
    String[] tokens = ctx.getInvokedFunctionArn().split(":");
    if (tokens.length == 7) {
        alias = "$LATEST";
    } else if (tokens.length == 8) {
        alias = tokens[7];
    }
    BenderLayout.ALIAS = alias;
    BenderLayout.VERSION = ctx.getFunctionVersion();
    /*
     * Create a new monitor and then get a static copy of it
     */
    monitor = Monitor.getInstance();
    monitor.addTag("functionName", ctx.getFunctionName());
    monitor.addTag("functionVersion", alias);
    String configFile;
    /*
     * TODO: Replace this to always use env vars. Code was written prior to
     * lambda env vars existing.
     */
    if (System.getenv("BENDER_CONFIG") != null) {
        configFile = System.getenv("BENDER_CONFIG");
    } else if (CONFIG_FILE == null) {
        configFile = "/config/" + alias;
    } else {
        configFile = CONFIG_FILE;
    }
    logger.info(String.format("Bender Initializing (config: %s)", configFile));
    try {
        if (configFile.startsWith("s3://")) {
            config = BenderConfig.load(s3ClientFactory, new AmazonS3URI(configFile));
        } else {
            config = BenderConfig.load(configFile);
        }
    } catch (ConfigurationException e) {
        throw new HandlerException("Error loading configuration: " + e.getMessage(), e);
    }
    HandlerResources handlerResources;
    try {
        handlerResources = new HandlerResources(config);
    } catch (ClassNotFoundException e) {
        throw new HandlerException("Unable to load resource: " + e.getMessage(), e);
    }
    /*
     * Register reporters
     */
    monitor.addReporters(handlerResources.getReporters());
    /*
     * Init other things
     */
    wrapper = handlerResources.getWrapperFactory().newInstance();
    ser = handlerResources.getSerializerProcessor();
    setIpcService(new IpcSenderService(handlerResources.getTransportFactory()));
    sources = new ArrayList<Source>(handlerResources.getSources().values());
    initialized = true;
}
Also used : IpcSenderService(com.nextdoor.bender.ipc.IpcSenderService) ConfigurationException(com.nextdoor.bender.config.ConfigurationException) AmazonS3URI(com.amazonaws.services.s3.AmazonS3URI) Source(com.nextdoor.bender.config.Source) HandlerResources(com.nextdoor.bender.config.HandlerResources)

Example 7 with IpcSenderService

use of com.nextdoor.bender.ipc.IpcSenderService in project bender by Nextdoor.

the class SNSS3HandlerTest method testExceptionHandlingd.

@Test
public void testExceptionHandlingd() throws Throwable {
    BaseHandler.CONFIG_FILE = "/com/nextdoor/bender/handler/config_test_sns.json";
    TestContext ctx = new TestContext();
    ctx.setFunctionName("unittest");
    ctx.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test-function:staging");
    /*
     * Invoke handler
     */
    SNSS3Handler fhandler = (SNSS3Handler) getHandler();
    fhandler.init(ctx);
    IpcSenderService ipcSpy = spy(fhandler.getIpcService());
    doThrow(new TransportException("expected")).when(ipcSpy).shutdown();
    fhandler.setIpcService(ipcSpy);
    AmazonSNSClient mockClient = mock(AmazonSNSClient.class);
    AmazonSNSClientFactory mockClientFactory = mock(AmazonSNSClientFactory.class);
    doReturn(mockClient).when(mockClientFactory).newInstance();
    fhandler.snsClientFactory = mockClientFactory;
    SNSEvent event = getTestEvent();
    try {
        fhandler.handler(event, ctx);
    } catch (Exception e) {
    }
    verify(mockClient, times(1)).publish("foo", "basic_input.log", "SNSS3Handler Failed");
}
Also used : IpcSenderService(com.nextdoor.bender.ipc.IpcSenderService) AmazonSNSClientFactory(com.nextdoor.bender.aws.AmazonSNSClientFactory) TestContext(com.nextdoor.bender.aws.TestContext) AmazonSNSClient(com.amazonaws.services.sns.AmazonSNSClient) TransportException(com.nextdoor.bender.ipc.TransportException) TransportException(com.nextdoor.bender.ipc.TransportException) SNSEvent(com.amazonaws.services.lambda.runtime.events.SNSEvent) HandlerTest(com.nextdoor.bender.handler.HandlerTest) Test(org.junit.Test)

Aggregations

IpcSenderService (com.nextdoor.bender.ipc.IpcSenderService)7 TestContext (com.nextdoor.bender.aws.TestContext)6 Test (org.junit.Test)6 TransportException (com.nextdoor.bender.ipc.TransportException)5 ArrayList (java.util.ArrayList)4 DeserializationException (com.nextdoor.bender.deserializer.DeserializationException)3 OperationException (com.nextdoor.bender.operation.OperationException)3 SerializationException (com.nextdoor.bender.serializer.SerializationException)3 IOException (java.io.IOException)3 TransportFactory (com.nextdoor.bender.ipc.TransportFactory)2 SNSEvent (com.amazonaws.services.lambda.runtime.events.SNSEvent)1 AmazonS3URI (com.amazonaws.services.s3.AmazonS3URI)1 AmazonSNSClient (com.amazonaws.services.sns.AmazonSNSClient)1 AmazonSNSClientFactory (com.nextdoor.bender.aws.AmazonSNSClientFactory)1 ConfigurationException (com.nextdoor.bender.config.ConfigurationException)1 HandlerResources (com.nextdoor.bender.config.HandlerResources)1 Source (com.nextdoor.bender.config.Source)1 HandlerTest (com.nextdoor.bender.handler.HandlerTest)1 TransportBuffer (com.nextdoor.bender.ipc.TransportBuffer)1 ArrayTransportBuffer (com.nextdoor.bender.testutils.DummyTransportHelper.ArrayTransportBuffer)1