2014-08-08 16:49:02 +02:00
|
|
|
package com.commafeed.backend.feed;
|
2013-04-13 07:02:59 +02:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
2014-08-17 14:16:30 +02:00
|
|
|
import javax.inject.Singleton;
|
2013-04-13 07:02:59 +02:00
|
|
|
|
2013-07-02 14:33:53 +02:00
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
2014-10-28 16:36:09 +01:00
|
|
|
import org.apache.commons.lang3.time.DateUtils;
|
2013-05-19 12:11:54 +02:00
|
|
|
|
2013-12-10 14:02:06 +01:00
|
|
|
import com.codahale.metrics.Gauge;
|
2013-08-18 16:29:07 +02:00
|
|
|
import com.codahale.metrics.Meter;
|
|
|
|
|
import com.codahale.metrics.MetricRegistry;
|
2014-08-08 16:49:02 +02:00
|
|
|
import com.commafeed.CommaFeedConfiguration;
|
2013-04-13 07:02:59 +02:00
|
|
|
import com.commafeed.backend.dao.FeedDAO;
|
|
|
|
|
import com.commafeed.backend.model.Feed;
|
2013-07-05 08:23:18 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2013-08-01 13:35:04 +02:00
|
|
|
import com.google.common.collect.Maps;
|
2013-04-16 12:36:36 +02:00
|
|
|
import com.google.common.collect.Queues;
|
2013-04-13 07:02:59 +02:00
|
|
|
|
2014-08-17 14:16:30 +02:00
|
|
|
@Singleton
|
2014-08-08 16:49:02 +02:00
|
|
|
public class FeedQueues {
|
2013-04-13 07:02:59 +02:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
private final FeedDAO feedDAO;
|
|
|
|
|
private final CommaFeedConfiguration config;
|
2013-05-21 16:40:37 +02:00
|
|
|
|
2013-07-31 13:01:40 +02:00
|
|
|
private Queue<FeedRefreshContext> addQueue = Queues.newConcurrentLinkedQueue();
|
|
|
|
|
private Queue<FeedRefreshContext> takeQueue = Queues.newConcurrentLinkedQueue();
|
2013-05-22 00:07:13 +02:00
|
|
|
private Queue<Feed> giveBackQueue = Queues.newConcurrentLinkedQueue();
|
2013-04-16 12:36:36 +02:00
|
|
|
|
2013-08-18 17:19:01 +02:00
|
|
|
private Meter refill;
|
2013-08-18 16:29:07 +02:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
@Inject
|
|
|
|
|
public FeedQueues(FeedDAO feedDAO, CommaFeedConfiguration config, MetricRegistry metrics) {
|
|
|
|
|
this.config = config;
|
|
|
|
|
this.feedDAO = feedDAO;
|
|
|
|
|
|
2013-08-18 17:19:01 +02:00
|
|
|
refill = metrics.meter(MetricRegistry.name(getClass(), "refill"));
|
2013-12-10 14:02:06 +01:00
|
|
|
metrics.register(MetricRegistry.name(getClass(), "addQueue"), new Gauge<Integer>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Integer getValue() {
|
|
|
|
|
return addQueue.size();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
metrics.register(MetricRegistry.name(getClass(), "takeQueue"), new Gauge<Integer>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Integer getValue() {
|
|
|
|
|
return takeQueue.size();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
metrics.register(MetricRegistry.name(getClass(), "giveBackQueue"), new Gauge<Integer>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Integer getValue() {
|
|
|
|
|
return giveBackQueue.size();
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-04-23 16:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-29 09:45:31 +02:00
|
|
|
/**
|
|
|
|
|
* take a feed from the refresh queue
|
|
|
|
|
*/
|
2014-08-08 16:49:02 +02:00
|
|
|
public synchronized FeedRefreshContext take() {
|
2013-07-31 13:01:40 +02:00
|
|
|
FeedRefreshContext context = takeQueue.poll();
|
2013-05-28 12:55:47 +02:00
|
|
|
|
2013-07-31 13:01:40 +02:00
|
|
|
if (context == null) {
|
2013-05-30 13:18:26 +02:00
|
|
|
refill();
|
2013-07-31 13:01:40 +02:00
|
|
|
context = takeQueue.poll();
|
2013-05-30 13:18:26 +02:00
|
|
|
}
|
2013-07-31 13:01:40 +02:00
|
|
|
return context;
|
2013-06-30 12:18:24 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-29 09:45:31 +02:00
|
|
|
/**
|
|
|
|
|
* add a feed to the refresh queue
|
|
|
|
|
*/
|
2013-07-31 13:01:40 +02:00
|
|
|
public void add(Feed feed, boolean urgent) {
|
2014-08-08 16:49:02 +02:00
|
|
|
int refreshInterval = config.getApplicationSettings().getRefreshIntervalMinutes();
|
2013-07-29 10:33:18 +02:00
|
|
|
if (feed.getLastUpdated() == null || feed.getLastUpdated().before(DateUtils.addMinutes(new Date(), -1 * refreshInterval))) {
|
2014-08-19 00:43:23 +02:00
|
|
|
boolean alreadyQueued = false;
|
|
|
|
|
for (FeedRefreshContext context : addQueue) {
|
|
|
|
|
if (context.getFeed().getId().equals(feed.getId())) {
|
|
|
|
|
alreadyQueued = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!alreadyQueued) {
|
|
|
|
|
addQueue.add(new FeedRefreshContext(feed, urgent));
|
|
|
|
|
}
|
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-08-18 17:19:01 +02:00
|
|
|
refill.mark();
|
2013-07-29 09:45:31 +02:00
|
|
|
|
2013-07-31 13:01:40 +02:00
|
|
|
List<FeedRefreshContext> contexts = Lists.newArrayList();
|
2014-08-08 16:49:02 +02:00
|
|
|
int batchSize = Math.min(100, 3 * config.getApplicationSettings().getBackgroundThreads());
|
2013-12-10 11:07:39 +01:00
|
|
|
|
|
|
|
|
// add feeds we got from the add() method
|
|
|
|
|
int addQueueSize = addQueue.size();
|
|
|
|
|
for (int i = 0; i < Math.min(batchSize, addQueueSize); i++) {
|
|
|
|
|
contexts.add(addQueue.poll());
|
2013-07-05 08:23:18 +02:00
|
|
|
}
|
2013-05-22 00:07:13 +02:00
|
|
|
|
2013-12-10 11:07:39 +01:00
|
|
|
// add feeds that are up to refresh from the database
|
2014-08-09 19:35:05 +02:00
|
|
|
int count = batchSize - contexts.size();
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
List<Feed> feeds = feedDAO.findNextUpdatable(count, getLastLoginThreshold());
|
|
|
|
|
for (Feed feed : feeds) {
|
|
|
|
|
contexts.add(new FeedRefreshContext(feed, false));
|
2013-12-10 11:07:39 +01:00
|
|
|
}
|
2013-05-30 13:18:26 +02:00
|
|
|
}
|
2013-05-22 10:39:03 +02:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
// set the disabledDate as we use it in feedDAO to decide what to refresh next. We also use a map to remove
|
2013-07-29 09:45:31 +02:00
|
|
|
// duplicates.
|
2013-07-31 13:01:40 +02:00
|
|
|
Map<Long, FeedRefreshContext> map = Maps.newLinkedHashMap();
|
|
|
|
|
for (FeedRefreshContext context : contexts) {
|
|
|
|
|
Feed feed = context.getFeed();
|
2014-08-08 16:49:02 +02:00
|
|
|
feed.setDisabledUntil(DateUtils.addMinutes(new Date(), config.getApplicationSettings().getRefreshIntervalMinutes()));
|
2013-07-31 13:01:40 +02:00
|
|
|
map.put(feed.getId(), context);
|
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-12-10 11:07:39 +01:00
|
|
|
int giveBackQueueSize = giveBackQueue.size();
|
|
|
|
|
for (int i = 0; i < giveBackQueueSize; i++) {
|
2013-07-31 13:01:40 +02:00
|
|
|
Feed feed = giveBackQueue.poll();
|
|
|
|
|
map.put(feed.getId(), new FeedRefreshContext(feed, false));
|
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-07-31 13:01:40 +02:00
|
|
|
List<Feed> feeds = Lists.newArrayList();
|
|
|
|
|
for (FeedRefreshContext context : map.values()) {
|
|
|
|
|
feeds.add(context.getFeed());
|
|
|
|
|
}
|
2014-08-08 16:49:02 +02:00
|
|
|
feedDAO.merge(feeds);
|
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-08-01 21:11:45 +02:00
|
|
|
private Date getLastLoginThreshold() {
|
2014-12-04 10:52:41 +01:00
|
|
|
if (config.getApplicationSettings().getHeavyLoad()) {
|
2013-08-01 21:11:45 +02:00
|
|
|
return DateUtils.addDays(new Date(), -30);
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-13 07:02:59 +02:00
|
|
|
}
|