@@ -495,4 +495,211 @@ export default function defineTestSuite() {
495495 expect ( allJobs ) . toHaveLength ( 0 ) ;
496496 } ) ;
497497 } ) ;
498+
499+ describe ( "staleJobs" , ( ) => {
500+ it ( "should not find any stale job" , async ( ) => {
501+ const job : NewJobData = {
502+ queue : "default" ,
503+ class : "TestJob" ,
504+ args : [ { foo : "bar" } ] ,
505+ constructor_args : [ { } ] ,
506+ state : "waiting" ,
507+ script : "test.js" ,
508+ attempt : 0 ,
509+ max_attempts : 5 ,
510+ } ;
511+
512+ let insertedJob = await backend . createNewJob ( job ) ;
513+
514+ insertedJob = await backend . createNewJob ( job ) ;
515+ await backend . updateJob ( { ...insertedJob , state : "canceled" } ) ;
516+
517+ insertedJob = await backend . createNewJob ( job ) ;
518+ await backend . updateJob ( { ...insertedJob , state : "claimed" , claimed_at : new Date ( ) } ) ;
519+
520+ insertedJob = await backend . createNewJob ( job ) ;
521+ await backend . updateJob ( { ...insertedJob , state : "completed" } ) ;
522+
523+ insertedJob = await backend . createNewJob ( job ) ;
524+ await backend . updateJob ( { ...insertedJob , state : "failed" } ) ;
525+
526+ insertedJob = await backend . createNewJob ( job ) ;
527+ await backend . updateJob ( { ...insertedJob , state : "running" , attempted_at : new Date ( ) , timeout : 1000000 } ) ;
528+
529+ insertedJob = await backend . createNewJob ( job ) ;
530+ await backend . updateJob ( { ...insertedJob , state : "running" , attempted_at : new Date ( ) } ) ;
531+
532+ const result = await backend . staleJobs ( ) ;
533+ expect ( result ) . toHaveLength ( 0 ) ;
534+ } ) ;
535+
536+ it ( "should find claimed stale job" , async ( ) => {
537+ const job : NewJobData = {
538+ queue : "default" ,
539+ class : "TestJob" ,
540+ args : [ { foo : "bar" } ] ,
541+ constructor_args : [ { } ] ,
542+ state : "waiting" ,
543+ script : "test.js" ,
544+ attempt : 0 ,
545+ max_attempts : 5 ,
546+ } ;
547+
548+ let insertedJob = await backend . createNewJob ( job ) ;
549+
550+ insertedJob = await backend . createNewJob ( job ) ;
551+ await backend . updateJob ( { ...insertedJob , state : "canceled" , claimed_at : new Date ( 0 ) } ) ;
552+
553+ insertedJob = await backend . createNewJob ( job ) ;
554+ await backend . updateJob ( { ...insertedJob , state : "claimed" , claimed_at : new Date ( 0 ) } ) ;
555+
556+ insertedJob = await backend . createNewJob ( job ) ;
557+ await backend . updateJob ( { ...insertedJob , state : "completed" , claimed_at : new Date ( 0 ) } ) ;
558+
559+ insertedJob = await backend . createNewJob ( job ) ;
560+ await backend . updateJob ( { ...insertedJob , state : "failed" , claimed_at : new Date ( 0 ) } ) ;
561+
562+ insertedJob = await backend . createNewJob ( job ) ;
563+ await backend . updateJob ( {
564+ ...insertedJob ,
565+ state : "running" ,
566+ claimed_at : new Date ( 0 ) ,
567+ attempted_at : new Date ( ) ,
568+ timeout : 1000000 ,
569+ } ) ;
570+
571+ insertedJob = await backend . createNewJob ( job ) ;
572+ await backend . updateJob ( { ...insertedJob , state : "running" , claimed_at : new Date ( 0 ) , attempted_at : new Date ( ) } ) ;
573+
574+ const result = await backend . staleJobs ( ) ;
575+ expect ( result ) . toHaveLength ( 1 ) ;
576+ } ) ;
577+
578+ it ( "should find running stale job without timeout" , async ( ) => {
579+ const job : NewJobData = {
580+ queue : "default" ,
581+ class : "TestJob" ,
582+ args : [ { foo : "bar" } ] ,
583+ constructor_args : [ { } ] ,
584+ state : "waiting" ,
585+ script : "test.js" ,
586+ attempt : 0 ,
587+ max_attempts : 5 ,
588+ } ;
589+
590+ let insertedJob = await backend . createNewJob ( job ) ;
591+
592+ insertedJob = await backend . createNewJob ( job ) ;
593+ await backend . updateJob ( { ...insertedJob , state : "canceled" , attempted_at : new Date ( 0 ) } ) ;
594+
595+ insertedJob = await backend . createNewJob ( job ) ;
596+ await backend . updateJob ( { ...insertedJob , state : "claimed" , attempted_at : new Date ( 0 ) } ) ;
597+
598+ insertedJob = await backend . createNewJob ( job ) ;
599+ await backend . updateJob ( { ...insertedJob , state : "completed" , attempted_at : new Date ( 0 ) } ) ;
600+
601+ insertedJob = await backend . createNewJob ( job ) ;
602+ await backend . updateJob ( { ...insertedJob , state : "failed" , attempted_at : new Date ( 0 ) } ) ;
603+
604+ insertedJob = await backend . createNewJob ( job ) ;
605+ await backend . updateJob ( {
606+ ...insertedJob ,
607+ state : "running" ,
608+ attempted_at : new Date ( ) ,
609+ timeout : 1000000 ,
610+ } ) ;
611+
612+ insertedJob = await backend . createNewJob ( job ) ;
613+ await backend . updateJob ( { ...insertedJob , state : "running" , attempted_at : new Date ( 0 ) } ) ;
614+
615+ const result = await backend . staleJobs ( ) ;
616+ expect ( result ) . toHaveLength ( 1 ) ;
617+ } ) ;
618+
619+ it ( "should find running stale job with timeout" , async ( ) => {
620+ const job : NewJobData = {
621+ queue : "default" ,
622+ class : "TestJob" ,
623+ args : [ { foo : "bar" } ] ,
624+ constructor_args : [ { } ] ,
625+ state : "waiting" ,
626+ script : "test.js" ,
627+ attempt : 0 ,
628+ max_attempts : 5 ,
629+ } ;
630+
631+ let insertedJob = await backend . createNewJob ( job ) ;
632+
633+ insertedJob = await backend . createNewJob ( job ) ;
634+ await backend . updateJob ( { ...insertedJob , state : "canceled" , attempted_at : new Date ( ) } ) ;
635+
636+ insertedJob = await backend . createNewJob ( job ) ;
637+ await backend . updateJob ( { ...insertedJob , state : "claimed" , attempted_at : new Date ( ) } ) ;
638+
639+ insertedJob = await backend . createNewJob ( job ) ;
640+ await backend . updateJob ( { ...insertedJob , state : "completed" , attempted_at : new Date ( ) } ) ;
641+
642+ insertedJob = await backend . createNewJob ( job ) ;
643+ await backend . updateJob ( { ...insertedJob , state : "failed" , attempted_at : new Date ( ) } ) ;
644+
645+ const now = new Date ( ) ;
646+ insertedJob = await backend . createNewJob ( job ) ;
647+ await backend . updateJob ( {
648+ ...insertedJob ,
649+ state : "running" ,
650+ attempted_at : new Date ( now . getTime ( ) - 1000001 ) ,
651+ timeout : 1000000 ,
652+ } ) ;
653+
654+ insertedJob = await backend . createNewJob ( job ) ;
655+ await backend . updateJob ( {
656+ ...insertedJob ,
657+ state : "running" ,
658+ attempted_at : new Date ( now . getTime ( ) - 5000 ) ,
659+ timeout : 1000000 ,
660+ } ) ;
661+
662+ insertedJob = await backend . createNewJob ( job ) ;
663+ await backend . updateJob ( { ...insertedJob , state : "running" , attempted_at : new Date ( ) } ) ;
664+
665+ const result = await backend . staleJobs ( ) ;
666+ expect ( result ) . toHaveLength ( 1 ) ;
667+ } ) ;
668+
669+ it ( "should find many stale jobs if ms very low" , async ( ) => {
670+ const job : NewJobData = {
671+ queue : "default" ,
672+ class : "TestJob" ,
673+ args : [ { foo : "bar" } ] ,
674+ constructor_args : [ { } ] ,
675+ state : "waiting" ,
676+ script : "test.js" ,
677+ attempt : 0 ,
678+ max_attempts : 5 ,
679+ } ;
680+
681+ let insertedJob = await backend . createNewJob ( job ) ;
682+
683+ insertedJob = await backend . createNewJob ( job ) ;
684+ await backend . updateJob ( { ...insertedJob , state : "canceled" } ) ;
685+
686+ insertedJob = await backend . createNewJob ( job ) ;
687+ await backend . updateJob ( { ...insertedJob , state : "claimed" , claimed_at : new Date ( ) } ) ;
688+
689+ insertedJob = await backend . createNewJob ( job ) ;
690+ await backend . updateJob ( { ...insertedJob , state : "completed" } ) ;
691+
692+ insertedJob = await backend . createNewJob ( job ) ;
693+ await backend . updateJob ( { ...insertedJob , state : "failed" } ) ;
694+
695+ insertedJob = await backend . createNewJob ( job ) ;
696+ await backend . updateJob ( { ...insertedJob , state : "running" , attempted_at : new Date ( ) , timeout : - 1 } ) ;
697+
698+ insertedJob = await backend . createNewJob ( job ) ;
699+ await backend . updateJob ( { ...insertedJob , state : "running" , attempted_at : new Date ( ) } ) ;
700+
701+ const result = await backend . staleJobs ( 0 , 0 ) ;
702+ expect ( result ) . toHaveLength ( 3 ) ;
703+ } ) ;
704+ } ) ;
498705}
0 commit comments