|
25 | 25 | import org.elasticsearch.transport.TransportService;
|
26 | 26 | import org.elasticsearch.xpack.core.XPackField;
|
27 | 27 | import org.elasticsearch.xpack.core.watcher.execution.ActionExecutionMode;
|
| 28 | +import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext; |
28 | 29 | import org.elasticsearch.xpack.core.watcher.history.WatchRecord;
|
29 | 30 | import org.elasticsearch.xpack.core.watcher.support.xcontent.WatcherParams;
|
30 | 31 | import org.elasticsearch.xpack.core.watcher.transport.actions.execute.ExecuteWatchAction;
|
@@ -110,48 +111,63 @@ protected void doExecute(ExecuteWatchRequest request, ActionListener<ExecuteWatc
|
110 | 111 | }
|
111 | 112 | }
|
112 | 113 |
|
113 |
| - private void executeWatch(ExecuteWatchRequest request, ActionListener<ExecuteWatchResponse> listener, |
114 |
| - Watch watch, boolean knownWatch) { |
115 |
| - |
116 |
| - threadPool.executor(XPackField.WATCHER).submit(new AbstractRunnable() { |
117 |
| - @Override |
118 |
| - public void onFailure(Exception e) { |
119 |
| - listener.onFailure(e); |
| 114 | + private void executeWatch( |
| 115 | + final ExecuteWatchRequest request, |
| 116 | + final ActionListener<ExecuteWatchResponse> listener, |
| 117 | + final Watch watch, |
| 118 | + final boolean knownWatch) { |
| 119 | + try { |
| 120 | + /* |
| 121 | + * Ensure that the headers from the incoming request are used instead those of the stored watch otherwise the watch would run |
| 122 | + * as the user who stored the watch, but it needs to run as the user who executes this request. |
| 123 | + */ |
| 124 | + final Map<String, String> headers = new HashMap<>(threadPool.getThreadContext().getHeaders()); |
| 125 | + watch.status().setHeaders(headers); |
| 126 | + |
| 127 | + final String triggerType = watch.trigger().type(); |
| 128 | + final TriggerEvent triggerEvent = triggerService.simulateEvent(triggerType, watch.id(), request.getTriggerData()); |
| 129 | + |
| 130 | + final ManualExecutionContext.Builder ctxBuilder = ManualExecutionContext.builder( |
| 131 | + watch, |
| 132 | + knownWatch, |
| 133 | + new ManualTriggerEvent(triggerEvent.jobName(), triggerEvent), executionService.defaultThrottlePeriod()); |
| 134 | + |
| 135 | + final ZonedDateTime executionTime = clock.instant().atZone(ZoneOffset.UTC); |
| 136 | + ctxBuilder.executionTime(executionTime); |
| 137 | + for (final Map.Entry<String, ActionExecutionMode> entry : request.getActionModes().entrySet()) { |
| 138 | + ctxBuilder.actionMode(entry.getKey(), entry.getValue()); |
| 139 | + } |
| 140 | + if (request.getAlternativeInput() != null) { |
| 141 | + ctxBuilder.withInput(new SimpleInput.Result(new Payload.Simple(request.getAlternativeInput()))); |
120 | 142 | }
|
| 143 | + if (request.isIgnoreCondition()) { |
| 144 | + ctxBuilder.withCondition(InternalAlwaysCondition.RESULT_INSTANCE); |
| 145 | + } |
| 146 | + ctxBuilder.recordExecution(request.isRecordExecution()); |
| 147 | + final WatchExecutionContext ctx = ctxBuilder.build(); |
121 | 148 |
|
122 |
| - @Override |
123 |
| - protected void doRun() throws Exception { |
124 |
| - // ensure that the headers from the incoming request are used instead those of the stored watch |
125 |
| - // otherwise the watch would run as the user who stored the watch, but it needs to be run as the user who |
126 |
| - // executes this request |
127 |
| - Map<String, String> headers = new HashMap<>(threadPool.getThreadContext().getHeaders()); |
128 |
| - watch.status().setHeaders(headers); |
| 149 | + // use execute so that the runnable is not wrapped in a RunnableFuture<?> |
| 150 | + threadPool.executor(XPackField.WATCHER).execute(new ExecutionService.WatchExecutionTask(ctx, new AbstractRunnable() { |
129 | 151 |
|
130 |
| - String triggerType = watch.trigger().type(); |
131 |
| - TriggerEvent triggerEvent = triggerService.simulateEvent(triggerType, watch.id(), request.getTriggerData()); |
| 152 | + @Override |
| 153 | + public void onFailure(final Exception e) { |
| 154 | + listener.onFailure(e); |
| 155 | + } |
132 | 156 |
|
133 |
| - ManualExecutionContext.Builder ctxBuilder = ManualExecutionContext.builder(watch, knownWatch, |
134 |
| - new ManualTriggerEvent(triggerEvent.jobName(), triggerEvent), executionService.defaultThrottlePeriod()); |
| 157 | + @Override |
| 158 | + protected void doRun() throws Exception { |
| 159 | + final WatchRecord record = executionService.execute(ctx); |
| 160 | + final XContentBuilder builder = XContentFactory.jsonBuilder(); |
135 | 161 |
|
136 |
| - ZonedDateTime executionTime = clock.instant().atZone(ZoneOffset.UTC); |
137 |
| - ctxBuilder.executionTime(executionTime); |
138 |
| - for (Map.Entry<String, ActionExecutionMode> entry : request.getActionModes().entrySet()) { |
139 |
| - ctxBuilder.actionMode(entry.getKey(), entry.getValue()); |
| 162 | + record.toXContent(builder, WatcherParams.builder().hideSecrets(true).debug(request.isDebug()).build()); |
| 163 | + listener.onResponse(new ExecuteWatchResponse(record.id().value(), BytesReference.bytes(builder), XContentType.JSON)); |
140 | 164 | }
|
141 |
| - if (request.getAlternativeInput() != null) { |
142 |
| - ctxBuilder.withInput(new SimpleInput.Result(new Payload.Simple(request.getAlternativeInput()))); |
143 |
| - } |
144 |
| - if (request.isIgnoreCondition()) { |
145 |
| - ctxBuilder.withCondition(InternalAlwaysCondition.RESULT_INSTANCE); |
146 |
| - } |
147 |
| - ctxBuilder.recordExecution(request.isRecordExecution()); |
148 | 165 |
|
149 |
| - WatchRecord record = executionService.execute(ctxBuilder.build()); |
150 |
| - XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 166 | + })); |
| 167 | + } catch (final Exception e) { |
| 168 | + listener.onFailure(e); |
| 169 | + } |
| 170 | + |
151 | 171 |
|
152 |
| - record.toXContent(builder, WatcherParams.builder().hideSecrets(true).debug(request.isDebug()).build()); |
153 |
| - listener.onResponse(new ExecuteWatchResponse(record.id().value(), BytesReference.bytes(builder), XContentType.JSON)); |
154 |
| - } |
155 |
| - }); |
156 | 172 | }
|
157 | 173 | }
|
0 commit comments