2013-04-26 18:53:48 +02:00
|
|
|
package com.commafeed.backend.feeds;
|
|
|
|
|
|
|
|
|
|
import java.util.Collection;
|
2013-06-03 06:50:36 +02:00
|
|
|
import java.util.Date;
|
2013-05-22 00:07:13 +02:00
|
|
|
import java.util.List;
|
2013-05-22 21:57:53 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2013-05-27 10:44:43 +02:00
|
|
|
import java.util.concurrent.locks.Lock;
|
2013-04-26 18:53:48 +02:00
|
|
|
|
2013-05-22 21:57:53 +02:00
|
|
|
import javax.annotation.PostConstruct;
|
2013-05-24 16:50:00 +02:00
|
|
|
import javax.annotation.PreDestroy;
|
2013-06-30 12:18:24 +02:00
|
|
|
import javax.enterprise.context.ApplicationScoped;
|
2013-04-26 18:53:48 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
2013-05-27 22:52:14 +02:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
2013-06-03 06:50:36 +02:00
|
|
|
import org.apache.commons.lang3.time.DateUtils;
|
2013-05-20 14:06:09 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2013-04-26 18:53:48 +02:00
|
|
|
|
2013-05-25 15:39:54 +02:00
|
|
|
import com.commafeed.backend.MetricsBean;
|
2013-06-28 17:02:29 +02:00
|
|
|
import com.commafeed.backend.cache.CacheService;
|
2013-04-26 18:53:48 +02:00
|
|
|
import com.commafeed.backend.dao.FeedDAO;
|
2013-05-30 12:57:14 +02:00
|
|
|
import com.commafeed.backend.dao.FeedEntryDAO;
|
2013-05-22 00:07:13 +02:00
|
|
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
2013-06-30 12:18:24 +02:00
|
|
|
import com.commafeed.backend.feeds.FeedRefreshExecutor.Task;
|
2013-05-22 21:57:53 +02:00
|
|
|
import com.commafeed.backend.model.ApplicationSettings;
|
2013-04-26 18:53:48 +02:00
|
|
|
import com.commafeed.backend.model.Feed;
|
|
|
|
|
import com.commafeed.backend.model.FeedEntry;
|
2013-05-22 00:07:13 +02:00
|
|
|
import com.commafeed.backend.model.FeedSubscription;
|
2013-05-20 14:06:09 +02:00
|
|
|
import com.commafeed.backend.pubsubhubbub.SubscriptionHandler;
|
2013-05-21 07:09:48 +02:00
|
|
|
import com.commafeed.backend.services.ApplicationSettingsService;
|
2013-04-26 18:53:48 +02:00
|
|
|
import com.commafeed.backend.services.FeedUpdateService;
|
2013-06-29 11:20:53 +02:00
|
|
|
import com.google.api.client.util.Lists;
|
2013-05-27 10:44:43 +02:00
|
|
|
import com.google.common.util.concurrent.Striped;
|
2013-05-24 09:21:20 +02:00
|
|
|
|
2013-06-30 12:18:24 +02:00
|
|
|
@ApplicationScoped
|
2013-04-26 18:53:48 +02:00
|
|
|
public class FeedRefreshUpdater {
|
|
|
|
|
|
2013-05-20 14:06:09 +02:00
|
|
|
protected static Logger log = LoggerFactory
|
|
|
|
|
.getLogger(FeedRefreshUpdater.class);
|
|
|
|
|
|
2013-04-26 18:53:48 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedUpdateService feedUpdateService;
|
|
|
|
|
|
2013-05-20 14:06:09 +02:00
|
|
|
@Inject
|
|
|
|
|
SubscriptionHandler handler;
|
|
|
|
|
|
2013-05-22 00:07:13 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedRefreshTaskGiver taskGiver;
|
|
|
|
|
|
2013-04-26 18:53:48 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedDAO feedDAO;
|
|
|
|
|
|
2013-05-21 07:09:48 +02:00
|
|
|
@Inject
|
|
|
|
|
ApplicationSettingsService applicationSettingsService;
|
2013-05-27 10:44:43 +02:00
|
|
|
|
2013-05-25 15:39:54 +02:00
|
|
|
@Inject
|
|
|
|
|
MetricsBean metricsBean;
|
2013-05-21 07:09:48 +02:00
|
|
|
|
2013-05-22 00:07:13 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedSubscriptionDAO feedSubscriptionDAO;
|
|
|
|
|
|
2013-05-30 12:57:14 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedEntryDAO feedEntryDAO;
|
|
|
|
|
|
2013-06-28 17:02:29 +02:00
|
|
|
@Inject
|
|
|
|
|
CacheService cache;
|
|
|
|
|
|
2013-06-30 12:18:24 +02:00
|
|
|
private FeedRefreshExecutor pool;
|
2013-05-27 10:44:43 +02:00
|
|
|
private Striped<Lock> locks;
|
2013-05-22 21:57:53 +02:00
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public void init() {
|
|
|
|
|
ApplicationSettings settings = applicationSettingsService.get();
|
2013-05-23 06:38:58 +02:00
|
|
|
int threads = Math.max(settings.getDatabaseUpdateThreads(), 1);
|
2013-05-23 10:52:14 +02:00
|
|
|
log.info("Creating database pool with {} threads", threads);
|
2013-06-30 12:18:24 +02:00
|
|
|
pool = new FeedRefreshExecutor("FeedRefreshUpdater", threads, 500 * threads);
|
2013-06-07 15:54:47 +02:00
|
|
|
locks = Striped.lazyWeakLock(threads * 100000);
|
2013-05-22 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-24 16:50:00 +02:00
|
|
|
@PreDestroy
|
2013-05-24 12:28:48 +02:00
|
|
|
public void shutdown() {
|
2013-06-30 12:18:24 +02:00
|
|
|
pool.shutdown();
|
2013-05-24 12:28:48 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-22 21:57:53 +02:00
|
|
|
public void updateFeed(Feed feed, Collection<FeedEntry> entries) {
|
2013-06-30 12:18:24 +02:00
|
|
|
pool.execute(new EntryTask(feed, entries));
|
2013-05-22 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-30 12:18:24 +02:00
|
|
|
private class EntryTask implements Task {
|
2013-05-22 21:57:53 +02:00
|
|
|
|
|
|
|
|
private Feed feed;
|
|
|
|
|
private Collection<FeedEntry> entries;
|
|
|
|
|
|
2013-06-30 12:18:24 +02:00
|
|
|
public EntryTask(Feed feed, Collection<FeedEntry> entries) {
|
2013-05-22 21:57:53 +02:00
|
|
|
this.feed = feed;
|
|
|
|
|
this.entries = entries;
|
2013-04-26 18:53:48 +02:00
|
|
|
}
|
2013-05-22 00:07:13 +02:00
|
|
|
|
2013-05-22 21:57:53 +02:00
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2013-05-27 12:16:37 +02:00
|
|
|
boolean ok = true;
|
2013-05-30 13:51:18 +02:00
|
|
|
if (entries.isEmpty() == false) {
|
2013-06-30 07:44:59 +02:00
|
|
|
|
2013-06-29 11:20:53 +02:00
|
|
|
List<String> lastEntries = cache.getLastEntries(feed);
|
|
|
|
|
List<String> currentEntries = Lists.newArrayList();
|
2013-06-30 07:44:59 +02:00
|
|
|
|
|
|
|
|
List<FeedSubscription> subscriptions = null;
|
2013-05-22 21:57:53 +02:00
|
|
|
for (FeedEntry entry : entries) {
|
2013-07-02 18:07:08 +02:00
|
|
|
String cacheKey = cache.buildUniqueEntryKey(feed, entry);
|
2013-06-29 11:20:53 +02:00
|
|
|
if (!lastEntries.contains(cacheKey)) {
|
|
|
|
|
log.debug("cache miss for {}", entry.getUrl());
|
2013-06-30 07:44:59 +02:00
|
|
|
if (subscriptions == null) {
|
|
|
|
|
subscriptions = feedSubscriptionDAO
|
|
|
|
|
.findByFeed(feed);
|
|
|
|
|
}
|
2013-06-29 11:20:53 +02:00
|
|
|
ok &= updateEntry(feed, entry, subscriptions);
|
|
|
|
|
metricsBean.entryCacheMiss();
|
|
|
|
|
} else {
|
|
|
|
|
log.debug("cache hit for {}", entry.getUrl());
|
|
|
|
|
metricsBean.entryCacheHit();
|
|
|
|
|
}
|
|
|
|
|
currentEntries.add(cacheKey);
|
2013-05-22 21:57:53 +02:00
|
|
|
}
|
2013-06-29 11:20:53 +02:00
|
|
|
cache.setLastEntries(feed, currentEntries);
|
2013-05-22 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (applicationSettingsService.get().isPubsubhubbub()) {
|
|
|
|
|
handlePubSub(feed);
|
|
|
|
|
}
|
2013-05-27 12:16:37 +02:00
|
|
|
if (!ok) {
|
|
|
|
|
feed.setDisabledUntil(null);
|
|
|
|
|
}
|
2013-05-25 15:39:54 +02:00
|
|
|
metricsBean.feedUpdated();
|
2013-05-22 21:57:53 +02:00
|
|
|
taskGiver.giveBack(feed);
|
2013-05-21 07:09:48 +02:00
|
|
|
}
|
2013-06-22 20:06:09 +02:00
|
|
|
|
2013-06-30 12:18:24 +02:00
|
|
|
@Override
|
|
|
|
|
public boolean isUrgent() {
|
|
|
|
|
return feed.isUrgent();
|
2013-06-22 20:06:09 +02:00
|
|
|
}
|
2013-05-20 14:06:09 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-27 12:16:37 +02:00
|
|
|
private boolean updateEntry(final Feed feed, final FeedEntry entry,
|
2013-05-24 09:21:20 +02:00
|
|
|
final List<FeedSubscription> subscriptions) {
|
2013-06-07 15:54:47 +02:00
|
|
|
boolean success = false;
|
|
|
|
|
|
2013-05-27 22:54:40 +02:00
|
|
|
String key = StringUtils.trimToEmpty(entry.getGuid() + entry.getUrl());
|
2013-05-27 22:43:08 +02:00
|
|
|
Lock lock = locks.get(key);
|
2013-05-27 12:16:37 +02:00
|
|
|
boolean locked = false;
|
2013-05-27 10:44:43 +02:00
|
|
|
try {
|
2013-05-27 12:16:37 +02:00
|
|
|
locked = lock.tryLock(1, TimeUnit.MINUTES);
|
2013-05-27 16:41:29 +02:00
|
|
|
if (locked) {
|
2013-06-29 11:20:53 +02:00
|
|
|
feedUpdateService.updateEntry(feed, entry, subscriptions);
|
2013-06-07 15:54:47 +02:00
|
|
|
success = true;
|
2013-05-27 16:41:29 +02:00
|
|
|
} else {
|
2013-05-27 22:43:08 +02:00
|
|
|
log.error("lock timeout for " + feed.getUrl() + " - " + key);
|
2013-05-27 16:41:29 +02:00
|
|
|
}
|
2013-05-27 11:42:01 +02:00
|
|
|
} catch (InterruptedException e) {
|
2013-05-27 11:46:23 +02:00
|
|
|
log.error("interrupted while waiting for lock for " + feed.getUrl()
|
|
|
|
|
+ " : " + e.getMessage(), e);
|
2013-05-27 10:44:43 +02:00
|
|
|
} finally {
|
2013-05-27 12:16:37 +02:00
|
|
|
if (locked) {
|
|
|
|
|
lock.unlock();
|
|
|
|
|
}
|
2013-05-27 10:44:43 +02:00
|
|
|
}
|
2013-06-07 15:54:47 +02:00
|
|
|
return success;
|
2013-05-21 17:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-21 12:41:57 +02:00
|
|
|
private void handlePubSub(final Feed feed) {
|
2013-06-05 21:50:26 +02:00
|
|
|
if (feed.getPushHub() != null && feed.getPushTopic() != null) {
|
|
|
|
|
Date lastPing = feed.getPushLastPing();
|
2013-06-25 16:57:48 +02:00
|
|
|
Date now = new Date();
|
2013-06-03 06:50:36 +02:00
|
|
|
if (lastPing == null || lastPing.before(DateUtils.addDays(now, -3))) {
|
|
|
|
|
new Thread() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
handler.subscribe(feed);
|
|
|
|
|
}
|
|
|
|
|
}.start();
|
|
|
|
|
}
|
2013-05-20 14:06:09 +02:00
|
|
|
}
|
2013-04-26 18:53:48 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-27 11:20:19 +02:00
|
|
|
public int getQueueSize() {
|
2013-06-30 12:18:24 +02:00
|
|
|
return pool.getQueueSize();
|
2013-05-27 11:20:19 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-05 09:52:23 +02:00
|
|
|
public int getActiveCount() {
|
|
|
|
|
return pool.getActiveCount();
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-26 18:53:48 +02:00
|
|
|
}
|