This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Enhance WAIT task to support time-based wait scenarios #2986
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a249563
Update Wait.java
v1r3n cf40979
Add support for waiting for time duration or until a specific date/time
v1r3n 7586801
Merge branch 'Netflix:main' into wait_improvements
v1r3n e1bbbc4
add unit tests and SDK methods
v1r3n 9a11791
formatting
v1r3n 8c8fe55
Remove Guava dependency
v1r3n 5cec41b
Fix formatting
v1r3n f1c2ecd
Wait task updates
v1r3n ae8b874
remove callbackafter
v1r3n 2876f3d
add the break
v1r3n be6194c
Fix readme
v1r3n 03ec26f
Move the date parsing to a util class
v1r3n f9679d9
move DateTimeUtils to the common utils package
v1r3n 739c615
Merge branch 'main' into wait_improvements
v1r3n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
core/src/main/java/com/netflix/conductor/core/utils/DateTimeUtils.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2022 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.netflix.conductor.core.utils; | ||
|
||
import java.text.ParseException; | ||
import java.time.Duration; | ||
import java.util.Date; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.commons.lang3.time.DateUtils; | ||
|
||
public class DateTimeUtils { | ||
|
||
private static final String[] patterns = | ||
new String[] {"yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm z", "yyyy-MM-dd"}; | ||
|
||
public static Duration parseDuration(String text) { | ||
Matcher m = | ||
Pattern.compile( | ||
"\\s*(?:(\\d+)\\s*(?:days?|d))?" | ||
+ "\\s*(?:(\\d+)\\s*(?:hours?|hrs?|h))?" | ||
+ "\\s*(?:(\\d+)\\s*(?:minutes?|mins?|m))?" | ||
+ "\\s*(?:(\\d+)\\s*(?:seconds?|secs?|s))?" | ||
+ "\\s*", | ||
Pattern.CASE_INSENSITIVE) | ||
.matcher(text); | ||
if (!m.matches()) throw new IllegalArgumentException("Not valid duration: " + text); | ||
|
||
int days = (m.start(1) == -1 ? 0 : Integer.parseInt(m.group(1))); | ||
int hours = (m.start(2) == -1 ? 0 : Integer.parseInt(m.group(2))); | ||
int mins = (m.start(3) == -1 ? 0 : Integer.parseInt(m.group(3))); | ||
int secs = (m.start(4) == -1 ? 0 : Integer.parseInt(m.group(4))); | ||
return Duration.ofSeconds((days * 86400) + (hours * 60L + mins) * 60L + secs); | ||
} | ||
|
||
public static Date parseDate(String date) throws ParseException { | ||
return DateUtils.parseDate(date, patterns); | ||
} | ||
} |
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
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
109 changes: 109 additions & 0 deletions
109
core/src/test/java/com/netflix/conductor/core/execution/tasks/TestWait.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2022 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package com.netflix.conductor.core.execution.tasks; | ||
|
||
import java.text.ParseException; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Date; | ||
|
||
import org.apache.commons.lang3.time.DateUtils; | ||
import org.junit.Test; | ||
|
||
import com.netflix.conductor.model.TaskModel; | ||
import com.netflix.conductor.model.WorkflowModel; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class TestWait { | ||
|
||
private final Wait wait = new Wait(); | ||
|
||
@Test | ||
public void testWaitForever() { | ||
|
||
TaskModel task = new TaskModel(); | ||
task.setStatus(TaskModel.Status.SCHEDULED); | ||
WorkflowModel model = new WorkflowModel(); | ||
|
||
wait.start(model, task, null); | ||
assertEquals(TaskModel.Status.IN_PROGRESS, task.getStatus()); | ||
assertTrue(task.getOutputData().isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testWaitUntil() throws ParseException { | ||
String dateFormat = "yyyy-MM-dd HH:mm"; | ||
|
||
WorkflowModel model = new WorkflowModel(); | ||
|
||
TaskModel task = new TaskModel(); | ||
task.setStatus(TaskModel.Status.SCHEDULED); | ||
|
||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat); | ||
LocalDateTime now = LocalDateTime.now(); | ||
String formatted = formatter.format(now); | ||
System.out.println(formatted); | ||
|
||
task.getInputData().put(Wait.UNTIL_INPUT, formatted); | ||
Date parsed = DateUtils.parseDate(formatted, dateFormat); | ||
|
||
wait.start(model, task, null); | ||
assertEquals(TaskModel.Status.IN_PROGRESS, task.getStatus()); | ||
assertEquals(parsed.getTime(), task.getWaitTimeout()); | ||
|
||
// Execute runs when checking if the task has completed | ||
boolean updated = wait.execute(model, task, null); | ||
assertTrue(updated); | ||
assertEquals(TaskModel.Status.COMPLETED, task.getStatus()); | ||
} | ||
|
||
@Test | ||
public void testWaitDuration() throws ParseException { | ||
WorkflowModel model = new WorkflowModel(); | ||
|
||
TaskModel task = new TaskModel(); | ||
task.setStatus(TaskModel.Status.SCHEDULED); | ||
|
||
task.getInputData().put(Wait.DURATION_INPUT, "1s"); | ||
wait.start(model, task, null); | ||
long now = System.currentTimeMillis(); | ||
|
||
assertEquals(TaskModel.Status.IN_PROGRESS, task.getStatus()); | ||
assertEquals(now + 1000, task.getWaitTimeout()); | ||
|
||
try { | ||
Thread.sleep(2_000); | ||
} catch (InterruptedException e) { | ||
} | ||
|
||
// Execute runs when checking if the task has completed | ||
boolean updated = wait.execute(model, task, null); | ||
assertTrue(updated); | ||
assertEquals(TaskModel.Status.COMPLETED, task.getStatus()); | ||
} | ||
|
||
@Test | ||
public void testInvalidWaitConfig() throws ParseException { | ||
WorkflowModel model = new WorkflowModel(); | ||
|
||
TaskModel task = new TaskModel(); | ||
task.setStatus(TaskModel.Status.SCHEDULED); | ||
|
||
task.getInputData().put(Wait.DURATION_INPUT, "1s"); | ||
task.getInputData().put(Wait.UNTIL_INPUT, "2022-12-12"); | ||
wait.start(model, task, null); | ||
assertEquals(TaskModel.Status.FAILED_WITH_TERMINAL_ERROR, task.getStatus()); | ||
assertTrue(!task.getReasonForIncompletion().isEmpty()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
callbackAfterSeconds
is moot sinceWAIT
is a synchronous task and is not added to a queue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated and removed.