Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

created a simple health check that can be used a smoke test, resolves…

… #119
  • Loading branch information...
commit cc1dd83a79308c3bcb9fd999401014e3eabd2fb8 1 parent 60eb51d
@ddossot ddossot authored
View
10 src/main/java/eu/openanalytics/rsb/Constants.java
@@ -20,12 +20,12 @@
*/
package eu.openanalytics.rsb;
+import java.net.InetAddress;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.activation.MimeType;
-import javax.activation.MimeTypeParseException;
/**
* Useful constants.
@@ -46,6 +46,7 @@
public final static String JOBS_PATH = "jobs";
public final static String RESULTS_PATH = "results";
public final static String RESULT_PATH = "result";
+ public final static String HEALTH_PATH = "system/health";
public static final String SOURCE_MESSAGE_HEADER = "source";
public static final String APPLICATION_NAME_MESSAGE_HEADER = "applicationName";
@@ -78,6 +79,8 @@
public static final MimeType ZIP_MIME_TYPE;
public static final MimeType DEFAULT_MIME_TYPE;
+ public static final String RSERVI_CLIENT_ID;
+
static {
try {
JSON_MIME_TYPE = new MimeType(JSON_CONTENT_TYPE);
@@ -86,7 +89,10 @@
PDF_MIME_TYPE = new MimeType(PDF_CONTENT_TYPE);
ZIP_MIME_TYPE = new MimeType(ZIP_CONTENT_TYPE);
DEFAULT_MIME_TYPE = new MimeType("application/octet-stream");
- } catch (final MimeTypeParseException mtpe) {
+
+ RSERVI_CLIENT_ID = "rsb@" + InetAddress.getLocalHost().getHostName();
+
+ } catch (final Exception mtpe) {
throw new IllegalStateException(mtpe);
}
}
View
10 src/main/java/eu/openanalytics/rsb/component/JobProcessor.java
@@ -28,9 +28,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
-import java.net.InetAddress;
import java.net.URI;
-import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.HashSet;
@@ -77,12 +75,6 @@
@Resource
private JobStatisticsHandler jobStatisticsHandler;
- private final String rServiClientId;
-
- public JobProcessor() throws UnknownHostException {
- rServiClientId = "rsb@" + InetAddress.getLocalHost().getHostName();
- }
-
public void process(final AbstractFunctionCallJob job) throws Exception {
process(job, new JobRunner() {
public AbstractResult<String> runOn(final RServi rServi) throws CoreException, IOException {
@@ -180,7 +172,7 @@ private void process(final AbstractJob job, final JobRunner jobRunner) throws Lo
final URI rserviPoolAddress = getRServiPoolUri(job.getApplicationName());
// don't catch RServi pool here so the error is propagated and the job can be retried
- final RServi rServi = rServiInstanceProvider.getRServiInstance(rserviPoolAddress.toString(), rServiClientId);
+ final RServi rServi = rServiInstanceProvider.getRServiInstance(rserviPoolAddress.toString(), Constants.RSERVI_CLIENT_ID);
try {
result = jobRunner.runOn(rServi);
View
66 src/main/java/eu/openanalytics/rsb/component/SystemHealthResource.java
@@ -0,0 +1,66 @@
+/*
+ * R Service Bus
+ *
+ * Copyright (c) Copyright of OpenAnalytics BVBA, 2010-2011
+ *
+ * ===========================================================================
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package eu.openanalytics.rsb.component;
+
+import javax.annotation.Resource;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+
+import org.springframework.stereotype.Component;
+
+import de.walware.rj.servi.RServi;
+import eu.openanalytics.rsb.Constants;
+import eu.openanalytics.rsb.rservi.RServiInstanceProvider;
+
+/**
+ * Handles health check requests.
+ *
+ * @author "OpenAnalytics &lt;rsb.development@openanalytics.eu&gt;"
+ */
+@Component("systemHealthResource")
+@Path("/" + Constants.HEALTH_PATH)
+public class SystemHealthResource extends AbstractComponent {
+ @Resource
+ private RServiInstanceProvider rServiInstanceProvider;
+
+ // exposed for unit testing
+ void setRServiInstanceProvider(final RServiInstanceProvider rServiInstanceProvider) {
+ this.rServiInstanceProvider = rServiInstanceProvider;
+ }
+
+ @GET
+ @Path("/check")
+ @Produces({ Constants.TEXT_CONTENT_TYPE })
+ public Response check() {
+ try {
+ final RServi rServi = rServiInstanceProvider.getRServiInstance(getConfiguration().getDefaultRserviPoolUri().toString(),
+ Constants.RSERVI_CLIENT_ID);
+ rServi.close();
+ return Response.ok("OK").build();
+ } catch (final Exception e) {
+ getLogger().error("RSB is in bad health!", e);
+ return Response.status(Status.INTERNAL_SERVER_ERROR).entity("ERROR").build();
+ }
+ }
+}
View
1  src/main/resources/META-INF/spring/cxf-beans.xml
@@ -58,6 +58,7 @@
<ref bean="jobsResource" />
<ref bean="resultsResource" />
<ref bean="resultResource" />
+ <ref bean="systemHealthResource"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider" />
View
5 src/site/xdoc/install.xml
@@ -33,6 +33,11 @@
<body>
<section name="Deployment">
<p>RSB should deploy fine on any Java web-container supporting Servlet 2.5.</p>
+ <p>To ensure an RSB instance is running and can access its default RServi pool, browse to or curl:</p>
+ <source>
+http://${RSB host}/rsb/api/rest/system/health/check
+ </source>
+ <p>If all is good, RSB should respond: OK</p>
</section>
<section name="Configuration">
<p>
View
3  src/test/java/eu/openanalytics/rsb/component/JobProcessorTestCase.java
@@ -34,7 +34,6 @@
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
-import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Collections;
import java.util.UUID;
@@ -82,7 +81,7 @@
private JobStatisticsHandler jobStatisticsHandler;
@Before
- public void prepareTest() throws UnknownHostException {
+ public void prepareTest() {
jobProcessor = new JobProcessor();
jobProcessor.setConfiguration(configuration);
jobProcessor.setMessageDispatcher(messageDispatcher);
View
88 src/test/java/eu/openanalytics/rsb/component/SystemHealthResourceTestCase.java
@@ -0,0 +1,88 @@
+/*
+ * R Service Bus
+ *
+ * Copyright (c) Copyright of OpenAnalytics BVBA, 2010-2011
+ *
+ * ===========================================================================
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package eu.openanalytics.rsb.component;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import javax.ws.rs.core.Response;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import de.walware.rj.servi.RServi;
+import eu.openanalytics.rsb.config.Configuration;
+import eu.openanalytics.rsb.rservi.RServiInstanceProvider;
+
+/**
+ * @author "OpenAnalytics &lt;rsb.development@openanalytics.eu&gt;"
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class SystemHealthResourceTestCase {
+
+ private SystemHealthResource systemHealthResource;
+
+ @Mock
+ private Configuration configuration;
+ @Mock
+ private RServiInstanceProvider rServiInstanceProvider;
+
+ @Before
+ public void prepareTest() throws URISyntaxException {
+ systemHealthResource = new SystemHealthResource();
+ systemHealthResource.setConfiguration(configuration);
+ systemHealthResource.setRServiInstanceProvider(rServiInstanceProvider);
+
+ final URI defaultPoolUri = new URI("fake://default");
+ when(configuration.getDefaultRserviPoolUri()).thenReturn(defaultPoolUri);
+ }
+
+ @Test
+ public void happyCheck() throws Exception {
+ final RServi rServi = mock(RServi.class);
+ when(rServiInstanceProvider.getRServiInstance(anyString(), anyString())).thenReturn(rServi);
+
+ final Response checkResult = systemHealthResource.check();
+
+ assertThat(checkResult.getStatus(), is(200));
+ assertThat(checkResult.getEntity().toString(), is("OK"));
+ }
+
+ @Test
+ public void unhappyCheck() throws Exception {
+ when(rServiInstanceProvider.getRServiInstance(anyString(), anyString())).thenThrow(
+ new RuntimeException("simulated RServi provider issue"));
+
+ final Response checkResult = systemHealthResource.check();
+
+ assertThat(checkResult.getStatus(), is(500));
+ assertThat(checkResult.getEntity().toString(), is("ERROR"));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.