-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b87c83b
commit 69f7330
Showing
18 changed files
with
639 additions
and
334 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
src/main/java/com/qwazr/crawler/common/CrawlDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/** | ||
* Copyright 2015-2017 Emmanuel Keller / QWAZR | ||
* <p> | ||
* Licensed 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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 com.qwazr.crawler.common; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
@JsonInclude(Include.NON_EMPTY) | ||
public class CrawlDefinition implements Cloneable { | ||
|
||
/** | ||
* The global variables shared by all the scripts. | ||
*/ | ||
public LinkedHashMap<String, String> variables = null; | ||
|
||
/** | ||
* A list of scripts paths mapped with the events which fire the scripts. | ||
*/ | ||
public Map<EventEnum, Script> scripts = null; | ||
|
||
public enum EventEnum { | ||
|
||
/** | ||
* Executed before the crawl session start | ||
*/ | ||
before_session, | ||
|
||
/** | ||
* Executed after the crawl session ends | ||
*/ | ||
after_session, | ||
|
||
/** | ||
* Executed before an URL is crawled | ||
*/ | ||
before_crawl, | ||
|
||
/** | ||
* Executed after an URL has been crawled | ||
*/ | ||
after_crawl | ||
} | ||
|
||
@JsonInclude(Include.NON_EMPTY) | ||
public static class Script implements Cloneable { | ||
|
||
/** | ||
* The path to the scripts | ||
*/ | ||
public String name = null; | ||
|
||
/** | ||
* The local variables passed to the scripts | ||
*/ | ||
public Map<String, String> variables = null; | ||
|
||
public Script() { | ||
} | ||
|
||
public Script(String name) { | ||
this.name = name; | ||
} | ||
|
||
protected Script(Script src) { | ||
this.name = src.name; | ||
this.variables = src.variables == null ? null : new HashMap<String, String>(src.variables); | ||
} | ||
|
||
@Override | ||
final public Object clone() { | ||
return new Script(this); | ||
} | ||
|
||
public Script addVariable(String name, String value) { | ||
if (variables == null) | ||
variables = new HashMap<>(); | ||
variables.put(name, value); | ||
return this; | ||
} | ||
|
||
} | ||
|
||
public CrawlDefinition() { | ||
} | ||
|
||
protected CrawlDefinition(CrawlDefinition src) { | ||
variables = src.variables == null ? null : new LinkedHashMap<>(src.variables); | ||
if (src.scripts == null) { | ||
scripts = null; | ||
} else { | ||
scripts = new HashMap<>(); | ||
src.scripts.forEach((eventEnum, script) -> scripts.put(eventEnum, new Script(script))); | ||
} | ||
} | ||
|
||
public Object clone() { | ||
return new CrawlDefinition(this); | ||
} | ||
|
||
@JsonIgnore | ||
public Script addScript(final String event, final String name) { | ||
if (scripts == null) | ||
scripts = new LinkedHashMap<>(); | ||
Script script = new Script(name); | ||
scripts.put(EventEnum.valueOf(event), script); | ||
return script; | ||
} | ||
|
||
public Map<EventEnum, Script> getScripts() { | ||
return scripts; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2017 Emmanuel Keller / QWAZR | ||
* <p> | ||
* Licensed 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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 com.qwazr.crawler.common; | ||
|
||
import com.qwazr.cluster.ClusterManager; | ||
import com.qwazr.server.ServerException; | ||
import org.slf4j.Logger; | ||
|
||
import javax.ws.rs.core.Response; | ||
import java.util.TreeMap; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
public abstract class CrawlManager<T extends CrawlThread<?>, D extends CrawlDefinition> { | ||
|
||
private final ConcurrentHashMap<String, T> crawlSessionMap; | ||
private final ExecutorService executorService; | ||
private final Logger logger; | ||
public final String myAddress; | ||
public final ClusterManager clusterManager; | ||
|
||
protected CrawlManager(final ClusterManager clusterManager, final ExecutorService executorService, | ||
final Logger logger) { | ||
crawlSessionMap = new ConcurrentHashMap<>(); | ||
this.clusterManager = clusterManager; | ||
this.myAddress = clusterManager.getServiceBuilder().local().getStatus().me; | ||
this.executorService = executorService; | ||
this.logger = logger; | ||
} | ||
|
||
public TreeMap<String, CrawlStatus> getSessions() { | ||
final TreeMap<String, CrawlStatus> map = new TreeMap<>(); | ||
crawlSessionMap.forEach((key, crawl) -> map.put(key, crawl.getStatus())); | ||
return map; | ||
} | ||
|
||
public CrawlStatus getSession(final String sessionName) { | ||
final T crawlThread = crawlSessionMap.get(sessionName); | ||
return crawlThread == null ? null : crawlThread.getStatus(); | ||
} | ||
|
||
public void abortSession(final String sessionName, final String abortingReason) throws ServerException { | ||
final T crawlThread = crawlSessionMap.get(sessionName); | ||
if (crawlThread == null) | ||
throw new ServerException(Response.Status.NOT_FOUND, "Session not found: " + sessionName); | ||
logger.info("Aborting session: {} - {}", sessionName, abortingReason); | ||
crawlThread.abort(abortingReason); | ||
} | ||
|
||
protected abstract T newCrawlThread(final String sessionName, final D crawlDefinition); | ||
|
||
public CrawlStatus runSession(final String sessionName, final D crawlDefinition) throws ServerException { | ||
|
||
final AtomicBoolean newThread = new AtomicBoolean(false); | ||
|
||
final T crawlThread = crawlSessionMap.computeIfAbsent(sessionName, key -> { | ||
logger.info("Create session: {}", sessionName); | ||
newThread.set(true); | ||
return newCrawlThread(sessionName, crawlDefinition); | ||
}); | ||
|
||
if (!newThread.get()) | ||
throw new ServerException(Response.Status.CONFLICT, "The session already exists: " + sessionName); | ||
executorService.execute(crawlThread); | ||
return crawlThread.getStatus(); | ||
} | ||
|
||
public void removeSession(final T crawlThread) { | ||
logger.info("Remove session: {}", crawlThread.getSessionName()); | ||
crawlSessionMap.remove(crawlThread.getSessionName(), crawlThread); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.