@ -591,12 +591,17 @@ private final class JobQueue {
}
fileprivate func appDidBecomeActive ( with jobs : [ Job ] , canStart : Bool ) {
let currentlyRunningJobIds : Set < Int64 > = jobsCurrentlyRunning . wrappedValue
queue . mutate { queue in
// A v o i d r e - a d d i n g j o b s t o t h e q u e u e t h a t a r e a l r e a d y i n i t ( t h i s c a n
// h a p p e n i f t h e u s e r s e n d s t h e a p p t o t h e b a c k g r o u n d b e f o r e t h e ' o n A c t i v e '
// j o b s a n d t h e n b r i n g s i t b a c k t o t h e f o r e g r o u n d )
let jobsNotAlreadyInQueue : [ Job ] = jobs
. filter { job in ! queue . contains ( where : { $0 . id = = job . id } ) }
. filter { job in
! currentlyRunningJobIds . contains ( job . id ? ? - 1 ) &&
! queue . contains ( where : { $0 . id = = job . id } )
}
queue . append ( contentsOf : jobsNotAlreadyInQueue )
}
@ -784,14 +789,20 @@ private final class JobQueue {
guard dependencyInfo . jobs . isEmpty else {
SNLog ( " [JobRunner] \( queueContext ) found job with \( dependencyInfo . jobs . count ) dependencies, running those first " )
// / R e m o v e a l l j o b s t h i s o n e i s d e p e n d a n t o n f r o m t h e q u e u e a n d r e - i n s e r t t h e m a t t h e s t a r t o f t h e q u e u e
// / R e m o v e a l l j o b s t h i s o n e i s d e p e n d a n t o n t h a t a r e n ' t c u r r e n t l y r u n n i n g f r o m t h e q u e u e a n d r e - i n s e r t t h e m a t t h e s t a r t
// / o f t h e q u e u e
// /
// / * * N o t e : * * W e d o n ' t a d d t h e c u r r e n t j o b b a c k t h e t h e q u e u e b e c a u s e i t s h o u l d o n l y b e r e - a d d e d i f i t ' s d e p e n d e n c i e s
// / a r e s u c c e s s f u l l y c o m p l e t e d
let currentlyRunningJobIds : [ Int64 ] = Array ( detailsForCurrentlyRunningJobs . wrappedValue . keys )
let dependencyJobsNotCurrentlyRunning : [ Job ] = dependencyInfo . jobs
. filter { job in ! currentlyRunningJobIds . contains ( job . id ? ? - 1 ) }
. sorted { lhs , rhs in ( lhs . id ? ? - 1 ) < ( rhs . id ? ? - 1 ) }
queue . mutate { queue in
queue = queue
. filter { ! dependencyInfo . jobs . contains ( $0 ) }
. inserting ( contentsOf : Array ( dependencyInfo . jobs ) , at : 0 )
. filter { ! dependency JobsNotCurrentlyRunning . contains ( $0 ) }
. inserting ( contentsOf : dependencyJobsNotCurrentlyRunning , at : 0 )
}
handleJobDeferred ( nextJob )
return
@ -960,17 +971,22 @@ private final class JobQueue {
default : break
}
// / N o w t h a t t h e j o b h a s b e e n c o m p l e t e d w e w a n t t o i n s e r t a n y j o b s t h a t w e r e d e p e n d a n t o n i t t o t h e s t a r t o f t h e q u e u e ( t h e
// / mo s t l i k e l y c a s e i s t h a t w e w a n t a n e n t i r e j o b c h a i n t o b e c o m p l e t e d a t t h e s a m e t i m e r a t h e r t h a n b e i n g b l o c k e d b y o t h e r
// / un r e l a t e d j o b s )
// / N o w t h a t t h e j o b h a s b e e n c o m p l e t e d w e w a n t t o i n s e r t a n y j o b s t h a t w e r e d e p e n d a n t o n i t , t h a t a r e n ' t a l r e a d y r u n n i n g
// / to t h e s t a r t o f t h e q u e u e ( t h e mo s t l i k e l y c a s e i s t h a t w e w a n t a n e n t i r e j o b c h a i n t o b e c o m p l e t e d a t t h e s a m e t i m e r a t h e r
// / th a n b e i n g b l o c k e d b y o t h e r un r e l a t e d j o b s )
// /
// / * * N o t e : * * I f a n y o f t h e s e ` d e p e n d a n t J o b s ` h a v e o t h e r d e p e n d e n c i e s t h e n w h e n t h e y a t t e m p t t o s t a r t t h e y w i l l b e
// / r e m o v e d f r o m t h e q u e u e , r e p l a c e d b y t h e i r d e p e n d e n c i e s
if ! dependantJobs . isEmpty {
let currentlyRunningJobIds : [ Int64 ] = Array ( detailsForCurrentlyRunningJobs . wrappedValue . keys )
let dependantJobsNotCurrentlyRunning : [ Job ] = dependantJobs
. filter { job in ! currentlyRunningJobIds . contains ( job . id ? ? - 1 ) }
. sorted { lhs , rhs in ( lhs . id ? ? - 1 ) < ( rhs . id ? ? - 1 ) }
queue . mutate { queue in
queue = queue
. filter { ! dependantJobs . contains ( $0 ) }
. inserting ( contentsOf : dependantJobs , at : 0 )
. filter { ! dependantJobs NotCurrentlyRunning . contains ( $0 ) }
. inserting ( contentsOf : dependantJobs NotCurrentlyRunning , at : 0 )
}
}