Merge "Use subscribe pattern to avoid eager stream-events materialization"

This commit is contained in:
David Pursehouse 2016-01-08 06:00:49 +00:00 committed by Gerrit Code Review
commit b3e6f9bb58
2 changed files with 26 additions and 1 deletions

View File

@ -26,6 +26,14 @@ link:access-control.html#capability_streamEvents[the 'Stream Events' global capa
== SCRIPTING
This command is intended to be used in scripts.
== OPTIONS
--subscribe|-s::
Type of the event to subscribe to. Multiple --subscribe options
may be specified to subscribe to multiple events. When this option
is provided, only subscribed events are emitted and all other
events are ignored. When this option is omitted, all events are
emitted.
== EXAMPLES
====
@ -34,6 +42,13 @@ This command is intended to be used in scripts.
{"type":"comment-added",change:{"project":"tools/gerrit", ...}, ...}
====
Only subscribe to specific event types:
====
$ ssh -p 29418 review.example.com gerrit stream-events \
-s draft-published -s patchset-created -s ref-replicated
====
== SCHEMA
The JSON messages consist of nested objects referencing the *change*,
*patchSet*, *account* involved, and other attributes as appropriate.

View File

@ -38,10 +38,13 @@ import com.google.gson.JsonSerializer;
import com.google.inject.Inject;
import org.apache.sshd.server.Environment;
import org.kohsuke.args4j.Option;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
@ -55,6 +58,10 @@ final class StreamEvents extends BaseCommand {
/** Number of events to write before yielding off the thread. */
private static final int BATCH_SIZE = 32;
@Option(name = "--subscribe", aliases = {"-s"}, metaVar = "SUBSCRIBE",
usage = "subscribe to specific stream-events")
private List<String> subscribedToEvents = new ArrayList<>();
@Inject
private IdentifiedUser currentUser;
@ -87,7 +94,10 @@ final class StreamEvents extends BaseCommand {
private final EventListener listener = new EventListener() {
@Override
public void onEvent(final Event event) {
offer(event);
if (subscribedToEvents.isEmpty()
|| subscribedToEvents.contains(event.getType())) {
offer(event);
}
}
};