-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add execution metadata to tasks and scheduled tasks
This commit adds new information about the execution and scheduling of tasks. The `Task` type now exposes the `TaskExecutionOutcome` of the latest execution; this includes the instant the execution started, the execution outcome and any thrown exception. The `ScheduledTask` contract can now provide the time when the next execution is scheduled. Closes gh-24560
- Loading branch information
Showing
8 changed files
with
494 additions
and
100 deletions.
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
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
74 changes: 74 additions & 0 deletions
74
spring-context/src/main/java/org/springframework/scheduling/config/TaskExecutionOutcome.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,74 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 org.springframework.scheduling.config; | ||
|
||
import java.time.Instant; | ||
|
||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Outcome of a {@link Task} execution. | ||
* @param executionTime the instant when the task execution started, {@code null} if the task has not started. | ||
* @param status the {@link Status} of the execution outcome. | ||
* @param throwable the exception thrown from the task execution, if any. | ||
* @author Brian Clozel | ||
* @since 6.2 | ||
*/ | ||
public record TaskExecutionOutcome(@Nullable Instant executionTime, Status status, @Nullable Throwable throwable) { | ||
|
||
TaskExecutionOutcome start(Instant executionTime) { | ||
return new TaskExecutionOutcome(executionTime, Status.STARTED, null); | ||
} | ||
|
||
TaskExecutionOutcome success() { | ||
Assert.state(this.executionTime != null, "Task has not been started yet"); | ||
return new TaskExecutionOutcome(this.executionTime, Status.SUCCESS, null); | ||
} | ||
|
||
TaskExecutionOutcome failure(Throwable throwable) { | ||
Assert.state(this.executionTime != null, "Task has not been started yet"); | ||
return new TaskExecutionOutcome(this.executionTime, Status.ERROR, throwable); | ||
} | ||
|
||
static TaskExecutionOutcome create() { | ||
return new TaskExecutionOutcome(null, Status.NONE, null); | ||
} | ||
|
||
|
||
/** | ||
* Status of the task execution outcome. | ||
*/ | ||
public enum Status { | ||
/** | ||
* The task has not been executed so far. | ||
*/ | ||
NONE, | ||
/** | ||
* The task execution has been started and is ongoing. | ||
*/ | ||
STARTED, | ||
/** | ||
* The task execution finished successfully. | ||
*/ | ||
SUCCESS, | ||
/** | ||
* The task execution finished with an error. | ||
*/ | ||
ERROR | ||
} | ||
} |
Oops, something went wrong.