newTopic = newTopics.take();
} catch(InterruptedException e) { continue; }
String lastMod = null;
Topic oldTopic = topics.get(newTopic);
// If we already saw this topic before, acquire its lock
if(oldTopic != null) {
oldTopic.lock.readLock().lock();
lastMod = oldTopic.getLastMod();
oldTopic.lock.readLock().unlock();
}
long startTime = DateTime.now().getMillis();
// Let's go get our updated topic, from the topic page
Topic topic;
try {
topic = sourceBoard.getThread(newTopic, lastMod);
} catch(HttpGetException e) {
if(e.getHttpStatus() == 304) {
// If the old topic exists, update its lastHit timestamp
// The old topic should always exist at this point.
pingTopic(oldTopic);
debug(TALK, newTopic + ": wasn't modified");
continue;
} else if(e.getHttpStatus() == 404) {
if(oldTopic != null) {
oldTopic.lock.writeLock().lock();
// If we found the topic before the page limbo
// threshold, then it was forcefully deleted
if(oldTopic.getLastPage() < pageLimbo) {
if(oldTopic.getAllPosts().size() > 1) {
int op = oldTopic.getAllPosts().iterator().next();
try {
DeletedPost post = new DeletedPost(op, System.currentTimeMillis() / 1000);
deletedPosts.put(post);
} catch(InterruptedException e1) { }
}
topicUpdates.add(oldTopic);
debug(TALK, newTopic + ": deleted (last seen on page " + oldTopic.getLastPage() + ")");
}
// Goodbye, old topic.
topics.remove(newTopic);
oldTopic.lock.writeLock().unlock();
}
continue;
} else {
// We got some funky error
pingTopic(oldTopic);
debug(WARN, newTopic + ": error: " + e.getMessage());
continue;
}
} catch(ContentGetException e) {
// We got an even funkier, non-HTTP error
pingTopic(oldTopic);
debug(WARN, newTopic + ": error: " + e.getMessage());
continue;
} catch(ContentParseException e) {
pingTopic(oldTopic);
debug(ERROR, newTopic + ": " + e.getMessage());
continue;
}
if(topic == null) {
pingTopic(oldTopic);
debug(WARN, newTopic + ": topic has no posts");
continue;
}
topic.setLastHit(startTime);
// We're about to make our rebuilt topic public
topic.lock.writeLock().lock();
if(oldTopic != null) {
oldTopic.lock.writeLock().lock();
// Beaten to the punch (how?)
if(oldTopic.getLastHit() > startTime) {
debug(ERROR, "Concurrency issue updating topic " + oldTopic.getNum());
oldTopic.lock.writeLock().unlock();
// Throw this away now.
topic.lock.writeLock().unlock();
continue;
}
// Get the deleted posts from the old topic
// Update their status in the DB, too.
findDeleted(oldTopic, topic, true);
// We don't really know at which page this thread is, so let
// us keep the last page a PageScanner (or BoardPoller) saw this thread at.
topic.setLastPage(oldTopic.getLastPage());
// If the old topic has a lastMod timestamp from a BoardPoller, keep it
topic.setLastModTimestamp(oldTopic.getLastModTimestamp());
// Goodbye, old topic.
topics.put(newTopic, topic);
oldTopic.lock.writeLock().unlock();
} else {