Skip to content

Commit

Permalink
Remove transaction management entirely.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Conway committed Sep 18, 2012
1 parent 9e593b8 commit d0ab51c
Showing 1 changed file with 34 additions and 67 deletions.
101 changes: 34 additions & 67 deletions app/updaters/GtfsDataExchangeUpdater.java
Expand Up @@ -29,7 +29,6 @@ the License, or (props, at your option) any later version.
import play.db.jpa.JPAPlugin;
import play.db.jpa.NoTransaction;
import play.exceptions.JPAException;
import play.jobs.Job;
import play.libs.WS;
import play.libs.WS.HttpResponse;
import play.libs.XPath;
Expand All @@ -52,30 +51,29 @@ the License, or (props, at your option) any later version.
* It also handles hooks, calling them with the feeds that have changed.
* @author mattwigway
*/
public class GtfsDataExchangeUpdater implements Updater {
/**
* This inner job fetches a single feed off the exchange. This is in its own job
* so that Play will automatically handle creating a new transaction for it. This job is run
* synchronously from the updater
* @author mattwigway
*
*/
private static class FetchOneFeed extends Job {
private JsonObject feed;
private FeedStorer storer;
private Set<MetroArea> updated;

public FetchOneFeed (JsonObject feed, FeedStorer storer, Set<MetroArea> updated) {
this.feed = feed;
this.storer = storer;
this.updated = updated;
}

public void doJob () {
public class GtfsDataExchangeUpdater implements Updater {
public Set<MetroArea> update (FeedStorer storer) {
Set<MetroArea> updated = new HashSet<MetroArea>();

// First, fetch the RSS feed
HttpResponse res = WS.url("http://www.gtfs-data-exchange.com/api/agencies").get();
int status = res.getStatus();
if (status != 200) {
Logger.error("Error fetching GTFS changes from Data Exchange: HTTP status %s", status);
return null;
}

JsonObject agencies = res.getJson().getAsJsonObject();
JsonArray data = agencies.get("data").getAsJsonArray();
JsonObject feed;

for (JsonElement rawFeed : data) {
feed = rawFeed.getAsJsonObject();

String dataExchangeId = feed.get("dataexchange_id").getAsString();

if (dataExchangeId == null || dataExchangeId.equals(""))
return;
continue;

// ORDER BY ...: so failed feeds aren't continuously refetched.
GtfsFeed originalFeed = GtfsFeed.find("dataExchangeId = ? AND supersededBy IS NULL ORDER BY dateUpdated DESC",
Expand All @@ -88,16 +86,17 @@ public void doJob () {
if (originalFeed != null) {
// difference of less than 2000ms: ignore
if ((lastUpdated - originalFeed.dateUpdated.getTime()) < 2000) {
return;
continue;
}
}

// get the data file URL
HttpResponse res = WS.url("http://www.gtfs-data-exchange.com/agency/" +
res = WS.url("http://www.gtfs-data-exchange.com/agency/" +
dataExchangeId + "/json").get();
if (!res.success()) {
Logger.error("Error fetching agency %s, status %s", dataExchangeId, res.getStatus());
return;
status = res.getStatus();
if (status != 200) {
Logger.error("Error fetching agency %s, status %s", dataExchangeId, status);
continue;
}

JsonObject agency = res.getJson().getAsJsonObject();
Expand All @@ -110,7 +109,8 @@ public void doJob () {
String feedId = storer.storeFeed(url);
if (feedId == null) {
Logger.error("Could not retrieve feed %s", url);
JPA.setRollbackOnly();
// feed will be redownloaded on next attempt
continue;
}

GtfsFeed newFeed;
Expand Down Expand Up @@ -145,13 +145,13 @@ public void doJob () {
// still save it in the DB
newFeed.status = FeedParseStatus.FAILED;
newFeed.save();

if (originalFeed != null) {
originalFeed.supersededBy = newFeed;
originalFeed.save();
}

return;
continue;
}

storer.releaseFeed(feedId);
Expand Down Expand Up @@ -179,41 +179,8 @@ public void doJob () {
originalFeed.supersededBy = newFeed;
originalFeed.save();
}
}
}

public Set<MetroArea> update (FeedStorer storer) {
Set<MetroArea> updated = new HashSet<MetroArea>();

// First, fetch the RSS feed
HttpResponse res = WS.url("http://www.gtfs-data-exchange.com/api/agencies").get();
int status = res.getStatus();
if (status != 200) {
Logger.error("Error fetching GTFS changes from Data Exchange: HTTP status %s", status);
return null;
}

JsonObject agencies = res.getJson().getAsJsonObject();
JsonArray data = agencies.get("data").getAsJsonArray();
JsonObject feed;
FetchOneFeed job;

for (JsonElement rawFeed : data) {
feed = rawFeed.getAsJsonObject();

job = new FetchOneFeed(feed, storer, updated);

// per https://groups.google.com/forum/?fromgroups=#!topic/play-framework/j2MbYy6W79w
// this runs the subjob in this thread
try {
job.now().get();
} catch (Exception e) {
Logger.error("Error during retrieval of %s",
feed.get("dataexchange_id").getAsString());
e.printStackTrace();
}
}

return updated;
}
}

return updated;
}
}

0 comments on commit d0ab51c

Please sign in to comment.