@@ -120,16 +120,26 @@ func (q *sidekiqQueue) Pull(opt *PullOptions) error {
120120 }
121121 }()
122122
123- pull := func () error {
124- res , err := q . dequeueScript . Run ( ctx , q . client , [] string { queueNamespace } ,
125- queueNamespace ,
126- queueID ,
127- ). Result ()
128- if err != nil {
129- return err
123+ res , err := q . dequeueScript . Run ( ctx , q . client , [] string { queueNamespace },
124+ queueNamespace ,
125+ queueID ,
126+ ). Result ()
127+ if err != nil {
128+ if errors . Is ( err , redis . Nil ) {
129+ return nil
130130 }
131+ return err
132+ }
133+ queue := opt .Queue
134+ if queue == nil {
135+ queue = q .RedisQueue
136+ }
137+ jobm := res .([]interface {})
138+ jobs := make ([]* work.Job , len (jobm ))
139+ queueIDs := make ([]string , len (jobm ))
140+ for i , iface := range jobm {
131141 var sqJob sidekiqJob
132- err = json .NewDecoder (strings .NewReader (res .(string ))).Decode (& sqJob )
142+ err : = json .NewDecoder (strings .NewReader (iface .(string ))).Decode (& sqJob )
133143 if err != nil {
134144 return err
135145 }
@@ -141,48 +151,62 @@ func (q *sidekiqQueue) Pull(opt *PullOptions) error {
141151 if err != nil {
142152 return err
143153 }
144- queue := opt .Queue
145- if queue == nil {
146- queue = q .RedisQueue
154+ jobs [i ] = job
155+ queueIDs [i ] = FormatQueueID (sqJob .Queue , sqJob .Class )
156+ }
157+ found := make ([]* work.Job , len (jobs ))
158+ if finder , ok := queue .(work.BulkJobFinder ); ok {
159+ jobIDs := make ([]string , len (jobs ))
160+ for i , job := range jobs {
161+ jobIDs [i ] = job .ID
162+ }
163+ // best effort to check for duplicates
164+ foundJobs , err := finder .BulkFind (jobIDs , & work.FindOptions {
165+ Namespace : opt .Namespace ,
166+ })
167+ if err != nil {
168+ return err
147169 }
148- var found bool
149- if finder , ok := queue .(work.BulkJobFinder ); ok {
150- // best effort to check for duplicates
151- jobs , err := finder .BulkFind ([]string {job .ID }, & work.FindOptions {
170+ found = foundJobs
171+ }
172+ if bulkEnqueuer , ok := queue .(work.BulkEnqueuer ); ok {
173+ m := make (map [string ][]* work.Job )
174+ for i , job := range jobs {
175+ if found [i ] != nil {
176+ continue
177+ }
178+ queueID := queueIDs [i ]
179+ m [queueID ] = append (m [queueID ], job )
180+ }
181+ for queueID , jobs := range m {
182+ err := bulkEnqueuer .BulkEnqueue (jobs , & work.EnqueueOptions {
152183 Namespace : opt .Namespace ,
184+ QueueID : queueID ,
153185 })
154186 if err != nil {
155187 return err
156188 }
157- found = len (jobs ) == 1 && jobs [0 ] != nil
158189 }
159- if ! found {
190+ } else {
191+ for i , job := range jobs {
192+ if found [i ] != nil {
193+ continue
194+ }
160195 err := queue .Enqueue (job , & work.EnqueueOptions {
161196 Namespace : opt .Namespace ,
162- QueueID : FormatQueueID ( sqJob . Queue , sqJob . Class ) ,
197+ QueueID : queueIDs [ i ] ,
163198 })
164199 if err != nil {
165200 return err
166201 }
167202 }
168- err = q .ackScript .Run (ctx , q .client , []string {queueNamespace },
169- queueNamespace ,
170- queueID ,
171- res .(string ),
172- ).Err ()
173- if err != nil {
174- return err
175- }
176- return nil
177203 }
178-
179- for {
180- err := pull ()
181- if err != nil {
182- if errors .Is (err , redis .Nil ) {
183- return nil
184- }
185- return err
186- }
204+ err = q .ackScript .Run (ctx , q .client , []string {queueNamespace },
205+ queueNamespace ,
206+ queueID ,
207+ ).Err ()
208+ if err != nil {
209+ return err
187210 }
211+ return nil
188212}
0 commit comments