Skip to content

Commit

Permalink
Apply patch for NUTCH-2031 Create Admin End point for Nutch 1.x REST …
Browse files Browse the repository at this point in the history
…service contributed by Sujen Shah <sujen1412@gmail.com> this closes #26.

git-svn-id: https://svn.apache.org/repos/asf/nutch/trunk@1683218 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
chrismattmann committed Jun 3, 2015
1 parent 0ee4f42 commit 16b73c1
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Nutch Change Log

Nutch 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)
Expand Down
50 changes: 35 additions & 15 deletions src/java/org/apache/nutch/service/NutchServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
Expand All @@ -42,6 +43,9 @@
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;
Expand All @@ -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;
Expand All @@ -72,7 +76,7 @@ public class NutchServer {
private JAXRSServerFactoryBean sf;

private static FetchNodeDb fetchNodeDb;

private static NutchServer server;

static {
Expand Down Expand Up @@ -107,9 +111,9 @@ private static void startServer() {
}

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){
Expand All @@ -118,15 +122,16 @@ private void start() {

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() {
List<Class<?>> resources = new ArrayList<Class<?>>();
resources.add(JobResource.class);
resources.add(ConfigResource.class);
resources.add(DbResource.class);
resources.add(AdminResource.class);
return resources;
}

Expand All @@ -147,7 +152,7 @@ public JobManager getJobManager() {
public FetchNodeDb getFetchNodeDb(){
return fetchNodeDb;
}

public boolean isRunning(){
return running;
}
Expand All @@ -170,9 +175,9 @@ public static void main(String[] args) throws ParseException {
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();
}
Expand All @@ -188,12 +193,27 @@ private static Options createOptions() {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
76 changes: 76 additions & 0 deletions src/java/org/apache/nutch/service/resources/AdminResource.java
Original file line number Diff line number Diff line change
@@ -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...");
}

}

0 comments on commit 16b73c1

Please sign in to comment.