Apache Nutch is an extensible and scalable web crawler
Apply patch for NUTCH-2031 Create Admin End point for Nutch 1.x REST service contributed by Sujen Shah <[email protected]> this closes #26.
Chris Mattmann16b73c1b8be2
diff --git a/CHANGES.txt b/CHANGES.txtindex d549d138a..0e24bc977 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogNutch Current Development 1.11-SNAPSHOT+* NUTCH-2031 Create Admin End point for Nutch 1.x REST service (Sujen Shah via mattmann)+* NUTCH-2015 Make FetchNodeDb optional (off by default) if NutchServer is not used (Sujen Shah via mattmann)* NUTCH-208 http: proxy exception list: (Matthias Günter, siren, markus, lewismc)diff --git a/src/java/org/apache/nutch/service/NutchServer.java b/src/java/org/apache/nutch/service/NutchServer.javaindex 308f1d75a..14f84d45b 100644--- a/src/java/org/apache/nutch/service/NutchServer.java+++ b/src/java/org/apache/nutch/service/NutchServer.java@@ -19,6 +19,7 @@ package org.apache.nutch.service;import java.util.ArrayList;+import java.util.Collection;import java.util.List;import java.util.concurrent.BlockingQueue;import java.util.concurrent.TimeUnit;@@ -42,6 +43,9 @@ import org.apache.nutch.service.impl.ConfManagerImpl;import org.apache.nutch.service.impl.JobFactory;import org.apache.nutch.service.impl.JobManagerImpl;import org.apache.nutch.service.impl.NutchServerPoolExecutor;+import org.apache.nutch.service.model.response.JobInfo;+import org.apache.nutch.service.model.response.JobInfo.State;+import org.apache.nutch.service.resources.AdminResource;import org.apache.nutch.service.resources.ConfigResource;import org.apache.nutch.service.resources.DbResource;import org.apache.nutch.service.resources.JobResource;@@ -59,11 +63,11 @@ public class NutchServer {private static final int JOB_CAPACITY = 100;private static Integer port = DEFAULT_PORT;- private static String host = LOCALHOST;+ private static String host = LOCALHOST;private static final String CMD_HELP = "help";private static final String CMD_PORT = "port";- private static final String CMD_HOST = "host";+ private static final String CMD_HOST = "host";private long started;private boolean running;@@ -72,7 +76,7 @@ public class NutchServer {private JAXRSServerFactoryBean sf;private static FetchNodeDb fetchNodeDb;-+private static NutchServer server;static {@@ -107,9 +111,9 @@ public class NutchServer {}private void start() {- LOG.info("Starting NutchServer on {}:{} ...", host, port);+ LOG.info("Starting NutchServer on {}:{} ...", host, port);try{- String address = "http://" + host + ":" + port;+ String address = "http://" + host + ":" + port;sf.setAddress(address);sf.create();}catch(Exception e){@@ -118,8 +122,8 @@ public class NutchServer {started = System.currentTimeMillis();running = true;- LOG.info("Started Nutch Server on {}:{} at {}", host, port, started);- System.out.println("Started Nutch Server on " + host + ":" + port + " at " + started);+ LOG.info("Started Nutch Server on {}:{} at {}", host, port, started);+ System.out.println("Started Nutch Server on " + host + ":" + port + " at " + started);}private List<Class<?>> getClasses() {@@ -127,6 +131,7 @@ public class NutchServer {resources.add(JobResource.class);resources.add(ConfigResource.class);resources.add(DbResource.class);+ resources.add(AdminResource.class);return resources;}@@ -147,7 +152,7 @@ public class NutchServer {public FetchNodeDb getFetchNodeDb(){return fetchNodeDb;}-+public boolean isRunning(){return running;}@@ -170,9 +175,9 @@ public class NutchServer {port = Integer.parseInt(commandLine.getOptionValue(CMD_PORT));}- if (commandLine.hasOption(CMD_HOST)) {- host = commandLine.getOptionValue(CMD_HOST);- }+ if (commandLine.hasOption(CMD_HOST)) {+ host = commandLine.getOptionValue(CMD_HOST);+ }startServer();}@@ -188,12 +193,27 @@ public class NutchServer {OptionBuilder.withDescription("The port to run the Nutch Server. Default port 8081");options.addOption(OptionBuilder.create(CMD_PORT));- OptionBuilder.withArgName("host");- OptionBuilder.hasOptionalArg();- OptionBuilder.withDescription("The host to bind the Nutch Server to. Default is localhost.");- options.addOption(OptionBuilder.create(CMD_PORT));+ OptionBuilder.withArgName("host");+ OptionBuilder.hasOptionalArg();+ OptionBuilder.withDescription("The host to bind the Nutch Server to. Default is localhost.");+ options.addOption(OptionBuilder.create(CMD_PORT));return options;}+ public boolean canStop(boolean force){+ if(force)+ return true;++ Collection<JobInfo> jobs = getJobManager().list(null, State.RUNNING);+ return jobs.isEmpty();+ }++ public int getPort() {+ return port;+ }++ public void stop() {+ System.exit(0);+ }}diff --git a/src/java/org/apache/nutch/service/resources/AbstractResource.java b/src/java/org/apache/nutch/service/resources/AbstractResource.javaindex 3ef203bbb..ebe41383d 100644--- a/src/java/org/apache/nutch/service/resources/AbstractResource.java+++ b/src/java/org/apache/nutch/service/resources/AbstractResource.java@@ -31,8 +31,10 @@ public abstract class AbstractResource {protected JobManager jobManager;protected ConfManager configManager;+ protected NutchServer server;public AbstractResource() {+ server = NutchServer.getInstance();configManager = NutchServer.getInstance().getConfManager();jobManager = NutchServer.getInstance().getJobManager();}diff --git a/src/java/org/apache/nutch/service/resources/AdminResource.java b/src/java/org/apache/nutch/service/resources/AdminResource.javanew file mode 100644index 000000000..9483342d9--- /dev/null+++ b/src/java/org/apache/nutch/service/resources/AdminResource.java@@ -0,0 +1,76 @@+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.nutch.service.resources;++import java.util.Date;++import javax.ws.rs.GET;+import javax.ws.rs.Path;+import javax.ws.rs.QueryParam;++import org.apache.nutch.service.model.response.JobInfo.State;+import org.apache.nutch.service.model.response.NutchServerInfo;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++@Path(value="/admin")+public class AdminResource extends AbstractResource{++ private final int DELAY_SEC = 1;+ private static final Logger LOG = LoggerFactory+ .getLogger(AdminResource.class);++ @GET+ @Path(value="/")+ public NutchServerInfo getServerStatus(){+ NutchServerInfo serverInfo = new NutchServerInfo();+ serverInfo.setConfiguration(configManager.list());+ serverInfo.setStartDate(new Date(server.getStarted()));+ serverInfo.setJobs(jobManager.list(null, State.ANY));+ serverInfo.setRunningJobs(jobManager.list(null, State.RUNNING));+ return serverInfo;+ }++ @GET+ @Path(value="/stop")+ public String stopServer(@QueryParam("force") boolean force){+ if(!server.canStop(force)){+ return "Jobs still running -- Cannot stop server now" ;+ }+ scheduleServerStop();+ return "Stopping in server on port " + server.getPort();+ }++ private void scheduleServerStop() {+ LOG.info("Shutting down server in {} sec", DELAY_SEC);+ Thread thread = new Thread() {+ public void run() {+ try {+ Thread.sleep(DELAY_SEC*1000);+ } catch (InterruptedException e) {+ Thread.currentThread().interrupt();+ }+ server.stop();+ LOG.info("Service stopped.");+ }+ };+ thread.setDaemon(true);+ thread.start();+ LOG.info("Service shutting down...");+ }++}