/
Heritrix3JobMonitor.java
224 lines (189 loc) · 7.37 KB
/
Heritrix3JobMonitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package dk.netarkivet.harvester.webinterface.servlet;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.netarchivesuite.heritrix3wrapper.AnypathResult;
import org.netarchivesuite.heritrix3wrapper.ByteRange;
import org.netarchivesuite.heritrix3wrapper.Heritrix3Wrapper;
import org.netarchivesuite.heritrix3wrapper.JobResult;
import dk.netarkivet.harvester.datamodel.Job;
import dk.netarkivet.harvester.harvesting.monitor.StartedJobInfo;
public class Heritrix3JobMonitor implements Pageable {
protected NASEnvironment environment;
public boolean bActive = true;
public boolean bPull = false;
public boolean bInitialized;
public long jobId;
public Job job;
public Heritrix3Wrapper h3wrapper;
public String h3HostnamePort;
public String hostUrl;
public String jobname;
public JobResult jobResult;
public String crawlLogFilePath;
public File logFile;
public RandomAccessFile logRaf;
public File idxFile;
public RandomAccessFile idxRaf;
public long lastIndexed = 0;
protected Heritrix3JobMonitor() {
}
public static Heritrix3JobMonitor getInstance(Long jobId, NASEnvironment environment) throws IOException {
Heritrix3JobMonitor jobmonitor = new Heritrix3JobMonitor();
jobmonitor.environment = environment;
jobmonitor.jobId = jobId;
jobmonitor.logFile = new File(environment.tempPath, "crawllog-" + jobId + ".log");
jobmonitor.idxFile = new File(environment.tempPath, "crawllog-" + jobId + ".idx");
jobmonitor.init();
return jobmonitor;
}
public synchronized void init() throws IOException {
if (bActive && !bInitialized) {
if (job == null) {
job = Heritrix3JobMonitorThread.jobDAO.read(jobId);
}
if (h3wrapper == null) {
StartedJobInfo startedInfo = Heritrix3JobMonitorThread.runningJobsInfoDAO.getMostRecentByJobId(jobId);
if (startedInfo != null) {
hostUrl = startedInfo.getHostUrl();
if (hostUrl != null && hostUrl.length() > 0) {
h3wrapper = Heritrix3WrapperManager.getHeritrix3Wrapper(hostUrl, environment.h3AdminName, environment.h3AdminPassword);
}
}
}
if (jobname == null && h3wrapper != null) {
jobname = Heritrix3WrapperManager.getJobname(h3wrapper, jobId);
}
if ((jobResult == null || jobResult.job == null) && jobname != null) {
jobResult = h3wrapper.job(jobname);
}
if (jobResult != null && jobResult.job != null) {
crawlLogFilePath = jobResult.job.crawlLogFilePath;
}
if (crawlLogFilePath != null) {
logRaf = new RandomAccessFile(logFile, "rw");
idxRaf = new RandomAccessFile(idxFile, "rw");
idxRaf.writeLong(0);
bInitialized = true;
}
}
}
public synchronized void update() throws IOException {
if (job != null) {
Job tmpJob = job = Heritrix3JobMonitorThread.jobDAO.read(jobId);
if (tmpJob != null) {
job = tmpJob;
}
}
if (jobResult != null && jobResult.job != null && jobname != null) {
JobResult tmpJobResult = h3wrapper.job(jobname);
if (tmpJobResult != null) {
jobResult = tmpJobResult;
}
}
}
public synchronized void updateCrawlLog(byte[] tmpBuf) throws IOException {
long pos;
long to;
int idx;
boolean bLoop;
ByteRange byteRange;
if (bActive && !bInitialized) {
init();
}
if (bActive && bInitialized) {
bLoop = true;
while (bLoop) {
idxRaf.seek(idxRaf.length());
pos = logRaf.length();
to = pos;
AnypathResult anypathResult = h3wrapper.anypath(jobResult.job.crawlLogFilePath, pos, pos + tmpBuf.length - 1);
if (anypathResult != null && anypathResult.byteRange != null && anypathResult.in != null) {
byteRange = anypathResult.byteRange;
if (byteRange.contentLength > 0) {
logRaf.seek(pos);
int read;
try {
while ((read = anypathResult.in.read(tmpBuf)) != -1) {
logRaf.write(tmpBuf, 0, read);
to += read;
idx = 0;
while (read > 0) {
++pos;
--read;
if (tmpBuf[idx++] == '\n') {
idxRaf.writeLong(pos);
lastIndexed = pos;
}
}
}
}
catch (IOException e) {
e.printStackTrace();
}
IOUtils.closeQuietly(anypathResult);
if (byteRange.contentLength == to) {
bLoop = false;
}
} else {
bLoop = false;
}
} else {
bLoop = false;
}
}
}
}
@Override
public synchronized long getIndexSize() {
return idxFile.length();
}
@Override
public long getLastIndexed() {
return lastIndexed;
}
@Override
public synchronized byte[] readPage(long page, long itemsPerPage, boolean descending) throws IOException {
return StringIndexFile.readPage(idxRaf, logRaf, page, itemsPerPage, descending);
}
public synchronized boolean isReady() {
return (bActive && bInitialized);
}
protected Map<String, SearchResult> qSearchResultMap = new HashMap<String, SearchResult>();
protected int searchResultNr = 1;
public synchronized SearchResult getSearchResult(String q) throws IOException {
SearchResult searchResult = qSearchResultMap.get(q);
if (searchResult == null) {
searchResult = new SearchResult(this, q, searchResultNr++);
qSearchResultMap.put(q, searchResult);
}
return searchResult;
}
public synchronized void cleanup(List<File> oldFilesList) {
bActive = false;
bInitialized = false;
hostUrl = null;
h3wrapper = null;
jobname = null;
jobResult = null;
crawlLogFilePath = null;
IOUtils.closeQuietly(logRaf);
IOUtils.closeQuietly(idxRaf);
oldFilesList.add(logFile);
oldFilesList.add(idxFile);
Iterator<SearchResult> srIter = qSearchResultMap.values().iterator();
SearchResult sr;
while (srIter.hasNext()) {
sr = srIter.next();
oldFilesList.add(sr.srIdxFile);
oldFilesList.add(sr.srLogFile);
sr.cleanup();
}
qSearchResultMap.clear();
}
}