File tree 2 files changed +28
-8
lines changed
2 files changed +28
-8
lines changed Original file line number Diff line number Diff line change @@ -86,13 +86,16 @@ export class PTask<T, R> {
86
86
}
87
87
88
88
public async run ( ) : Promise < R > {
89
- const newRes = await ProcessingPriorityQueue . getInstance (
90
- this . queueName
91
- ) . enqueue ( this ) ;
92
- const result = this . resultsMerge ( this . resSoFar , newRes ) ;
93
- this . removeSelfFromQueue ( ) ;
94
- this . _status = "completed" ;
95
- return result ;
89
+ try {
90
+ const newRes = await ProcessingPriorityQueue . getInstance (
91
+ this . queueName
92
+ ) . enqueue ( this ) ;
93
+
94
+ return this . resultsMerge ( this . resSoFar , newRes ) ;
95
+ } finally {
96
+ this . removeSelfFromQueue ( ) ;
97
+ this . _status = "completed" ;
98
+ }
96
99
}
97
100
98
101
public async pause ( ) : Promise < void > {
Original file line number Diff line number Diff line change @@ -670,7 +670,7 @@ describe("PriorityTask", () => {
670
670
} ) ;
671
671
} ) ;
672
672
673
- it ( "should be removed from the queue when complete" , ( done ) => {
673
+ it ( "should be removed from the queue when complete successfully " , ( done ) => {
674
674
const ptask = new PTask < void , void > ( {
675
675
args : undefined ,
676
676
priority : 1 ,
@@ -685,6 +685,23 @@ describe("PriorityTask", () => {
685
685
} ) ;
686
686
expect ( PTask . getAllPTasks ( "krombopulos" ) . length ) . toBe ( 1 ) ;
687
687
} ) ;
688
+
689
+ it ( 'should be removed from the queue when complete unsuccessfully' , ( done ) => {
690
+ const ptask = new PTask < void , void > ( {
691
+ args : undefined ,
692
+ priority : 1 ,
693
+ onRun : async ( ) =>
694
+ await new Promise ( ( resolve , reject ) => setTimeout ( reject , 500 ) ) ,
695
+ queueName : "krombopulos-2" ,
696
+ } ) ;
697
+
698
+ ptask . run ( )
699
+ . catch ( ( ) => {
700
+ expect ( PTask . getAllPTasks ( "krombopulos-2" ) . length ) . toBe ( 0 ) ;
701
+ done ( ) ;
702
+ } ) ;
703
+ expect ( PTask . getAllPTasks ( "krombopulos-2" ) . length ) . toBe ( 1 ) ;
704
+ } )
688
705
689
706
it ( "should not schedule more than concurrencyLimit items" , ( done ) => {
690
707
const CONCURRENCY_LIMIT = 2 ;
You can’t perform that action at this time.
0 commit comments