Skip to content

Commit 8791166

Browse files
committed
feat: refactor task runner bindings and timelined progress
1 parent ff5ff31 commit 8791166

File tree

3 files changed

+108
-39
lines changed

3 files changed

+108
-39
lines changed

owlplug-client/src/main/java/com/owlplug/core/components/TaskRunner.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public void submitTaskOnQueueHead(AbstractTask task) {
9494
private synchronized void scheduleNext() {
9595

9696
if (!taskQueue.isEmpty() && currentTask == null) {
97-
disableError();
9897
taskBarController.resetErrorLog();
9998
// Get the next pending task
10099
AbstractTask polledTask = taskQueue.pollFirst();
@@ -112,8 +111,11 @@ public void onSuccess(TaskResult result) {
112111
log.error("Error while running task", currentTask.getException());
113112
taskBarController.setErrorLog(currentTask, currentTask.getException().getMessage(),
114113
currentTask.getException().toString());
114+
} else {
115+
log.error("Task failed without exception !");
116+
taskBarController.setErrorLog(currentTask, "Task failed",
117+
"No error information available. TaskResult=" + result);
115118
}
116-
triggerOnError();
117119
}
118120
removeCurrentTask();
119121
scheduleNext();
@@ -136,22 +138,22 @@ public void onFailure(Throwable ex) {
136138
private void setCurrentTask(AbstractTask task) {
137139
this.currentTask = task;
138140
// Bind progress indicators
139-
taskBarController.taskProgressBar.progressProperty().bind(currentTask.progressProperty());
140-
taskBarController.taskLabel.textProperty().bind(currentTask.messageProperty());
141+
taskBarController.progressProperty().bind(currentTask.progressProperty());
142+
taskBarController.taskNameProperty().bind(currentTask.messageProperty());
141143
}
142144

143145
private void removeCurrentTask() {
144146
// Unbind progress indicators
145-
taskBarController.taskProgressBar.progressProperty().unbind();
146-
taskBarController.taskLabel.textProperty().unbind();
147+
taskBarController.progressProperty().unbind();
148+
taskBarController.taskNameProperty().unbind();
147149

148150
currentTask = null;
149151

150152
}
151153

152154
private void addInTaskHistory(AbstractTask task) {
153155
if (taskHistory.size() >= 10) {
154-
taskHistory.remove(0);
156+
taskHistory.removeFirst();
155157
}
156158
taskHistory.add(task);
157159
}
@@ -165,7 +167,6 @@ public void close() {
165167
}
166168

167169
}
168-
169170

170171
public List<AbstractTask> getPendingTasks() {
171172
return new ArrayList<>(taskQueue);
@@ -175,14 +176,5 @@ public List<AbstractTask> getTaskHistory() {
175176
return new ArrayList<>(taskHistory);
176177
}
177178

178-
private void triggerOnError() {
179-
taskBarController.taskProgressBar.getStyleClass().add("progress-bar-error");
180-
181-
}
182-
183-
public void disableError() {
184-
taskBarController.taskProgressBar.getStyleClass().remove("progress-bar-error");
185-
186-
}
187179

188180
}

owlplug-client/src/main/java/com/owlplug/core/controllers/TaskBarController.java

Lines changed: 98 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
import com.owlplug.core.components.TaskRunner;
2323
import com.owlplug.core.tasks.AbstractTask;
2424
import java.util.ArrayList;
25+
import javafx.animation.Interpolator;
26+
import javafx.animation.KeyFrame;
27+
import javafx.animation.KeyValue;
28+
import javafx.animation.Timeline;
29+
import javafx.application.Platform;
30+
import javafx.beans.property.DoubleProperty;
31+
import javafx.beans.property.SimpleDoubleProperty;
32+
import javafx.beans.property.SimpleStringProperty;
33+
import javafx.beans.property.StringProperty;
2534
import javafx.concurrent.Worker.State;
2635
import javafx.fxml.FXML;
2736
import javafx.scene.control.Button;
@@ -34,6 +43,7 @@
3443
import javafx.scene.image.ImageView;
3544
import javafx.scene.layout.Region;
3645
import javafx.util.Callback;
46+
import javafx.util.Duration;
3747
import org.springframework.beans.factory.annotation.Autowired;
3848
import org.springframework.stereotype.Controller;
3949

@@ -44,14 +54,21 @@ public class TaskBarController extends BaseController {
4454
private TaskRunner taskRunner;
4555

4656
@FXML
47-
public Label taskLabel;
57+
private Label taskLabel;
4858
@FXML
49-
public ProgressBar taskProgressBar;
59+
private ProgressBar taskProgressBar;
5060
@FXML
5161
private Button taskHistoryButton;
5262
@FXML
5363
private Button logsButton;
5464

65+
private final DoubleProperty progressProperty = new SimpleDoubleProperty();
66+
67+
private final StringProperty taskNameProperty = new SimpleStringProperty();
68+
69+
private Timeline progressTimeline;
70+
71+
5572
/**
5673
* FXML initialize.
5774
*/
@@ -60,25 +77,10 @@ public void initialize() {
6077
taskHistoryButton.setOnAction(e -> openTaskHistory());
6178
resetErrorLog();
6279

63-
}
64-
65-
public void setErrorLog(AbstractTask task, String title, String content) {
66-
67-
this.getTelemetryService().event("/Error/TaskExecution", p -> {
68-
p.put("taskName", task.getName());
69-
p.put("error", title);
70-
p.put("content", content);
71-
});
72-
logsButton.setVisible(true);
73-
logsButton.setManaged(true);
74-
logsButton.setOnAction(e -> {
75-
showErrorDialog(title, content);
80+
progressProperty.addListener((obs, oldVal, newVal) -> {
81+
updateProgress(newVal.doubleValue());
7682
});
77-
}
78-
79-
public void resetErrorLog() {
80-
logsButton.setManaged(false);
81-
logsButton.setVisible(false);
83+
taskLabel.textProperty().bind(taskNameProperty);
8284
}
8385

8486
private void openTaskHistory() {
@@ -128,8 +130,83 @@ public void updateItem(AbstractTask item, boolean empty) {
128130

129131
private void showErrorDialog(String title, String content) {
130132
this.getDialogManager().newSimpleInfoDialog(
131-
new Label(title), new TextArea(content)
133+
new Label(title), new TextArea(content)
132134
).show();
133135
}
134136

137+
public void setErrorLog(AbstractTask task, String title, String content) {
138+
139+
this.getTelemetryService().event("/Error/TaskExecution", p -> {
140+
p.put("taskName", task.getName());
141+
p.put("error", title);
142+
p.put("content", content);
143+
});
144+
taskProgressBar.getStyleClass().add("progress-bar-error");
145+
logsButton.setVisible(true);
146+
logsButton.setManaged(true);
147+
logsButton.setOnAction(e -> {
148+
showErrorDialog(title, content);
149+
});
150+
}
151+
152+
public void resetErrorLog() {
153+
taskProgressBar.getStyleClass().remove("progress-bar-error");
154+
logsButton.setManaged(false);
155+
logsButton.setVisible(false);
156+
}
157+
158+
159+
public StringProperty taskNameProperty() {
160+
return taskNameProperty;
161+
}
162+
163+
public DoubleProperty progressProperty() {
164+
return progressProperty;
165+
}
166+
167+
private void updateProgress(double target) {
168+
double current = taskProgressBar.getProgress();
169+
if (Double.isNaN(current)) {
170+
current = 0.0;
171+
}
172+
// Non-running task can report negative progress to indicate indeterminate state
173+
// Clamp to 0.0 for display animation purposes
174+
if (target < 0.0) {
175+
target = 0.0;
176+
}
177+
178+
// Apply immediately if decreasing or equa
179+
if (target <= current) {
180+
if (progressTimeline != null) {
181+
progressTimeline.stop();
182+
progressTimeline = null;
183+
}
184+
taskProgressBar.setProgress(target);
185+
return;
186+
}
187+
188+
// If a timeline is already running, stop it
189+
if (progressTimeline != null) {
190+
progressTimeline.stop();
191+
}
192+
193+
// | Delta | Step-by-step | millis result |
194+
// | :---- | :------------------------------ | :----------------------- |
195+
// | 0.5 | `delta/0.5 = 1 → (1−1)=0` | `1000 + 0×4000 = 1000` |
196+
// | 0.25 | `delta/0.5 = 0.5 → (1−0.5)=0.5` | `1000 + 0.5×4000 = 3000` |
197+
// | 0.1 | `delta/0.5 = 0.2 → (1−0.2)=0.8` | `1000 + 0.8×4000 = 4200` |
198+
// | 0.05 | `delta/0.5 = 0.1 → (1−0.1)=0.9` | `1000 + 0.9×4000 = 4600` |
199+
// | 0.0 | (edge) | `1000 + 1×4000 = 5000` |
200+
// Increasing progress animation duration from 1s to 5s depending on delta
201+
// Small increments takes longer to reach target
202+
double delta = target - current;
203+
double millis = 1000 + (1 - Math.min(1, delta / 0.5)) * 4000;
204+
KeyValue kv = new KeyValue(taskProgressBar.progressProperty(), target, Interpolator.EASE_BOTH);
205+
KeyFrame kf = new KeyFrame(Duration.millis(millis), kv);
206+
207+
progressTimeline = new Timeline(kf);
208+
progressTimeline.setOnFinished(ev -> progressTimeline = null);
209+
progressTimeline.play();
210+
211+
}
135212
}

owlplug-client/src/main/java/com/owlplug/plugin/tasks/PluginScanTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ protected void collect() throws Exception {
9898

9999
log.info("Plugin Scan task started");
100100
this.updateMessage("Collecting plugins...");
101-
this.commitProgress(10);
101+
this.commitProgress(20);
102102

103103
// Clear data from previous scan if not incremental
104104
if (!parameters.isDifferential()) {

0 commit comments

Comments
 (0)