16
16
import static com .github .kagkarlsson .scheduler .ExceptionUtils .describe ;
17
17
18
18
import com .github .kagkarlsson .scheduler .logging .ConfigurableLogger ;
19
- import com .github .kagkarlsson .scheduler .stats .StatsRegistry ;
19
+ import com .github .kagkarlsson .scheduler .stats .SchedulerListener ;
20
+ import com .github .kagkarlsson .scheduler .stats .SchedulerListener .CandidateEventType ;
21
+ import com .github .kagkarlsson .scheduler .stats .SchedulerListener .SchedulerEventType ;
20
22
import com .github .kagkarlsson .scheduler .task .CompletionHandler ;
21
23
import com .github .kagkarlsson .scheduler .task .Execution ;
22
24
import com .github .kagkarlsson .scheduler .task .ExecutionComplete ;
@@ -36,7 +38,7 @@ class ExecutePicked implements Runnable {
36
38
private final TaskRepository taskRepository ;
37
39
private SchedulerClientEventListener earlyExecutionListener ;
38
40
private final SchedulerClient schedulerClient ;
39
- private final StatsRegistry statsRegistry ;
41
+ private final SchedulerListener schedulerListener ;
40
42
private final TaskResolver taskResolver ;
41
43
private final SchedulerState schedulerState ;
42
44
private final ConfigurableLogger failureLogger ;
@@ -49,7 +51,7 @@ public ExecutePicked(
49
51
TaskRepository taskRepository ,
50
52
SchedulerClientEventListener earlyExecutionListener ,
51
53
SchedulerClient schedulerClient ,
52
- StatsRegistry statsRegistry ,
54
+ SchedulerListener schedulerListener ,
53
55
TaskResolver taskResolver ,
54
56
SchedulerState schedulerState ,
55
57
ConfigurableLogger failureLogger ,
@@ -60,7 +62,7 @@ public ExecutePicked(
60
62
this .taskRepository = taskRepository ;
61
63
this .earlyExecutionListener = earlyExecutionListener ;
62
64
this .schedulerClient = schedulerClient ;
63
- this .statsRegistry = statsRegistry ;
65
+ this .schedulerListener = schedulerListener ;
64
66
this .taskResolver = taskResolver ;
65
67
this .schedulerState = schedulerState ;
66
68
this .failureLogger = failureLogger ;
@@ -77,7 +79,8 @@ public void run() {
77
79
final UUID executionId = executor .addCurrentlyProcessing (currentlyExecuting );
78
80
79
81
try {
80
- statsRegistry .register (StatsRegistry .CandidateStatsEvent .EXECUTED );
82
+ schedulerListener .onCandidateEvent (CandidateEventType .EXECUTED );
83
+ schedulerListener .onExecutionStart (currentlyExecuting );
81
84
executePickedExecution (pickedExecution , currentlyExecuting );
82
85
} finally {
83
86
executor .removeCurrentlyProcessing (executionId );
@@ -90,7 +93,7 @@ private void executePickedExecution(Execution execution, CurrentlyExecuting curr
90
93
LOG .error (
91
94
"Failed to find implementation for task with name '{}'. Should have been excluded in JdbcRepository." ,
92
95
execution .taskInstance .getTaskName ());
93
- statsRegistry . register ( StatsRegistry . SchedulerStatsEvent .UNEXPECTED_ERROR );
96
+ schedulerListener . onSchedulerEvent ( SchedulerEventType .UNEXPECTED_ERROR );
94
97
return ;
95
98
}
96
99
@@ -106,15 +109,12 @@ private void executePickedExecution(Execution execution, CurrentlyExecuting curr
106
109
LOG .debug ("Execution done: " + execution );
107
110
108
111
complete (completion , execution , executionStarted );
109
- statsRegistry .register (StatsRegistry .ExecutionStatsEvent .COMPLETED );
110
112
111
113
} catch (RuntimeException unhandledException ) {
112
114
failure (task .get (), execution , unhandledException , executionStarted , "Unhandled exception" );
113
- statsRegistry .register (StatsRegistry .ExecutionStatsEvent .FAILED );
114
115
115
116
} catch (Throwable unhandledError ) {
116
117
failure (task .get (), execution , unhandledError , executionStarted , "Error" );
117
- statsRegistry .register (StatsRegistry .ExecutionStatsEvent .FAILED );
118
118
}
119
119
}
120
120
@@ -126,16 +126,17 @@ private void complete(
126
126
completion .complete (
127
127
completeEvent ,
128
128
new ExecutionOperations (taskRepository , earlyExecutionListener , execution ));
129
- statsRegistry .registerSingleCompletedExecution (completeEvent );
130
129
} catch (Throwable e ) {
131
- statsRegistry . register ( StatsRegistry . SchedulerStatsEvent .COMPLETIONHANDLER_ERROR );
132
- statsRegistry . register ( StatsRegistry . SchedulerStatsEvent .UNEXPECTED_ERROR );
130
+ schedulerListener . onSchedulerEvent ( SchedulerEventType .COMPLETIONHANDLER_ERROR );
131
+ schedulerListener . onSchedulerEvent ( SchedulerEventType .UNEXPECTED_ERROR );
133
132
LOG .error (
134
133
"Failed while completing execution {}, because {}. Execution will likely remain scheduled and locked/picked. "
135
134
+ "The execution should be detected as dead after a while, and handled according to the tasks DeadExecutionHandler." ,
136
135
execution ,
137
136
describe (e ),
138
137
e );
138
+ } finally {
139
+ schedulerListener .onExecutionComplete (completeEvent );
139
140
}
140
141
}
141
142
@@ -155,16 +156,17 @@ private void failure(
155
156
.onFailure (
156
157
completeEvent ,
157
158
new ExecutionOperations (taskRepository , earlyExecutionListener , execution ));
158
- statsRegistry .registerSingleCompletedExecution (completeEvent );
159
159
} catch (Throwable e ) {
160
- statsRegistry . register ( StatsRegistry . SchedulerStatsEvent .FAILUREHANDLER_ERROR );
161
- statsRegistry . register ( StatsRegistry . SchedulerStatsEvent .UNEXPECTED_ERROR );
160
+ schedulerListener . onSchedulerEvent ( SchedulerEventType .FAILUREHANDLER_ERROR );
161
+ schedulerListener . onSchedulerEvent ( SchedulerEventType .UNEXPECTED_ERROR );
162
162
LOG .error (
163
163
"Failed while completing execution {}, because {}. Execution will likely remain scheduled and locked/picked. "
164
164
+ "The execution should be detected as dead after a while, and handled according to the tasks DeadExecutionHandler." ,
165
165
execution ,
166
166
describe (cause ),
167
167
e );
168
+ } finally {
169
+ schedulerListener .onExecutionComplete (completeEvent );
168
170
}
169
171
}
170
172
}
0 commit comments