Skip to content

Commit

Permalink
Add thread done & cancelled information
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuel-keller committed Oct 22, 2017
1 parent 90599aa commit a951437
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 13 deletions.
6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>qwazr-crawlers</artifactId>
Expand Down Expand Up @@ -64,7 +65,8 @@
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>exec</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.qwazr.crawler.web.WebCrawlerServer</Main-Class>
<Implementation-Version>${project.version}-${buildNumber}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/qwazr/crawler/common/CrawlManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public S runSession(final String sessionName, final D crawlDefinition) throws Se
statusHistory.remove(sessionName);
if (!newThread.get())
throw new ServerException(Response.Status.CONFLICT, "The session already exists: " + sessionName);
executorService.execute(crawlThread);

crawlThread.start();
return crawlThread.getStatus(true);
}

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/qwazr/crawler/common/CrawlSessionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;

public class CrawlSessionImpl<D extends CrawlDefinition, S extends CrawlStatus<D>> implements CrawlSession {
Expand Down Expand Up @@ -153,9 +154,13 @@ public TimeTracker getTimeTracker() {
return timeTracker;
}

public void done() {
void done() {
crawlStatusBuilder.done();
buildStatus();
}

void setFuture(Future<?> future) {
crawlStatusBuilder.future(future);
buildStatus();
}
}
41 changes: 38 additions & 3 deletions src/main/java/com/qwazr/crawler/common/CrawlStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.qwazr.utils.TimeTracker;

import java.util.Objects;
import java.util.concurrent.Future;

@JsonInclude(Include.NON_NULL)
public abstract class CrawlStatus<T extends CrawlDefinition> {
Expand Down Expand Up @@ -85,6 +86,12 @@ public abstract class CrawlStatus<T extends CrawlDefinition> {
@JsonProperty("end_time")
final public Long endTime;

@JsonProperty("thread_cancelled")
final public Boolean threadCancelled;

@JsonProperty("thread_done")
final public Boolean threadDone;

final public TimeTracker.Status timer;

/**
Expand All @@ -100,7 +107,9 @@ protected CrawlStatus(@JsonProperty("node_address") String nodeAddress, @JsonPro
@JsonProperty("redirect") Integer redirect, @JsonProperty("error") Integer error,
@JsonProperty("last_error") String lastError, @JsonProperty("current_crawl") String currentCrawl,
@JsonProperty("start_time") final Long startTime, @JsonProperty("end_time") final Long endTime,
@JsonProperty("current_depth") Integer currentDepth, @JsonProperty("crawl_definition") T crawlDefinition) {
@JsonProperty("current_depth") Integer currentDepth, @JsonProperty("crawl_definition") T crawlDefinition,
@JsonProperty("thread_cancelled") Boolean threadCancelled,
@JsonProperty("thread_done") Boolean threadDone) {
this.nodeAddress = nodeAddress;
this.timer = timer;
this.aborting = aborting;
Expand All @@ -115,9 +124,11 @@ protected CrawlStatus(@JsonProperty("node_address") String nodeAddress, @JsonPro
this.startTime = startTime;
this.endTime = endTime;
this.crawlDefinition = crawlDefinition;
this.threadCancelled = threadCancelled;
this.threadDone = threadDone;
}

public CrawlStatus(AbstractBuilder<T, ?, ?> builder, boolean withCrawlDefinition) {
protected CrawlStatus(AbstractBuilder<T, ?, ?> builder, boolean withCrawlDefinition) {
this.nodeAddress = builder.nodeAddress;
this.timer = builder.timeTracker == null ? null : builder.timeTracker.getStatus();
this.aborting = builder.aborting;
Expand All @@ -132,6 +143,13 @@ public CrawlStatus(AbstractBuilder<T, ?, ?> builder, boolean withCrawlDefinition
this.startTime = builder.startTime;
this.endTime = builder.endTime;
this.crawlDefinition = withCrawlDefinition ? builder.crawlDefinition : null;
if (builder.future == null) {
this.threadDone = null;
this.threadCancelled = null;
} else {
this.threadDone = builder.future.isDone();
this.threadCancelled = builder.future.isCancelled();
}
}

/**
Expand Down Expand Up @@ -225,6 +243,16 @@ public T getCrawlDefinition() {
return crawlDefinition;
}

@JsonIgnore
public Boolean getThreadCancelled() {
return threadCancelled;
}

@JsonIgnore
public Boolean getThreadDone() {
return threadDone;
}

@Override
public boolean equals(final Object o) {
if (o == null || !(o instanceof CrawlStatus))
Expand All @@ -237,7 +265,8 @@ public boolean equals(final Object o) {
crawled == s.crawled && ignored == s.ignored && error == s.error &&
Objects.equals(lastError, s.lastError) && Objects.equals(currentCrawl, s.currentCrawl) &&
Objects.equals(currentDepth, s.currentDepth) && Objects.equals(endTime, s.endTime) &&
Objects.equals(timer, s.timer) && Objects.equals(crawlDefinition, s.crawlDefinition);
Objects.equals(timer, s.timer) && Objects.equals(crawlDefinition, s.crawlDefinition) &&
Objects.equals(threadCancelled, s.threadCancelled) && Objects.equals(threadDone, s.threadDone);
}

public abstract static class AbstractBuilder<D extends CrawlDefinition, S extends CrawlStatus<D>, B extends AbstractBuilder<D, S, ?>> {
Expand All @@ -258,6 +287,7 @@ public abstract static class AbstractBuilder<D extends CrawlDefinition, S extend
private String currentCrawl;
private Integer currentDepth;
private Long endTime;
private volatile Future future;

protected AbstractBuilder(final Class<B> builderClass, final String nodeAddress, final TimeTracker timeTracker,
final D crawlDefinition) {
Expand Down Expand Up @@ -310,6 +340,11 @@ public B done() {
return builderClass.cast(this);
}

B future(Future future) {
this.future = future;
return builderClass.cast(this);
}

public abstract S build(boolean withCrawlDefinition);
}

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/qwazr/crawler/common/CrawlThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -145,11 +146,15 @@ final public void run() {
}
}

final public S getStatus(boolean withDefinition) {
protected final S getStatus(boolean withDefinition) {
return session.getCrawlStatus(withDefinition);
}

public void abort(final String abortingReason) {
protected void start() {
session.setFuture(manager.executorService.submit(this));
}

protected void abort(final String abortingReason) {
session.abort(abortingReason);
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/qwazr/crawler/file/FileCrawlStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.qwazr.crawler.common.CrawlStatus;
import com.qwazr.utils.TimeTracker;

import java.util.concurrent.Future;

final public class FileCrawlStatus extends CrawlStatus<FileCrawlDefinition> {

@JsonCreator
Expand All @@ -30,9 +32,11 @@ final public class FileCrawlStatus extends CrawlStatus<FileCrawlDefinition> {
@JsonProperty("last_error") String lastError, @JsonProperty("current_crawl") String currentCrawl,
@JsonProperty("start_time") final Long startTime, @JsonProperty("end_time") final Long endTime,
@JsonProperty("current_depth") Integer currentDepth,
@JsonProperty("crawl_definition") FileCrawlDefinition crawlDefinition) {
@JsonProperty("crawl_definition") FileCrawlDefinition crawlDefinition,
@JsonProperty("thread_cancelled") Boolean threadCancelled,
@JsonProperty("thread_done") Boolean threadDone) {
super(nodeAddress, aborting, abortingReason, timer, crawled, ignored, redirect, error, lastError, currentCrawl,
startTime, endTime, currentDepth, crawlDefinition);
startTime, endTime, currentDepth, crawlDefinition, threadCancelled, threadDone);
}

private FileCrawlStatus(Builder builder, boolean withCrawlDefinition) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/qwazr/crawler/web/WebCrawlStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ final public class WebCrawlStatus extends CrawlStatus<WebCrawlDefinition> {
@JsonProperty("last_error") String lastError, @JsonProperty("current_crawl") String currentCrawl,
@JsonProperty("start_time") final Long startTime, @JsonProperty("end_time") final Long endTime,
@JsonProperty("current_depth") Integer currentDepth,
@JsonProperty("crawl_definition") WebCrawlDefinition crawlDefinition) {
@JsonProperty("crawl_definition") WebCrawlDefinition crawlDefinition,
@JsonProperty("thread_cancelled") Boolean threadCancelled,
@JsonProperty("thread_done") Boolean threadDone) {
super(nodeAddress, aborting, abortingReason, timer, crawled, ignored, redirect, error, lastError, currentCrawl,
startTime, endTime, currentDepth, crawlDefinition);
startTime, endTime, currentDepth, crawlDefinition, threadCancelled, threadDone);
}

private WebCrawlStatus(Builder builder, boolean withDefinition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ void crawlTest(FileCrawlDefinition.Builder fileCrawlDefinitionBuilder, int expec
Assert.assertEquals(expectedCrawled, crawlStatus.crawled);
Assert.assertEquals(expectedIgnored, crawlStatus.ignored);
Assert.assertEquals(expectedError, crawlStatus.error);
Assert.assertNotNull(crawlStatus.threadDone);

if (expectedError == 0)
Assert.assertNull(crawlStatus.lastError);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private CrawlStatus crawlTest(WebCrawlDefinition webCrawl, int crawled, int igno
Assert.assertEquals(crawled, status.crawled);
Assert.assertEquals(ignored, status.ignored);
Assert.assertEquals(error, status.error);
Assert.assertNotNull(status.threadDone);
return status;
}

Expand Down

0 comments on commit a951437

Please sign in to comment.