2013-04-13 07:02:59 +02:00
|
|
|
package com.commafeed.backend.feeds;
|
|
|
|
|
|
2013-05-19 12:11:54 +02:00
|
|
|
import java.util.Date;
|
2013-04-13 07:02:59 +02:00
|
|
|
import java.util.List;
|
2013-05-28 12:55:47 +02:00
|
|
|
import java.util.Map;
|
2013-04-13 07:02:59 +02:00
|
|
|
import java.util.Queue;
|
2013-06-30 12:18:24 +02:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Executors;
|
2013-04-13 07:02:59 +02:00
|
|
|
|
2013-04-23 16:00:18 +02:00
|
|
|
import javax.annotation.PostConstruct;
|
2013-06-30 12:18:24 +02:00
|
|
|
import javax.annotation.PreDestroy;
|
|
|
|
|
import javax.enterprise.context.ApplicationScoped;
|
2013-04-13 07:02:59 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
2013-07-02 14:33:53 +02:00
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
2013-06-30 12:18:24 +02:00
|
|
|
import org.apache.commons.lang.time.DateUtils;
|
2013-06-29 11:20:53 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2013-05-19 12:11:54 +02:00
|
|
|
|
2013-04-16 13:52:20 +02:00
|
|
|
import com.commafeed.backend.MetricsBean;
|
2013-04-13 07:02:59 +02:00
|
|
|
import com.commafeed.backend.dao.FeedDAO;
|
|
|
|
|
import com.commafeed.backend.model.Feed;
|
2013-04-15 10:50:25 +02:00
|
|
|
import com.commafeed.backend.services.ApplicationSettingsService;
|
2013-05-28 12:55:47 +02:00
|
|
|
import com.google.api.client.util.Maps;
|
2013-07-05 08:23:18 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2013-04-16 12:36:36 +02:00
|
|
|
import com.google.common.collect.Queues;
|
2013-04-13 07:02:59 +02:00
|
|
|
|
2013-07-26 16:00:02 +02:00
|
|
|
/**
|
|
|
|
|
* Infinite loop fetching feeds from the database and queuing them to the {@link FeedRefreshWorker} pool. Also handles feed database updates
|
|
|
|
|
* at the end of the cycle through {@link #giveBack(Feed)}.
|
|
|
|
|
*
|
|
|
|
|
*/
|
2013-06-30 12:18:24 +02:00
|
|
|
@ApplicationScoped
|
2013-04-13 07:02:59 +02:00
|
|
|
public class FeedRefreshTaskGiver {
|
|
|
|
|
|
2013-06-29 11:20:53 +02:00
|
|
|
protected static final Logger log = LoggerFactory.getLogger(FeedRefreshTaskGiver.class);
|
|
|
|
|
|
2013-04-13 07:02:59 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedDAO feedDAO;
|
|
|
|
|
|
2013-04-15 10:50:25 +02:00
|
|
|
@Inject
|
|
|
|
|
ApplicationSettingsService applicationSettingsService;
|
|
|
|
|
|
2013-04-16 13:52:20 +02:00
|
|
|
@Inject
|
|
|
|
|
MetricsBean metricsBean;
|
|
|
|
|
|
2013-06-30 12:18:24 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedRefreshWorker worker;
|
|
|
|
|
|
2013-04-23 16:00:18 +02:00
|
|
|
private int backgroundThreads;
|
2013-05-21 16:40:37 +02:00
|
|
|
|
|
|
|
|
private Queue<Feed> addQueue = Queues.newConcurrentLinkedQueue();
|
2013-05-22 00:07:13 +02:00
|
|
|
private Queue<Feed> takeQueue = Queues.newConcurrentLinkedQueue();
|
|
|
|
|
private Queue<Feed> giveBackQueue = Queues.newConcurrentLinkedQueue();
|
2013-04-16 12:36:36 +02:00
|
|
|
|
2013-06-30 12:18:24 +02:00
|
|
|
private ExecutorService executor;
|
|
|
|
|
|
2013-04-23 16:00:18 +02:00
|
|
|
@PostConstruct
|
|
|
|
|
public void init() {
|
2013-07-25 09:17:33 +02:00
|
|
|
backgroundThreads = applicationSettingsService.get().getBackgroundThreads();
|
2013-06-30 12:18:24 +02:00
|
|
|
executor = Executors.newFixedThreadPool(1);
|
2013-04-23 16:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-30 12:18:24 +02:00
|
|
|
@PreDestroy
|
|
|
|
|
public void shutdown() {
|
|
|
|
|
executor.shutdownNow();
|
|
|
|
|
while (!executor.isTerminated()) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(100);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
log.error("interrupted while waiting for threads to finish.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void start() {
|
|
|
|
|
try {
|
|
|
|
|
// sleeping for a little while, let everything settle
|
|
|
|
|
Thread.sleep(5000);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
log.error("interrupted while sleeping");
|
2013-05-19 12:11:54 +02:00
|
|
|
}
|
2013-06-30 12:18:24 +02:00
|
|
|
log.info("starting feed refresh task giver");
|
|
|
|
|
|
|
|
|
|
executor.execute(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
while (!executor.isShutdown()) {
|
|
|
|
|
try {
|
|
|
|
|
Feed feed = take();
|
|
|
|
|
if (feed != null) {
|
|
|
|
|
metricsBean.feedRefreshed();
|
|
|
|
|
worker.updateFeed(feed);
|
|
|
|
|
} else {
|
|
|
|
|
log.debug("nothing to do, sleeping for 15s");
|
2013-07-29 09:45:31 +02:00
|
|
|
metricsBean.threadWaited();
|
2013-06-30 12:18:24 +02:00
|
|
|
try {
|
|
|
|
|
Thread.sleep(15000);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
log.error("interrupted while sleeping");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-04-25 14:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-29 09:45:31 +02:00
|
|
|
/**
|
|
|
|
|
* take a feed from the refresh queue
|
|
|
|
|
*/
|
2013-06-30 12:18:24 +02:00
|
|
|
private Feed take() {
|
2013-05-22 00:07:13 +02:00
|
|
|
Feed feed = takeQueue.poll();
|
2013-05-28 12:55:47 +02:00
|
|
|
|
2013-05-30 13:18:26 +02:00
|
|
|
if (feed == null) {
|
|
|
|
|
refill();
|
|
|
|
|
feed = takeQueue.poll();
|
|
|
|
|
}
|
2013-06-30 12:18:24 +02:00
|
|
|
return feed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Long getUpdatableCount() {
|
2013-07-29 10:33:18 +02:00
|
|
|
return feedDAO.getUpdatableCount();
|
2013-06-30 12:18:24 +02:00
|
|
|
}
|
2013-05-21 16:40:37 +02:00
|
|
|
|
2013-07-29 09:45:31 +02:00
|
|
|
/**
|
|
|
|
|
* add a feed to the refresh queue
|
|
|
|
|
*/
|
2013-06-30 12:18:24 +02:00
|
|
|
public void add(Feed feed) {
|
2013-07-29 10:33:18 +02:00
|
|
|
int refreshInterval = applicationSettingsService.get().getRefreshIntervalMinutes();
|
|
|
|
|
if (feed.getLastUpdated() == null || feed.getLastUpdated().before(DateUtils.addMinutes(new Date(), -1 * refreshInterval))) {
|
2013-06-30 12:18:24 +02:00
|
|
|
addQueue.add(feed);
|
2013-05-30 13:18:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-05-21 16:40:37 +02:00
|
|
|
|
2013-07-29 09:45:31 +02:00
|
|
|
/**
|
|
|
|
|
* refills the refresh queue and empties the giveBack queue while at it
|
|
|
|
|
*/
|
2013-05-30 13:18:26 +02:00
|
|
|
private void refill() {
|
2013-07-30 13:23:32 +02:00
|
|
|
int count = Math.min(300, 3 * backgroundThreads);
|
2013-07-29 09:45:31 +02:00
|
|
|
|
|
|
|
|
// first, get feeds that are up to refresh from the database
|
2013-07-05 08:23:18 +02:00
|
|
|
List<Feed> feeds = null;
|
|
|
|
|
if (applicationSettingsService.get().isCrawlingPaused()) {
|
|
|
|
|
feeds = Lists.newArrayList();
|
|
|
|
|
} else {
|
2013-07-29 10:33:18 +02:00
|
|
|
feeds = feedDAO.findNextUpdatable(count);
|
2013-07-05 08:23:18 +02:00
|
|
|
}
|
2013-05-22 00:07:13 +02:00
|
|
|
|
2013-07-29 09:45:31 +02:00
|
|
|
// then, add to those the feeds we got from the add() method. We add them at the beginning of the list as they probably have a
|
|
|
|
|
// higher priority
|
2013-05-30 13:18:26 +02:00
|
|
|
int size = addQueue.size();
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
2013-06-22 20:06:09 +02:00
|
|
|
feeds.add(0, addQueue.poll());
|
2013-05-30 13:18:26 +02:00
|
|
|
}
|
2013-05-22 10:39:03 +02:00
|
|
|
|
2013-07-29 09:45:31 +02:00
|
|
|
// set the disabledDate to now as we use the disabledDate in feedDAO to decide what to refresh next. We also use a map to remove
|
|
|
|
|
// duplicates.
|
2013-06-22 20:06:09 +02:00
|
|
|
Map<Long, Feed> map = Maps.newLinkedHashMap();
|
2013-05-30 13:18:26 +02:00
|
|
|
for (Feed f : feeds) {
|
2013-07-27 15:45:03 +02:00
|
|
|
f.setDisabledUntil(new Date());
|
2013-05-30 13:18:26 +02:00
|
|
|
map.put(f.getId(), f);
|
2013-04-13 07:02:59 +02:00
|
|
|
}
|
2013-07-29 09:45:31 +02:00
|
|
|
|
|
|
|
|
// refill the queue
|
2013-05-30 13:18:26 +02:00
|
|
|
takeQueue.addAll(map.values());
|
|
|
|
|
|
2013-07-29 09:45:31 +02:00
|
|
|
// add feeds from the giveBack queue to the map, overriding duplicates
|
2013-05-30 13:18:26 +02:00
|
|
|
size = giveBackQueue.size();
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
Feed f = giveBackQueue.poll();
|
|
|
|
|
map.put(f.getId(), f);
|
2013-05-22 12:25:35 +02:00
|
|
|
}
|
2013-05-30 13:18:26 +02:00
|
|
|
|
2013-07-29 09:45:31 +02:00
|
|
|
// update all feeds in the database
|
2013-06-06 09:54:17 +02:00
|
|
|
feedDAO.saveOrUpdate(map.values());
|
2013-04-13 07:02:59 +02:00
|
|
|
}
|
2013-04-16 09:29:33 +02:00
|
|
|
|
2013-07-29 09:45:31 +02:00
|
|
|
/**
|
|
|
|
|
* give a feed back, updating it to the database during the next refill()
|
|
|
|
|
*/
|
2013-05-22 00:07:13 +02:00
|
|
|
public void giveBack(Feed feed) {
|
2013-07-02 14:33:53 +02:00
|
|
|
String normalized = FeedUtils.normalizeURL(feed.getUrl());
|
|
|
|
|
feed.setNormalizedUrl(normalized);
|
|
|
|
|
feed.setNormalizedUrlHash(DigestUtils.sha1Hex(normalized));
|
2013-07-27 15:45:03 +02:00
|
|
|
feed.setLastUpdated(new Date());
|
2013-05-22 00:07:13 +02:00
|
|
|
giveBackQueue.add(feed);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-13 07:02:59 +02:00
|
|
|
}
|