@@ -201,78 +201,117 @@ public void registerBatchJobWatcher(Job job, String taskInstanceId, TaskResponse
201201 final String jobName = job .getMetadata ().getName ();
202202 final String namespace = job .getMetadata ().getNamespace ();
203203 final CountDownLatch countDownLatch = new CountDownLatch (1 );
204- SharedIndexInformer <Job > informer = k8sUtils .createBatchJobInformer (jobName , namespace ,
205- new ResourceEventHandler <Job >() {
206-
207- @ Override
208- public void onAdd (Job watchedJob ) {
209- try {
210- LogUtils .setWorkflowAndTaskInstanceIDMDC (taskRequest .getWorkflowInstanceId (),
211- taskRequest .getTaskInstanceId ());
212- LogUtils .setTaskInstanceLogFullPathMDC (taskRequest .getLogPath ());
213- log .info ("event received : job:{} action:ADD" , watchedJob .getMetadata ().getName ());
214- } finally {
215- LogUtils .removeTaskInstanceLogFullPathMDC ();
216- LogUtils .removeWorkflowAndTaskInstanceIdMDC ();
217- }
218- }
219-
220- @ Override
221- public void onUpdate (Job oldJob , Job watchedJob ) {
222- try {
223- LogUtils .setWorkflowAndTaskInstanceIDMDC (taskRequest .getWorkflowInstanceId (),
224- taskRequest .getTaskInstanceId ());
225- LogUtils .setTaskInstanceLogFullPathMDC (taskRequest .getLogPath ());
226- log .info ("event received : job:{} action:UPDATE" , watchedJob .getMetadata ().getName ());
227- int jobStatus = getK8sJobStatus (watchedJob );
228- log .info ("job {} status {}" , watchedJob .getMetadata ().getName (), jobStatus );
229- if (jobStatus == TaskConstants .RUNNING_CODE ) {
230- return ;
231- }
232- setTaskStatus (jobStatus , taskInstanceId , taskResponse );
233- countDownLatch .countDown ();
234- } finally {
235- LogUtils .removeTaskInstanceLogFullPathMDC ();
236- LogUtils .removeWorkflowAndTaskInstanceIdMDC ();
237- }
238- }
239-
240- @ Override
241- public void onDelete (Job watchedJob , boolean deletedFinalStateUnknown ) {
242- try {
243- LogUtils .setWorkflowAndTaskInstanceIDMDC (taskRequest .getWorkflowInstanceId (),
244- taskRequest .getTaskInstanceId ());
245- LogUtils .setTaskInstanceLogFullPathMDC (taskRequest .getLogPath ());
246- log .error ("[K8sJobExecutor-{}] fail in k8s" , watchedJob .getMetadata ().getName ());
247- taskResponse .setExitStatusCode (EXIT_CODE_FAILURE );
248- countDownLatch .countDown ();
249- } finally {
250- LogUtils .removeTaskInstanceLogFullPathMDC ();
251- LogUtils .removeWorkflowAndTaskInstanceIdMDC ();
252- }
253- }
254- });
204+ SharedIndexInformer <Job > informer = null ;
255205 try {
256- boolean timeoutFlag = taskRequest . getTaskTimeoutStrategy () == TaskTimeoutStrategy . FAILED
257- || taskRequest . getTaskTimeoutStrategy () == TaskTimeoutStrategy . WARNFAILED ;
258- if ( timeoutFlag ) {
259- Boolean timeout = !( countDownLatch .await ( taskRequest . getTaskTimeout (), TimeUnit . SECONDS ));
260- waitTimeout ( timeout );
261- } else {
262- countDownLatch . await ( );
263- }
206+ informer = k8sUtils . createBatchJobInformer ( jobName , namespace ,
207+ createBatchJobEventHandler ( taskInstanceId , taskResponse , countDownLatch )) ;
208+ informer . stopped (). whenComplete (( v , ex ) -> {
209+ if ( ex != null && countDownLatch .getCount () > 0 ) {
210+ failInformerAndCountDown ( jobName , ex . getMessage (), ex , taskResponse , countDownLatch );
211+ }
212+ } );
213+ awaitJobCompletion ( countDownLatch );
264214 } catch (InterruptedException e ) {
265- log .error ("job failed in k8s: {}" , e .getMessage (), e );
266215 Thread .currentThread ().interrupt ();
267- taskResponse .setExitStatusCode (EXIT_CODE_FAILURE );
216+ withTaskLogContext (() -> {
217+ log .error ("job failed in k8s: {}" , e .getMessage (), e );
218+ taskResponse .setExitStatusCode (EXIT_CODE_FAILURE );
219+ countDownLatch .countDown ();
220+ });
268221 } catch (Exception e ) {
269- log .error ("job failed in k8s: {}" , e .getMessage (), e );
270- taskResponse .setExitStatusCode (EXIT_CODE_FAILURE );
222+ if (countDownLatch .getCount () > 0 ) {
223+ withTaskLogContext (() -> {
224+ log .error ("job failed in k8s: {}" , e .getMessage (), e );
225+ taskResponse .setExitStatusCode (EXIT_CODE_FAILURE );
226+ countDownLatch .countDown ();
227+ });
228+ }
271229 } finally {
272- informer .stop ();
230+ if (informer != null ) {
231+ informer .stop ();
232+ }
273233 }
274234 }
275235
236+ private ResourceEventHandler <Job > createBatchJobEventHandler (String taskInstanceId , TaskResponse taskResponse ,
237+ CountDownLatch countDownLatch ) {
238+ return new ResourceEventHandler <Job >() {
239+
240+ @ Override
241+ public void onAdd (Job watchedJob ) {
242+ withTaskLogContext (() -> {
243+ log .info ("event received, job: {}, action: ADD" , watchedJob .getMetadata ().getName ());
244+ handleBatchJobTerminalStatus (watchedJob , taskInstanceId , taskResponse , countDownLatch );
245+ });
246+ }
247+
248+ @ Override
249+ public void onUpdate (Job oldJob , Job watchedJob ) {
250+ withTaskLogContext (() -> {
251+ log .info ("event received, job: {}, action: UPDATE" , watchedJob .getMetadata ().getName ());
252+ handleBatchJobTerminalStatus (watchedJob , taskInstanceId , taskResponse , countDownLatch );
253+ });
254+ }
255+
256+ @ Override
257+ public void onDelete (Job watchedJob , boolean deletedFinalStateUnknown ) {
258+ withTaskLogContext (() -> {
259+ log .info ("event received, job: {}, action: DELETE" , watchedJob .getMetadata ().getName ());
260+ log .error ("[K8sJobExecutor-{}] fail in k8s" , watchedJob .getMetadata ().getName ());
261+ taskResponse .setExitStatusCode (EXIT_CODE_FAILURE );
262+ countDownLatch .countDown ();
263+ });
264+ }
265+ };
266+ }
267+
268+ private void awaitJobCompletion (CountDownLatch countDownLatch ) throws InterruptedException {
269+ boolean timeoutFlag = taskRequest .getTaskTimeoutStrategy () == TaskTimeoutStrategy .FAILED
270+ || taskRequest .getTaskTimeoutStrategy () == TaskTimeoutStrategy .WARNFAILED ;
271+ if (timeoutFlag ) {
272+ if (!countDownLatch .await (taskRequest .getTaskTimeout (), TimeUnit .SECONDS )) {
273+ waitTimeout (true );
274+ }
275+ } else {
276+ countDownLatch .await ();
277+ }
278+ }
279+
280+ private void withTaskLogContext (Runnable action ) {
281+ try {
282+ LogUtils .setWorkflowAndTaskInstanceIDMDC (taskRequest .getWorkflowInstanceId (),
283+ taskRequest .getTaskInstanceId ());
284+ LogUtils .setTaskInstanceLogFullPathMDC (taskRequest .getLogPath ());
285+ action .run ();
286+ } finally {
287+ LogUtils .removeTaskInstanceLogFullPathMDC ();
288+ LogUtils .removeWorkflowAndTaskInstanceIdMDC ();
289+ }
290+ }
291+
292+ private void handleBatchJobTerminalStatus (Job watchedJob , String taskInstanceId , TaskResponse taskResponse ,
293+ CountDownLatch countDownLatch ) {
294+ int jobStatus = getK8sJobStatus (watchedJob );
295+ log .info ("job {} status {}" , watchedJob .getMetadata ().getName (), jobStatus );
296+ if (jobStatus == TaskConstants .RUNNING_CODE ) {
297+ return ;
298+ }
299+ setTaskStatus (jobStatus , taskInstanceId , taskResponse );
300+ countDownLatch .countDown ();
301+ }
302+
303+ private void failInformerAndCountDown (String jobName , String message , Throwable ex , TaskResponse taskResponse ,
304+ CountDownLatch countDownLatch ) {
305+ if (countDownLatch .getCount () == 0 ) {
306+ return ;
307+ }
308+ withTaskLogContext (() -> {
309+ log .error ("[K8sJobExecutor-{}] fail in k8s: {}" , jobName , message , ex );
310+ taskResponse .setExitStatusCode (EXIT_CODE_FAILURE );
311+ countDownLatch .countDown ();
312+ });
313+ }
314+
276315 private void parsePodLogOutput () {
277316 ExecutorService collectPodLogExecutorService = ThreadUtils
278317 .newSingleDaemonScheduledExecutorService ("CollectPodLogOutput-thread-" + taskRequest .getTaskName ());
0 commit comments