Skip to content

Commit e458551

Browse files
Mpdreamzrusscam
authored andcommitted
Add max_iterations configuration to watcher action with foreach execu… (#4139)
addresses elastic/elasticsearch#45715 (cherry picked from commit 0348bf1)
1 parent e569a17 commit e458551

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/Nest/XPack/Watcher/Action/ActionBase.cs

+22-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public interface IAction
4242
[IgnoreDataMember]
4343
string Foreach { get; set; }
4444

45+
[IgnoreDataMember]
46+
/// <summary>The maximum number of iterations that each watch executes. If this limit is reached,
47+
/// the execution is gracefully stopped. Defaults to <c>100</c>.
48+
/// </summary>
49+
int? MaxIterations { get; set; }
50+
4551
/// <summary>
4652
/// Transforms the payload before executing the action. The transformation is only applied
4753
/// for the payload for this action.
@@ -75,6 +81,9 @@ internal ActionBase() { }
7581
/// <inheritdoc />
7682
public string Foreach { get; set; }
7783

84+
/// <inheritdoc />
85+
public int? MaxIterations { get; set; }
86+
7887
/// <inheritdoc />
7988
public TransformContainer Transform { get; set; }
8089

@@ -125,7 +134,8 @@ internal class ActionsFormatter : IJsonFormatter<Actions>
125134
{ "pagerduty", 6 },
126135
{ "foreach", 7 },
127136
{ "transform", 8 },
128-
{ "condition", 9 }
137+
{ "condition", 9 },
138+
{ "max_iterations", 10 },
129139
};
130140

131141
public Actions Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
@@ -140,6 +150,7 @@ public Actions Deserialize(ref JsonReader reader, IJsonFormatterResolver formatt
140150
Time throttlePeriod = null;
141151
IAction action = null;
142152
string @foreach = null;
153+
int? maxIterations = null;
143154
TransformContainer transform = null;
144155
ConditionContainer condition = null;
145156

@@ -190,6 +201,9 @@ public Actions Deserialize(ref JsonReader reader, IJsonFormatterResolver formatt
190201
condition = formatterResolver.GetFormatter<ConditionContainer>()
191202
.Deserialize(ref reader, formatterResolver);
192203
break;
204+
case 10:
205+
maxIterations = reader.ReadInt32();
206+
break;
193207
}
194208
}
195209
else
@@ -201,6 +215,7 @@ public Actions Deserialize(ref JsonReader reader, IJsonFormatterResolver formatt
201215
action.Name = name;
202216
action.ThrottlePeriod = throttlePeriod;
203217
action.Foreach = @foreach;
218+
action.MaxIterations = maxIterations;
204219
action.Transform = transform;
205220
action.Condition = condition;
206221
dictionary.Add(name, action);
@@ -238,6 +253,12 @@ public void Serialize(ref JsonWriter writer, Actions value, IJsonFormatterResolv
238253
writer.WriteString(action.Foreach);
239254
writer.WriteValueSeparator();
240255
}
256+
if (action.MaxIterations.HasValue)
257+
{
258+
writer.WritePropertyName("max_iterations");
259+
writer.WriteInt32(action.MaxIterations.Value);
260+
writer.WriteValueSeparator();
261+
}
241262

242263
if (action.Transform != null)
243264
{

src/Nest/XPack/Watcher/Action/ActionsDescriptorBase.cs

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ string IAction.Name
3232
TransformContainer IAction.Transform { get; set; }
3333
ConditionContainer IAction.Condition { get; set; }
3434
string IAction.Foreach { get; set; }
35+
int? IAction.MaxIterations { get; set; }
3536

3637
/// <inheritdoc cref="IAction.Transform"/>
3738
public TDescriptor Transform(Func<TransformDescriptor, TransformContainer> selector) =>
@@ -46,5 +47,8 @@ public TDescriptor Condition(Func<ConditionDescriptor, ConditionContainer> selec
4647

4748
/// <inheritdoc cref="IAction.Foreach"/>
4849
public TDescriptor Foreach(string @foreach) => Assign(@foreach, (a, v) => a.Foreach = v);
50+
51+
/// <inheritdoc cref="IAction.MaxIterations"/>
52+
public TDescriptor MaxIterations(int maxIterations) => Assign(maxIterations, (a,v) => a.MaxIterations = v);
4953
}
5054
}

src/Tests/Tests/XPack/Watcher/PutWatch/PutWatchApiTests.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ protected override void ExpectResponse(PutWatchResponse response)
712712
}
713713
}
714714

715-
[SkipVersion("<7.3.0", "Foreach introduced in 7.3.0")]
715+
[SkipVersion("<7.4.0", "Foreach introduced in 7.3.0, max iterations in 7.4.0")]
716716
public class PutWatchApiWithForeachTests : ApiIntegrationTestBase<XPackCluster, PutWatchResponse, IPutWatchRequest, PutWatchDescriptor, PutWatchRequest>
717717
{
718718
public PutWatchApiWithForeachTests(XPackCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
@@ -757,6 +757,7 @@ public PutWatchApiWithForeachTests(XPackCluster cluster, EndpointUsage usage) :
757757
log_hits = new
758758
{
759759
@foreach = "ctx.payload.hits.hits",
760+
max_iterations = 500,
760761
logging = new
761762
{
762763
text = "Found id {{ctx.payload._id}} with field {{ctx.payload._source.numberOfCommits}}"
@@ -804,6 +805,7 @@ public PutWatchApiWithForeachTests(XPackCluster cluster, EndpointUsage usage) :
804805
.Actions(a => a
805806
.Logging("log_hits", i => i
806807
.Foreach("ctx.payload.hits.hits")
808+
.MaxIterations(500)
807809
.Text("Found id {{ctx.payload._id}} with field {{ctx.payload._source.numberOfCommits}}")
808810
.Transform(t => t
809811
.Script(st =>st
@@ -841,6 +843,7 @@ public PutWatchApiWithForeachTests(XPackCluster cluster, EndpointUsage usage) :
841843
Actions = new LoggingAction("log_hits")
842844
{
843845
Foreach = "ctx.payload.hits.hits",
846+
MaxIterations = 500,
844847
Text = "Found id {{ctx.payload._id}} with field {{ctx.payload._source.numberOfCommits}}",
845848
Transform = new InlineScriptTransform("return [ 'time' : ctx.trigger.scheduled_time ]"),
846849
Condition = new AlwaysCondition()

0 commit comments

Comments
 (0)