Cleanup and Refactoring

- Changed watch `name` to watch `id
- `TriggerEngine.Job#name` -> `TriggerEngine.Job#id`
- Removed `Trigger.SourceBuilder` in favor of `Trigger.Builder`
- Fixed compile warnings in `EmailTemplate`

Original commit: elastic/x-pack-elasticsearch@b7fb23712c
This commit is contained in:
uboness 2015-04-23 13:06:24 +02:00
parent 70209698fb
commit ae1d4021c0
35 changed files with 94 additions and 97 deletions

View File

@ -158,19 +158,19 @@ public class EmailTemplate implements ToXContent {
builder.field(Email.Field.FROM.getPreferredName(), from); builder.field(Email.Field.FROM.getPreferredName(), from);
} }
if (replyTo != null) { if (replyTo != null) {
builder.field(Email.Field.REPLY_TO.getPreferredName(), replyTo); builder.field(Email.Field.REPLY_TO.getPreferredName(), (Object[]) replyTo);
} }
if (priority != null) { if (priority != null) {
builder.field(Email.Field.PRIORITY.getPreferredName(), priority); builder.field(Email.Field.PRIORITY.getPreferredName(), priority);
} }
if (to != null) { if (to != null) {
builder.field(Email.Field.TO.getPreferredName(), to); builder.field(Email.Field.TO.getPreferredName(), (Object[]) to);
} }
if (cc != null) { if (cc != null) {
builder.field(Email.Field.CC.getPreferredName(), cc); builder.field(Email.Field.CC.getPreferredName(), (Object[]) cc);
} }
if (bcc != null) { if (bcc != null) {
builder.field(Email.Field.BCC.getPreferredName(), bcc); builder.field(Email.Field.BCC.getPreferredName(), (Object[]) bcc);
} }
if (subject != null) { if (subject != null) {
builder.field(Email.Field.SUBJECT.getPreferredName(), subject); builder.field(Email.Field.SUBJECT.getPreferredName(), subject);

View File

@ -45,7 +45,7 @@ public class ExecutableWebhookAction extends ExecutableAction<WebhookAction, Web
int status = response.status(); int status = response.status();
if (status >= 300) { if (status >= 300) {
logger.warn("received http status [{}] when connecting to watch action [{}/{}/{}]", status, ctx.watch().name(), type(), actionId); logger.warn("received http status [{}] when connecting to watch action [{}/{}/{}]", status, ctx.watch().id(), type(), actionId);
} }
return new WebhookAction.Result.Executed(request, response); return new WebhookAction.Result.Executed(request, response);
} }

View File

@ -32,7 +32,7 @@ import java.util.Map;
*/ */
public class WatchSourceBuilder implements ToXContent { public class WatchSourceBuilder implements ToXContent {
private Trigger.SourceBuilder trigger; private Trigger trigger;
private Input input = NoneInput.INSTANCE; private Input input = NoneInput.INSTANCE;
private Condition condition = AlwaysCondition.INSTANCE; private Condition condition = AlwaysCondition.INSTANCE;
private Transform transform = null; private Transform transform = null;
@ -40,7 +40,11 @@ public class WatchSourceBuilder implements ToXContent {
private TimeValue throttlePeriod = null; private TimeValue throttlePeriod = null;
private Map<String, Object> metadata; private Map<String, Object> metadata;
public WatchSourceBuilder trigger(Trigger.SourceBuilder trigger) { public WatchSourceBuilder trigger(Trigger.Builder trigger) {
return trigger(trigger.build());
}
public WatchSourceBuilder trigger(Trigger trigger) {
this.trigger = trigger; this.trigger = trigger;
return this; return this;
} }

View File

@ -5,7 +5,6 @@
*/ */
package org.elasticsearch.watcher.condition.script; package org.elasticsearch.watcher.condition.script;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.ExecutableScript;
import org.elasticsearch.watcher.condition.ConditionException; import org.elasticsearch.watcher.condition.ConditionException;
@ -35,6 +34,6 @@ public class ExecutableScriptCondition extends ExecutableCondition<ScriptConditi
if (value instanceof Boolean) { if (value instanceof Boolean) {
return (Boolean) value ? ScriptCondition.Result.MET : ScriptCondition.Result.UNMET; return (Boolean) value ? ScriptCondition.Result.MET : ScriptCondition.Result.UNMET;
} }
throw new ConditionException("failed to execute [{}] condition for watch [{}]. script [{}] must return a boolean value (true|false) but instead returned [{}]", type(), ctx.watch().name(), condition.script.script(), value); throw new ConditionException("failed to execute [{}] condition for watch [{}]. script [{}] must return a boolean value (true|false) but instead returned [{}]", type(), ctx.watch().id(), condition.script.script(), value);
} }
} }

View File

@ -211,7 +211,7 @@ public class ExecutionService extends AbstractComponent {
public WatchRecord execute(WatchExecutionContext ctx) throws IOException { public WatchRecord execute(WatchExecutionContext ctx) throws IOException {
WatchRecord watchRecord = new WatchRecord(ctx.id(), ctx.watch(), ctx.triggerEvent()); WatchRecord watchRecord = new WatchRecord(ctx.id(), ctx.watch(), ctx.triggerEvent());
WatchLockService.Lock lock = watchLockService.acquire(ctx.watch().name()); WatchLockService.Lock lock = watchLockService.acquire(ctx.watch().id());
try { try {
WatchExecution execution = executeInner(ctx); WatchExecution execution = executeInner(ctx);
watchRecord.seal(execution); watchRecord.seal(execution);
@ -341,8 +341,8 @@ public class ExecutionService extends AbstractComponent {
logger.debug("can't initiate watch execution as execution service is not started, ignoring it..."); logger.debug("can't initiate watch execution as execution service is not started, ignoring it...");
return; return;
} }
logger.trace("executing [{}] [{}]", ctx.watch().name(), ctx.id()); logger.trace("executing [{}] [{}]", ctx.watch().id(), ctx.id());
WatchLockService.Lock lock = watchLockService.acquire(ctx.watch().name()); WatchLockService.Lock lock = watchLockService.acquire(ctx.watch().id());
try { try {
watchRecord.update(WatchRecord.State.CHECKING, null); watchRecord.update(WatchRecord.State.CHECKING, null);
logger.debug("checking watch [{}]", watchRecord.name()); logger.debug("checking watch [{}]", watchRecord.name());
@ -369,7 +369,7 @@ public class ExecutionService extends AbstractComponent {
} }
} finally { } finally {
lock.release(); lock.release();
logger.trace("finished [{}]/[{}]", ctx.watch().name(), ctx.id()); logger.trace("finished [{}]/[{}]", ctx.watch().id(), ctx.id());
} }
} }

View File

@ -11,7 +11,6 @@ import org.elasticsearch.common.collect.ImmutableSet;
import org.elasticsearch.common.joda.time.DateTime; import org.elasticsearch.common.joda.time.DateTime;
import org.elasticsearch.common.joda.time.DateTimeZone; import org.elasticsearch.common.joda.time.DateTimeZone;
import org.elasticsearch.watcher.condition.Condition; import org.elasticsearch.watcher.condition.Condition;
import org.elasticsearch.watcher.input.ExecutableInput;
import org.elasticsearch.watcher.input.Input; import org.elasticsearch.watcher.input.Input;
import org.elasticsearch.watcher.throttle.Throttler; import org.elasticsearch.watcher.throttle.Throttler;
import org.elasticsearch.watcher.trigger.manual.ManualTriggerEvent; import org.elasticsearch.watcher.trigger.manual.ManualTriggerEvent;
@ -121,7 +120,7 @@ public class ManualExecutionContext extends WatchExecutionContext {
executionTime = DateTime.now(DateTimeZone.UTC); executionTime = DateTime.now(DateTimeZone.UTC);
} }
if (triggerEvent == null) { if (triggerEvent == null) {
triggerEvent = new ManualTriggerEvent(watch.name(), executionTime, new HashMap<String, Object>()); triggerEvent = new ManualTriggerEvent(watch.id(), executionTime, new HashMap<String, Object>());
} }
return new ManualExecutionContext(watch, executionTime, triggerEvent, inputResult, conditionResult, throttlerResult, simulateActionPredicate, recordExecution); return new ManualExecutionContext(watch, executionTime, triggerEvent, inputResult, conditionResult, throttlerResult, simulateActionPredicate, recordExecution);
} }

View File

@ -11,7 +11,6 @@ import org.elasticsearch.watcher.actions.ExecutableActions;
import org.elasticsearch.watcher.condition.Condition; import org.elasticsearch.watcher.condition.Condition;
import org.elasticsearch.watcher.input.Input; import org.elasticsearch.watcher.input.Input;
import org.elasticsearch.watcher.throttle.Throttler; import org.elasticsearch.watcher.throttle.Throttler;
import org.elasticsearch.watcher.transform.ExecutableTransform;
import org.elasticsearch.watcher.transform.Transform; import org.elasticsearch.watcher.transform.Transform;
import org.elasticsearch.watcher.trigger.TriggerEvent; import org.elasticsearch.watcher.trigger.TriggerEvent;
import org.elasticsearch.watcher.watch.Payload; import org.elasticsearch.watcher.watch.Payload;
@ -43,7 +42,7 @@ public abstract class WatchExecutionContext {
this.watch = watch; this.watch = watch;
this.executionTime = executionTime; this.executionTime = executionTime;
this.triggerEvent = triggerEvent; this.triggerEvent = triggerEvent;
this.id = new Wid(watch.name(), watch.nonce(), executionTime); this.id = new Wid(watch.id(), watch.nonce(), executionTime);
} }
/** /**

View File

@ -58,7 +58,7 @@ public class WatchRecord implements ToXContent {
public WatchRecord(Wid id, Watch watch, TriggerEvent triggerEvent) { public WatchRecord(Wid id, Watch watch, TriggerEvent triggerEvent) {
this.id = id; this.id = id;
this.name = watch.name(); this.name = watch.id();
this.triggerEvent = triggerEvent; this.triggerEvent = triggerEvent;
this.condition = watch.condition().condition(); this.condition = watch.condition().condition();
this.input = watch.input(); this.input = watch.input();

View File

@ -51,13 +51,13 @@ public class ExecutableSearchInput extends ExecutableInput<SearchInput, SearchIn
SearchRequest request = createSearchRequestWithTimes(input.getSearchRequest(), ctx, scriptService); SearchRequest request = createSearchRequestWithTimes(input.getSearchRequest(), ctx, scriptService);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("[{}] running query for [{}] [{}]", ctx.id(), ctx.watch().name(), XContentHelper.convertToJson(request.source(), false, true)); logger.trace("[{}] running query for [{}] [{}]", ctx.id(), ctx.watch().id(), XContentHelper.convertToJson(request.source(), false, true));
} }
// actionGet deals properly with InterruptedException // actionGet deals properly with InterruptedException
SearchResponse response = client.search(request); SearchResponse response = client.search(request);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("[{}] found [{}] hits", ctx.id(), ctx.watch().name(), response.getHits().getTotalHits()); logger.debug("[{}] found [{}] hits", ctx.id(), ctx.watch().id(), response.getHits().getTotalHits());
for (SearchHit hit : response.getHits()) { for (SearchHit hit : response.getHits()) {
logger.debug("[{}] hit [{}]", ctx.id(), XContentHelper.toString(hit)); logger.debug("[{}] hit [{}]", ctx.id(), XContentHelper.toString(hit));
} }

View File

@ -25,7 +25,7 @@ public final class Variables {
public static Map<String, Object> createCtxModel(WatchExecutionContext ctx, Payload payload) { public static Map<String, Object> createCtxModel(WatchExecutionContext ctx, Payload payload) {
Map<String, Object> vars = new HashMap<>(); Map<String, Object> vars = new HashMap<>();
vars.put(WATCH_ID, ctx.watch().name()); vars.put(WATCH_ID, ctx.watch().id());
vars.put(EXECUTION_TIME, ctx.executionTime()); vars.put(EXECUTION_TIME, ctx.executionTime());
vars.put(TRIGGER, ctx.triggerEvent().data()); vars.put(TRIGGER, ctx.triggerEvent().data());
if (payload != null) { if (payload != null) {

View File

@ -17,7 +17,7 @@ public class AckThrottler implements Throttler {
@Override @Override
public Result throttle(WatchExecutionContext ctx) { public Result throttle(WatchExecutionContext ctx) {
if (ctx.watch().acked()) { if (ctx.watch().acked()) {
return Result.throttle("watch [" + ctx.watch().name() + "] was acked at [" + formatDate(ctx.watch().status().ackStatus().timestamp()) + "]"); return Result.throttle("watch [" + ctx.watch().id() + "] was acked at [" + formatDate(ctx.watch().status().ackStatus().timestamp()) + "]");
} }
return Result.NO; return Result.NO;
} }

View File

@ -25,7 +25,6 @@ import org.elasticsearch.watcher.condition.always.AlwaysCondition;
import org.elasticsearch.watcher.execution.ExecutionService; import org.elasticsearch.watcher.execution.ExecutionService;
import org.elasticsearch.watcher.execution.ManualExecutionContext; import org.elasticsearch.watcher.execution.ManualExecutionContext;
import org.elasticsearch.watcher.history.WatchRecord; import org.elasticsearch.watcher.history.WatchRecord;
import org.elasticsearch.watcher.input.simple.ExecutableSimpleInput;
import org.elasticsearch.watcher.input.simple.SimpleInput; import org.elasticsearch.watcher.input.simple.SimpleInput;
import org.elasticsearch.watcher.license.LicenseService; import org.elasticsearch.watcher.license.LicenseService;
import org.elasticsearch.watcher.support.clock.Clock; import org.elasticsearch.watcher.support.clock.Clock;
@ -89,7 +88,7 @@ public class TransportExecuteWatchAction extends WatcherTransportAction<ExecuteW
} }
} }
if (request.getTriggerData() != null) { if (request.getTriggerData() != null) {
ctxBuilder.triggerEvent(new ManualTriggerEvent(watch.name(), executionTime, request.getTriggerData())); ctxBuilder.triggerEvent(new ManualTriggerEvent(watch.id(), executionTime, request.getTriggerData()));
} }
if (request.getAlternativeInput() != null) { if (request.getAlternativeInput() != null) {
ctxBuilder.withInput(new SimpleInput.Result(new Payload.Simple(request.getAlternativeInput()))); ctxBuilder.withInput(new SimpleInput.Result(new Payload.Simple(request.getAlternativeInput())));

View File

@ -73,7 +73,7 @@ public class TransportGetWatchAction extends WatcherTransportAction<GetWatchRequ
listener.onFailure(e); listener.onFailure(e);
return; return;
} }
listener.onResponse(new GetWatchResponse(watch.name(), watch.status().version(), true, watchSource)); listener.onResponse(new GetWatchResponse(watch.id(), watch.status().version(), true, watchSource));
} catch (Throwable t) { } catch (Throwable t) {
logger.error("failed to get watch [{}]", t, request.getId()); logger.error("failed to get watch [{}]", t, request.getId());

View File

@ -17,16 +17,16 @@ public interface Trigger extends ToXContent {
String type(); String type();
public static interface Parser<T extends Trigger> { interface Parser<T extends Trigger> {
String type(); String type();
T parse(XContentParser parser) throws IOException; T parse(XContentParser parser) throws IOException;
} }
public static interface SourceBuilder extends ToXContent { interface Builder<T extends Trigger> {
String type(); T build();
} }
} }

View File

@ -16,7 +16,7 @@ public final class TriggerBuilders {
private TriggerBuilders() { private TriggerBuilders() {
} }
public static ScheduleTrigger.SourceBuilder schedule(Schedule schedule) { public static ScheduleTrigger.Builder schedule(Schedule schedule) {
return new ScheduleTrigger.SourceBuilder(schedule); return ScheduleTrigger.builder(schedule);
} }
} }

View File

@ -32,24 +32,24 @@ public interface TriggerEngine<T extends Trigger, E extends TriggerEvent> {
/** /**
* Removes the job associated with the given name from this trigger engine. * Removes the job associated with the given name from this trigger engine.
* *
* @param jobName The name of the job to remove * @param jobId The name of the job to remove
* @return {@code true} if the job existed and removed, {@code false} otherwise. * @return {@code true} if the job existed and removed, {@code false} otherwise.
*/ */
boolean remove(String jobName); boolean remove(String jobId);
T parseTrigger(String context, XContentParser parser) throws IOException; T parseTrigger(String context, XContentParser parser) throws IOException;
E parseTriggerEvent(String context, XContentParser parser) throws IOException; E parseTriggerEvent(String context, XContentParser parser) throws IOException;
public static interface Listener { interface Listener {
void triggered(Iterable<TriggerEvent> events); void triggered(Iterable<TriggerEvent> events);
} }
public static interface Job { interface Job {
String name(); String id();
Trigger trigger(); Trigger trigger();
} }

View File

@ -50,7 +50,7 @@ public class ManualTriggerEngine implements TriggerEngine<ManualTrigger,ManualTr
} }
@Override @Override
public boolean remove(String jobName) { public boolean remove(String jobId) {
return false; return false;
} }

View File

@ -28,15 +28,10 @@ public class ScheduleTrigger implements Trigger {
return TYPE; return TYPE;
} }
public Schedule schedule() { public Schedule getSchedule() {
return schedule; return schedule;
} }
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject().field(schedule.type(), schedule).endObject();
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
@ -54,24 +49,26 @@ public class ScheduleTrigger implements Trigger {
return schedule.hashCode(); return schedule.hashCode();
} }
public static class SourceBuilder implements Trigger.SourceBuilder { @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject().field(schedule.type(), schedule).endObject();
}
public static Builder builder(Schedule schedule) {
return new Builder(schedule);
}
public static class Builder implements Trigger.Builder<ScheduleTrigger> {
private final Schedule schedule; private final Schedule schedule;
public SourceBuilder(Schedule schedule) { private Builder(Schedule schedule) {
this.schedule = schedule; this.schedule = schedule;
} }
@Override @Override
public String type() { public ScheduleTrigger build() {
return TYPE; return new ScheduleTrigger(schedule);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.startObject()
.field(schedule.type(), schedule)
.endObject();
} }
} }
} }

View File

@ -47,7 +47,7 @@ public class SchedulerScheduleTriggerEngine extends ScheduleTriggerEngine {
for (Job job : jobs) { for (Job job : jobs) {
if (job.trigger() instanceof ScheduleTrigger) { if (job.trigger() instanceof ScheduleTrigger) {
ScheduleTrigger trigger = (ScheduleTrigger) job.trigger(); ScheduleTrigger trigger = (ScheduleTrigger) job.trigger();
schedules.add(new ActiveSchedule(job.name(), trigger.schedule(), starTime)); schedules.add(new ActiveSchedule(job.id(), trigger.getSchedule(), starTime));
} }
} }
this.schedules = new Schedules(schedules); this.schedules = new Schedules(schedules);
@ -70,13 +70,13 @@ public class SchedulerScheduleTriggerEngine extends ScheduleTriggerEngine {
public void add(Job job) { public void add(Job job) {
assert job.trigger() instanceof ScheduleTrigger; assert job.trigger() instanceof ScheduleTrigger;
ScheduleTrigger trigger = (ScheduleTrigger) job.trigger(); ScheduleTrigger trigger = (ScheduleTrigger) job.trigger();
ActiveSchedule schedule = new ActiveSchedule(job.name(), trigger.schedule(), clock.millis()); ActiveSchedule schedule = new ActiveSchedule(job.id(), trigger.getSchedule(), clock.millis());
schedules = schedules.add(schedule); schedules = schedules.add(schedule);
} }
@Override @Override
public boolean remove(String jobName) { public boolean remove(String jobId) {
Schedules newSchedules = schedules.remove(jobName); Schedules newSchedules = schedules.remove(jobId);
if (newSchedules == null) { if (newSchedules == null) {
return false; return false;
} }

View File

@ -47,7 +47,7 @@ public class TickerScheduleTriggerEngine extends ScheduleTriggerEngine {
for (Job job : jobs) { for (Job job : jobs) {
if (job.trigger() instanceof ScheduleTrigger) { if (job.trigger() instanceof ScheduleTrigger) {
ScheduleTrigger trigger = (ScheduleTrigger) job.trigger(); ScheduleTrigger trigger = (ScheduleTrigger) job.trigger();
schedules.put(job.name(), new ActiveSchedule(job.name(), trigger.schedule(), starTime)); schedules.put(job.id(), new ActiveSchedule(job.id(), trigger.getSchedule(), starTime));
} }
} }
this.schedules = schedules; this.schedules = schedules;
@ -63,12 +63,12 @@ public class TickerScheduleTriggerEngine extends ScheduleTriggerEngine {
public void add(Job job) { public void add(Job job) {
assert job.trigger() instanceof ScheduleTrigger; assert job.trigger() instanceof ScheduleTrigger;
ScheduleTrigger trigger = (ScheduleTrigger) job.trigger(); ScheduleTrigger trigger = (ScheduleTrigger) job.trigger();
schedules.put(job.name(), new ActiveSchedule(job.name(), trigger.schedule(), clock.millis())); schedules.put(job.id(), new ActiveSchedule(job.id(), trigger.getSchedule(), clock.millis()));
} }
@Override @Override
public boolean remove(String jobName) { public boolean remove(String jobId) {
return schedules.remove(jobName) != null; return schedules.remove(jobId) != null;
} }
void checkJobs() { void checkJobs() {

View File

@ -54,7 +54,7 @@ public class Watch implements TriggerEngine.Job, ToXContent {
private final static TimeValue DEFAULT_THROTTLE_PERIOD = new TimeValue(5, TimeUnit.SECONDS); private final static TimeValue DEFAULT_THROTTLE_PERIOD = new TimeValue(5, TimeUnit.SECONDS);
private final static String DEFAULT_THROTTLE_PERIOD_SETTING = "watcher.throttle.period.default_period"; private final static String DEFAULT_THROTTLE_PERIOD_SETTING = "watcher.throttle.period.default_period";
private final String name; private final String id;
private final Trigger trigger; private final Trigger trigger;
private final ExecutableInput input; private final ExecutableInput input;
private final ExecutableCondition condition; private final ExecutableCondition condition;
@ -71,9 +71,9 @@ public class Watch implements TriggerEngine.Job, ToXContent {
private final transient AtomicLong nonceCounter = new AtomicLong(); private final transient AtomicLong nonceCounter = new AtomicLong();
public Watch(String name, Clock clock, LicenseService licenseService, Trigger trigger, ExecutableInput input, ExecutableCondition condition, @Nullable ExecutableTransform transform, public Watch(String id, Clock clock, LicenseService licenseService, Trigger trigger, ExecutableInput input, ExecutableCondition condition, @Nullable ExecutableTransform transform,
ExecutableActions actions, @Nullable Map<String, Object> metadata, @Nullable TimeValue throttlePeriod, @Nullable Status status) { ExecutableActions actions, @Nullable Map<String, Object> metadata, @Nullable TimeValue throttlePeriod, @Nullable Status status) {
this.name = name; this.id = id;
this.trigger = trigger; this.trigger = trigger;
this.input = input; this.input = input;
this.condition = condition; this.condition = condition;
@ -85,8 +85,8 @@ public class Watch implements TriggerEngine.Job, ToXContent {
throttler = new WatchThrottler(clock, throttlePeriod, licenseService); throttler = new WatchThrottler(clock, throttlePeriod, licenseService);
} }
public String name() { public String id() {
return name; return id;
} }
public Trigger trigger() { public Trigger trigger() {
@ -147,12 +147,12 @@ public class Watch implements TriggerEngine.Job, ToXContent {
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
Watch watch = (Watch) o; Watch watch = (Watch) o;
return watch.name.equals(name); return watch.id.equals(id);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return name.hashCode(); return id.hashCode();
} }
@Override @Override

View File

@ -163,9 +163,9 @@ public class WatchStore extends AbstractComponent {
*/ */
void update(Watch watch) throws IOException { void update(Watch watch) throws IOException {
ensureStarted(); ensureStarted();
assert watch == watches.get(watch.name()) : "update watch can only be applied to an already loaded watch"; assert watch == watches.get(watch.id()) : "update watch can only be applied to an already loaded watch";
BytesReference source = JsonXContent.contentBuilder().value(watch).bytes(); BytesReference source = JsonXContent.contentBuilder().value(watch).bytes();
IndexResponse response = client.index(createIndexRequest(watch.name(), source)); IndexResponse response = client.index(createIndexRequest(watch.id(), source));
watch.status().version(response.getVersion()); watch.status().version(response.getVersion());
watch.status().dirty(false); watch.status().dirty(false);
// Don't need to update the watches, since we are working on an instance from it. // Don't need to update the watches, since we are working on an instance from it.

View File

@ -70,7 +70,7 @@ public class IndexActionTests extends ElasticsearchIntegrationTest {
} }
}, },
logger); logger);
WatchExecutionContext ctx = new TriggeredExecutionContext(watch, new DateTime(), new ScheduleTriggerEvent(watch.name(), new DateTime(), new DateTime())); WatchExecutionContext ctx = new TriggeredExecutionContext(watch, new DateTime(), new ScheduleTriggerEvent(watch.id(), new DateTime(), new DateTime()));
Map<String, Object> payloadMap = new HashMap<>(); Map<String, Object> payloadMap = new HashMap<>();
payloadMap.put("test", "foo"); payloadMap.put("test", "foo");

View File

@ -114,7 +114,7 @@ public class WebhookActionTests extends ElasticsearchTestCase {
ExecutableWebhookAction executable = new ExecutableWebhookAction(action, logger, httpClient, templateEngine); ExecutableWebhookAction executable = new ExecutableWebhookAction(action, logger, httpClient, templateEngine);
Watch watch = createWatch("test_watch", client, account); Watch watch = createWatch("test_watch", client, account);
WatchExecutionContext ctx = new TriggeredExecutionContext(watch, new DateTime(), new ScheduleTriggerEvent(watch.name(), new DateTime(), new DateTime())); WatchExecutionContext ctx = new TriggeredExecutionContext(watch, new DateTime(), new ScheduleTriggerEvent(watch.id(), new DateTime(), new DateTime()));
WebhookAction.Result actionResult = executable.execute("_id", ctx, new Payload.Simple()); WebhookAction.Result actionResult = executable.execute("_id", ctx, new Payload.Simple());
scenario.assertResult(actionResult); scenario.assertResult(actionResult);

View File

@ -37,7 +37,7 @@ public class HistoryStoreLifeCycleTest extends AbstractWatcherIntegrationTests {
WatchRecord[] watchRecords = new WatchRecord[randomIntBetween(1, 50)]; WatchRecord[] watchRecords = new WatchRecord[randomIntBetween(1, 50)];
for (int i = 0; i < watchRecords.length; i++) { for (int i = 0; i < watchRecords.length; i++) {
DateTime dateTime = new DateTime(i, DateTimeZone.UTC); DateTime dateTime = new DateTime(i, DateTimeZone.UTC);
ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.name(), dateTime, dateTime); ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.id(), dateTime, dateTime);
Wid wid = new Wid("record_" + i, randomLong(), DateTime.now(UTC)); Wid wid = new Wid("record_" + i, randomLong(), DateTime.now(UTC));
watchRecords[i] = new WatchRecord(wid, watch, event); watchRecords[i] = new WatchRecord(wid, watch, event);
historyStore.put(watchRecords[i]); historyStore.put(watchRecords[i]);

View File

@ -72,11 +72,11 @@ public class HistoryStoreTests extends ElasticsearchTestCase {
@Test @Test
public void testPut() throws Exception { public void testPut() throws Exception {
Watch watch = mock(Watch.class); Watch watch = mock(Watch.class);
when(watch.name()).thenReturn("_name"); when(watch.id()).thenReturn("_name");
when(watch.condition()).thenReturn(new ExecutableAlwaysCondition(logger)); when(watch.condition()).thenReturn(new ExecutableAlwaysCondition(logger));
when(watch.input()).thenReturn(null); when(watch.input()).thenReturn(null);
when(watch.metadata()).thenReturn(null); when(watch.metadata()).thenReturn(null);
ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.name(), new DateTime(0, DateTimeZone.UTC), new DateTime(0, DateTimeZone.UTC)); ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.id(), new DateTime(0, DateTimeZone.UTC), new DateTime(0, DateTimeZone.UTC));
Wid wid = new Wid("_name", 0, new DateTime(0, DateTimeZone.UTC)); Wid wid = new Wid("_name", 0, new DateTime(0, DateTimeZone.UTC));
WatchRecord watchRecord = new WatchRecord(wid, watch, event); WatchRecord watchRecord = new WatchRecord(wid, watch, event);
@ -92,11 +92,11 @@ public class HistoryStoreTests extends ElasticsearchTestCase {
@Test @Test
public void testUpdate() throws Exception { public void testUpdate() throws Exception {
Watch watch = mock(Watch.class); Watch watch = mock(Watch.class);
when(watch.name()).thenReturn("_name"); when(watch.id()).thenReturn("_name");
when(watch.condition()).thenReturn(new ExecutableAlwaysCondition(logger)); when(watch.condition()).thenReturn(new ExecutableAlwaysCondition(logger));
when(watch.input()).thenReturn(null); when(watch.input()).thenReturn(null);
when(watch.metadata()).thenReturn(null); when(watch.metadata()).thenReturn(null);
ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.name(), new DateTime(0, DateTimeZone.UTC), new DateTime(0, DateTimeZone.UTC)); ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.id(), new DateTime(0, DateTimeZone.UTC), new DateTime(0, DateTimeZone.UTC));
Wid wid = new Wid("_name", 0, new DateTime(0, DateTimeZone.UTC)); Wid wid = new Wid("_name", 0, new DateTime(0, DateTimeZone.UTC));
WatchRecord watchRecord = new WatchRecord(wid, watch, event); WatchRecord watchRecord = new WatchRecord(wid, watch, event);
watchRecord.version(4l); watchRecord.version(4l);
@ -113,11 +113,11 @@ public class HistoryStoreTests extends ElasticsearchTestCase {
@Test(expected = HistoryException.class) @Test(expected = HistoryException.class)
public void testPut_stopped() { public void testPut_stopped() {
Watch watch = mock(Watch.class); Watch watch = mock(Watch.class);
when(watch.name()).thenReturn("_name"); when(watch.id()).thenReturn("_name");
when(watch.condition()).thenReturn(new ExecutableAlwaysCondition(logger)); when(watch.condition()).thenReturn(new ExecutableAlwaysCondition(logger));
when(watch.input()).thenReturn(null); when(watch.input()).thenReturn(null);
when(watch.metadata()).thenReturn(null); when(watch.metadata()).thenReturn(null);
ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.name(), new DateTime(0, DateTimeZone.UTC), new DateTime(0, DateTimeZone.UTC)); ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.id(), new DateTime(0, DateTimeZone.UTC), new DateTime(0, DateTimeZone.UTC));
Wid wid = new Wid("_name", 0, new DateTime(0, DateTimeZone.UTC)); Wid wid = new Wid("_name", 0, new DateTime(0, DateTimeZone.UTC));
WatchRecord watchRecord = new WatchRecord(wid, watch, event); WatchRecord watchRecord = new WatchRecord(wid, watch, event);
@ -133,11 +133,11 @@ public class HistoryStoreTests extends ElasticsearchTestCase {
@Test(expected = HistoryException.class) @Test(expected = HistoryException.class)
public void testUpdate_stopped() throws Exception { public void testUpdate_stopped() throws Exception {
Watch watch = mock(Watch.class); Watch watch = mock(Watch.class);
when(watch.name()).thenReturn("_name"); when(watch.id()).thenReturn("_name");
when(watch.condition()).thenReturn(new ExecutableAlwaysCondition(logger)); when(watch.condition()).thenReturn(new ExecutableAlwaysCondition(logger));
when(watch.input()).thenReturn(null); when(watch.input()).thenReturn(null);
when(watch.metadata()).thenReturn(null); when(watch.metadata()).thenReturn(null);
ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.name(), new DateTime(0, DateTimeZone.UTC), new DateTime(0, DateTimeZone.UTC)); ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.id(), new DateTime(0, DateTimeZone.UTC), new DateTime(0, DateTimeZone.UTC));
Wid wid = new Wid("_name", 0, new DateTime(0, DateTimeZone.UTC)); Wid wid = new Wid("_name", 0, new DateTime(0, DateTimeZone.UTC));
WatchRecord watchRecord = new WatchRecord(wid, watch, event); WatchRecord watchRecord = new WatchRecord(wid, watch, event);

View File

@ -39,7 +39,7 @@ public class WatchRecordTests extends AbstractWatcherIntegrationTests {
@Test @Test
public void testParser() throws Exception { public void testParser() throws Exception {
Watch watch = WatcherTestUtils.createTestWatch("fired_test", scriptService(), httpClient(), noopEmailService(), logger); Watch watch = WatcherTestUtils.createTestWatch("fired_test", scriptService(), httpClient(), noopEmailService(), logger);
ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.name(), DateTime.now(UTC), DateTime.now(UTC)); ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.id(), DateTime.now(UTC), DateTime.now(UTC));
Wid wid = new Wid("_record", randomLong(), DateTime.now(UTC)); Wid wid = new Wid("_record", randomLong(), DateTime.now(UTC));
WatchRecord watchRecord = new WatchRecord(wid, watch, event); WatchRecord watchRecord = new WatchRecord(wid, watch, event);
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder(); XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
@ -55,7 +55,7 @@ public class WatchRecordTests extends AbstractWatcherIntegrationTests {
@Test @Test
public void testParser_WithSealedWatchRecord() throws Exception { public void testParser_WithSealedWatchRecord() throws Exception {
Watch watch = WatcherTestUtils.createTestWatch("fired_test", scriptService(), httpClient(), noopEmailService(), logger); Watch watch = WatcherTestUtils.createTestWatch("fired_test", scriptService(), httpClient(), noopEmailService(), logger);
ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.name(), DateTime.now(UTC), DateTime.now(UTC)); ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.id(), DateTime.now(UTC), DateTime.now(UTC));
Wid wid = new Wid("_record", randomLong(), DateTime.now(UTC)); Wid wid = new Wid("_record", randomLong(), DateTime.now(UTC));
WatchRecord watchRecord = new WatchRecord(wid, watch, event); WatchRecord watchRecord = new WatchRecord(wid, watch, event);
WatchExecutionContext ctx = new TriggeredExecutionContext(watch, new DateTime(), event); WatchExecutionContext ctx = new TriggeredExecutionContext(watch, new DateTime(), event);
@ -85,7 +85,7 @@ public class WatchRecordTests extends AbstractWatcherIntegrationTests {
@Test @Test
public void testParser_WithSealedWatchRecord_WithScriptSearchCondition() throws Exception { public void testParser_WithSealedWatchRecord_WithScriptSearchCondition() throws Exception {
Watch watch = WatcherTestUtils.createTestWatch("fired_test", scriptService(), httpClient(), noopEmailService(), logger); Watch watch = WatcherTestUtils.createTestWatch("fired_test", scriptService(), httpClient(), noopEmailService(), logger);
ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.name(), DateTime.now(UTC), DateTime.now(UTC)); ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.id(), DateTime.now(UTC), DateTime.now(UTC));
WatchExecutionContext ctx = new TriggeredExecutionContext( watch, new DateTime(), event); WatchExecutionContext ctx = new TriggeredExecutionContext( watch, new DateTime(), event);
WatchRecord watchRecord = new WatchRecord(ctx.id(), watch, event); WatchRecord watchRecord = new WatchRecord(ctx.id(), watch, event);
ctx.onActionResult(new ActionWrapper.Result("_email", new EmailAction.Result.Failure("failed to send because blah"))); ctx.onActionResult(new ActionWrapper.Result("_email", new EmailAction.Result.Failure("failed to send because blah")));

View File

@ -93,7 +93,7 @@ public class HttpInputTests extends ElasticsearchTestCase {
new Watch.Status()); new Watch.Status());
WatchExecutionContext ctx = new TriggeredExecutionContext(watch, WatchExecutionContext ctx = new TriggeredExecutionContext(watch,
new DateTime(0, DateTimeZone.UTC), new DateTime(0, DateTimeZone.UTC),
new ScheduleTriggerEvent(watch.name(), new DateTime(0, DateTimeZone.UTC), new DateTime(0, DateTimeZone.UTC))); new ScheduleTriggerEvent(watch.id(), new DateTime(0, DateTimeZone.UTC), new DateTime(0, DateTimeZone.UTC)));
HttpInput.Result result = input.execute(ctx); HttpInput.Result result = input.execute(ctx);
assertThat(result.type(), equalTo(HttpInput.TYPE)); assertThat(result.type(), equalTo(HttpInput.TYPE));
assertThat(result.payload().data(), equalTo(MapBuilder.<String, Object>newMapBuilder().put("key", "value").map())); assertThat(result.payload().data(), equalTo(MapBuilder.<String, Object>newMapBuilder().put("key", "value").map()));

View File

@ -32,7 +32,7 @@ public class WatchExecutionContextMockBuilder {
public WatchExecutionContextMockBuilder(String watchId) { public WatchExecutionContextMockBuilder(String watchId) {
ctx = mock(WatchExecutionContext.class); ctx = mock(WatchExecutionContext.class);
watch = mock(Watch.class); watch = mock(Watch.class);
when(watch.name()).thenReturn(watchId); when(watch.id()).thenReturn(watchId);
when(ctx.watch()).thenReturn(watch); when(ctx.watch()).thenReturn(watch);
payload(Collections.<String, Object>emptyMap()); payload(Collections.<String, Object>emptyMap());
metadata(Collections.<String, Object>emptyMap()); metadata(Collections.<String, Object>emptyMap());

View File

@ -143,7 +143,7 @@ public class ScheduleEngineTriggerBenchmark {
} }
@Override @Override
public String name() { public String id() {
return name; return name;
} }

View File

@ -99,14 +99,14 @@ public class BootStrapTests extends AbstractWatcherIntegrationTests {
new Watch.Status()); new Watch.Status());
XContentBuilder builder = jsonBuilder().value(watch); XContentBuilder builder = jsonBuilder().value(watch);
IndexResponse indexResponse = client().prepareIndex(WatchStore.INDEX, WatchStore.DOC_TYPE, watch.name()) IndexResponse indexResponse = client().prepareIndex(WatchStore.INDEX, WatchStore.DOC_TYPE, watch.id())
.setSource(builder).get(); .setSource(builder).get();
ensureGreen(WatchStore.INDEX); ensureGreen(WatchStore.INDEX);
refresh(); refresh();
assertThat(indexResponse.isCreated(), is(true)); assertThat(indexResponse.isCreated(), is(true));
DateTime now = DateTime.now(UTC); DateTime now = DateTime.now(UTC);
ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.name(), now, now); ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.id(), now, now);
Wid wid = new Wid("_record", randomLong(), DateTime.now(UTC)); Wid wid = new Wid("_record", randomLong(), DateTime.now(UTC));
WatchRecord watchRecord = new WatchRecord(wid, watch, event); WatchRecord watchRecord = new WatchRecord(wid, watch, event);
String actionHistoryIndex = HistoryStore.getHistoryIndexNameForTime(now); String actionHistoryIndex = HistoryStore.getHistoryIndexNameForTime(now);
@ -162,10 +162,10 @@ public class BootStrapTests extends AbstractWatcherIntegrationTests {
XContentBuilder jsonBuilder = jsonBuilder(); XContentBuilder jsonBuilder = jsonBuilder();
watch.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS); watch.toXContent(jsonBuilder, ToXContent.EMPTY_PARAMS);
PutWatchResponse putWatchResponse = watcherClient().preparePutWatch(watch.name()).setSource(jsonBuilder.bytes()).get(); PutWatchResponse putWatchResponse = watcherClient().preparePutWatch(watch.id()).setSource(jsonBuilder.bytes()).get();
assertThat(putWatchResponse.isCreated(), is(true)); assertThat(putWatchResponse.isCreated(), is(true));
ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.name(), historyIndexDate, historyIndexDate); ScheduleTriggerEvent event = new ScheduleTriggerEvent(watch.id(), historyIndexDate, historyIndexDate);
Wid wid = new Wid("record_" + i, randomLong(), DateTime.now(UTC)); Wid wid = new Wid("record_" + i, randomLong(), DateTime.now(UTC));
WatchRecord watchRecord = new WatchRecord(wid, watch, event); WatchRecord watchRecord = new WatchRecord(wid, watch, event);

View File

@ -32,7 +32,7 @@ public class AckThrottlerTests extends ElasticsearchTestCase {
Watch.Status status = mock(Watch.Status.class); Watch.Status status = mock(Watch.Status.class);
when(status.ackStatus()).thenReturn(new Watch.Status.AckStatus(Watch.Status.AckStatus.State.ACKED, timestamp)); when(status.ackStatus()).thenReturn(new Watch.Status.AckStatus(Watch.Status.AckStatus.State.ACKED, timestamp));
when(watch.status()).thenReturn(status); when(watch.status()).thenReturn(status);
when(watch.name()).thenReturn("_watch"); when(watch.id()).thenReturn("_watch");
when(watch.acked()).thenReturn(true); when(watch.acked()).thenReturn(true);
AckThrottler throttler = new AckThrottler(); AckThrottler throttler = new AckThrottler();
Throttler.Result result = throttler.throttle(ctx); Throttler.Result result = throttler.throttle(ctx);

View File

@ -62,12 +62,12 @@ public class ScheduleTriggerEngineMock extends ScheduleTriggerEngine {
@Override @Override
public void add(Job job) { public void add(Job job) {
jobs.put(job.name(), job); jobs.put(job.id(), job);
} }
@Override @Override
public boolean remove(String jobName) { public boolean remove(String jobId) {
return jobs.remove(jobName) != null; return jobs.remove(jobId) != null;
} }
public void trigger(String jobName) { public void trigger(String jobName) {

View File

@ -204,7 +204,7 @@ public abstract class BaseTriggerEngineTests extends ElasticsearchTestCase {
} }
@Override @Override
public String name() { public String id() {
return name; return name;
} }

View File

@ -356,7 +356,7 @@ public class WatchTests extends ElasticsearchTestCase {
} }
@Override @Override
public boolean remove(String jobName) { public boolean remove(String jobId) {
return false; return false;
} }
} }