@@ -9,13 +9,14 @@ import { filter } from 'rxjs/operators';
99import { performance } from 'perf_hooks' ;
1010
1111import { pipe } from 'fp-ts/lib/pipeable' ;
12- import { Option , some , map as mapOptional } from 'fp-ts/lib/Option' ;
12+ import { Option , some , map as mapOptional , getOrElse } from 'fp-ts/lib/Option' ;
13+
1314import {
1415 SavedObjectsSerializer ,
1516 ILegacyScopedClusterClient ,
1617 ISavedObjectsRepository ,
1718} from '../../../../src/core/server' ;
18- import { Result , asErr , either , map , mapErr , promiseResult } from './lib/result_type' ;
19+ import { Result , asOk , asErr , either , map , mapErr , promiseResult } from './lib/result_type' ;
1920import { TaskManagerConfig } from './config' ;
2021
2122import { Logger } from './types' ;
@@ -405,7 +406,9 @@ export async function claimAvailableTasks(
405406
406407 if ( docs . length !== claimedTasks ) {
407408 logger . warn (
408- `[Task Ownership error]: (${ claimedTasks } ) tasks were claimed by Kibana, but (${ docs . length } ) tasks were fetched`
409+ `[Task Ownership error]: ${ claimedTasks } tasks were claimed by Kibana, but ${
410+ docs . length
411+ } task(s) were fetched (${ docs . map ( ( doc ) => doc . id ) . join ( ', ' ) } )`
409412 ) ;
410413 }
411414 return docs ;
@@ -437,48 +440,65 @@ export async function awaitTaskRunResult(
437440 // listen for all events related to the current task
438441 . pipe ( filter ( ( { id } : TaskLifecycleEvent ) => id === taskId ) )
439442 . subscribe ( ( taskEvent : TaskLifecycleEvent ) => {
440- either (
441- taskEvent . event ,
442- ( taskInstance : ConcreteTaskInstance ) => {
443- // resolve if the task has run sucessfully
444- if ( isTaskRunEvent ( taskEvent ) ) {
445- subscription . unsubscribe ( ) ;
446- resolve ( { id : taskInstance . id } ) ;
447- }
448- } ,
449- async ( error : Error ) => {
443+ if ( isTaskClaimEvent ( taskEvent ) ) {
444+ mapErr ( async ( error : Option < ConcreteTaskInstance > ) => {
450445 // reject if any error event takes place for the requested task
451446 subscription . unsubscribe ( ) ;
452- if ( isTaskRunRequestEvent ( taskEvent ) ) {
453- return reject (
454- new Error (
455- `Failed to run task "${ taskId } " as Task Manager is at capacity, please try again later`
456- )
457- ) ;
458- } else if ( isTaskClaimEvent ( taskEvent ) ) {
459- reject (
460- map (
461- // if the error happened in the Claim phase - we try to provide better insight
462- // into why we failed to claim by getting the task's current lifecycle status
463- await promiseResult < TaskLifecycle , Error > ( getLifecycle ( taskId ) ) ,
464- ( taskLifecycleStatus : TaskLifecycle ) => {
465- if ( taskLifecycleStatus === TaskLifecycleResult . NotFound ) {
466- return new Error ( `Failed to run task "${ taskId } " as it does not exist` ) ;
467- } else if (
468- taskLifecycleStatus === TaskStatus . Running ||
469- taskLifecycleStatus === TaskStatus . Claiming
470- ) {
471- return new Error ( `Failed to run task "${ taskId } " as it is currently running` ) ;
472- }
473- return error ;
474- } ,
475- ( ) => error
476- )
477- ) ;
447+ return reject (
448+ map (
449+ await pipe (
450+ error ,
451+ mapOptional ( async ( taskReturnedBySweep ) => asOk ( taskReturnedBySweep . status ) ) ,
452+ getOrElse ( ( ) =>
453+ // if the error happened in the Claim phase - we try to provide better insight
454+ // into why we failed to claim by getting the task's current lifecycle status
455+ promiseResult < TaskLifecycle , Error > ( getLifecycle ( taskId ) )
456+ )
457+ ) ,
458+ ( taskLifecycleStatus : TaskLifecycle ) => {
459+ if ( taskLifecycleStatus === TaskLifecycleResult . NotFound ) {
460+ return new Error ( `Failed to run task "${ taskId } " as it does not exist` ) ;
461+ } else if (
462+ taskLifecycleStatus === TaskStatus . Running ||
463+ taskLifecycleStatus === TaskStatus . Claiming
464+ ) {
465+ return new Error ( `Failed to run task "${ taskId } " as it is currently running` ) ;
466+ }
467+ return new Error (
468+ `Failed to run task "${ taskId } " for unknown reason (Current Task Lifecycle is "${ taskLifecycleStatus } ")`
469+ ) ;
470+ } ,
471+ ( getLifecycleError : Error ) =>
472+ new Error (
473+ `Failed to run task "${ taskId } " and failed to get current Status:${ getLifecycleError } `
474+ )
475+ )
476+ ) ;
477+ } , taskEvent . event ) ;
478+ } else {
479+ either < ConcreteTaskInstance , Error | Option < ConcreteTaskInstance > > (
480+ taskEvent . event ,
481+ ( taskInstance : ConcreteTaskInstance ) => {
482+ // resolve if the task has run sucessfully
483+ if ( isTaskRunEvent ( taskEvent ) ) {
484+ subscription . unsubscribe ( ) ;
485+ resolve ( { id : taskInstance . id } ) ;
486+ }
487+ } ,
488+ async ( error : Error | Option < ConcreteTaskInstance > ) => {
489+ // reject if any error event takes place for the requested task
490+ subscription . unsubscribe ( ) ;
491+ if ( isTaskRunRequestEvent ( taskEvent ) ) {
492+ return reject (
493+ new Error (
494+ `Failed to run task "${ taskId } " as Task Manager is at capacity, please try again later`
495+ )
496+ ) ;
497+ }
498+ return reject ( new Error ( `Failed to run task "${ taskId } ": ${ error } ` ) ) ;
478499 }
479- return reject ( error ) ;
480- }
481- ) ;
500+ ) ;
501+ }
482502 } ) ;
483503 } ) ;
484504}
0 commit comments