Last active
August 29, 2015 13:57
-
-
Save stickfigure/9895662 to your computer and use it in GitHub Desktop.
App Engine tasks which rewrite data for a single entity type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package blah; | |
import com.google.appengine.api.taskqueue.DeferredTask; | |
import com.google.common.base.Function; | |
import com.google.common.collect.Iterables; | |
import com.googlecode.objectify.Key; | |
import lombok.RequiredArgsConstructor; | |
import lombok.extern.slf4j.Slf4j; | |
import st.voo.tick.Queues; | |
import static st.voo.tick.OfyService.ofy; | |
/** | |
* Task which kicks off rewriting a large chunk of data | |
*/ | |
@Slf4j | |
@RequiredArgsConstructor | |
public class RewriteDataTask<T> extends DeferredTask { | |
private static final long serialVersionUID = 1L; | |
final Class<T> type; | |
/** */ | |
@Override | |
public void run() { | |
log.warn("Starting rewrite data"); | |
log.info("############# SPAWNING rewrite for " + type); | |
Iterable<Key<T>> keys = ofy().load().type(type).keys().iterable(); | |
Queues.deflt().add(Iterables.transform(keys, new Function<Key<T>, RewriteSingleTask<T>>() { | |
@Override | |
public RewriteSingleTask<T> apply(Key<T> key) { | |
return makeTask(key); | |
} | |
})); | |
log.warn("Finished spawning rewrite data"); | |
} | |
private RewriteSingleTask<T> makeTask(Key<T> key) { | |
return new RewriteSingleTask<T>(key) { | |
private static final long serialVersionUID = 1L; | |
@Override | |
protected void process(T thing) { | |
RewriteDataTask.this.process(thing); | |
} | |
}; | |
} | |
protected void process(T thing) { | |
// Do nothing by default | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package blah; | |
import com.google.appengine.api.taskqueue.DeferredTask; | |
import com.googlecode.objectify.Key; | |
import com.googlecode.objectify.VoidWork; | |
import lombok.RequiredArgsConstructor; | |
import lombok.extern.slf4j.Slf4j; | |
import static st.voo.tick.OfyService.ofy; | |
/** | |
* Task which rewrites one single piece of data | |
*/ | |
@Slf4j | |
@RequiredArgsConstructor | |
public class RewriteSingleTask<T> extends DeferredTask { | |
private static final long serialVersionUID = 1L; | |
final Key<T> key; | |
/** */ | |
@Override | |
public void run() { | |
ofy().transact(new VoidWork() { | |
@Override | |
public void vrun() { | |
T thing = ofy().load().key(key).safe(); | |
log.info("Processing: " + thing); | |
process(thing); | |
ofy().save().entity(thing); | |
} | |
}); | |
} | |
protected void process(T thing) { | |
// Do nothing by default | |
} | |
} |
The transaction is necessary, otherwise you risk losing changes when a resave conflicts with normal business activity.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Jeff, why would you spawn so many tasks just for individual saves?
Wouldn't it be better to do them in batches?
Here is what I have, I might need to add transaction though: