55 * Queue Job Realm Schema defined in ../config/Database
66 *
77 */
8-
98import Database from '../config/Database' ;
109import uuid from 'react-native-uuid' ;
1110import Worker from './Worker' ;
1211import promiseReflect from 'promise-reflect' ;
1312
14-
1513export class Queue {
16-
1714 /**
1815 *
1916 * Set initial class properties.
@@ -82,7 +79,6 @@ export class Queue {
8279 * @param startQueue - {boolean} - Whether or not to immediately begin prcessing queue. If false queue.start() must be manually called.
8380 */
8481 createJob ( name , payload = { } , options = { } , startQueue = true ) {
85-
8682 if ( ! name ) {
8783 throw new Error ( 'Job name must be supplied.' ) ;
8884 }
@@ -109,10 +105,9 @@ export class Queue {
109105 } ) ;
110106
111107 // Start queue on job creation if it isn't running by default.
112- if ( startQueue && this . status == 'inactive' ) {
108+ if ( startQueue && this . status === 'inactive' ) {
113109 this . start ( ) ;
114110 }
115-
116111 }
117112
118113 /**
@@ -139,9 +134,8 @@ export class Queue {
139134 * @return {boolean|undefined } - False if queue is already started. Otherwise nothing is returned when queue finishes processing.
140135 */
141136 async start ( lifespan = 0 ) {
142-
143137 // If queue is already running, don't fire up concurrent loop.
144- if ( this . status == 'active' ) {
138+ if ( this . status === 'active' ) {
145139 return false ;
146140 }
147141
@@ -160,8 +154,7 @@ export class Queue {
160154 concurrentJobs = await this . getConcurrentJobs ( ) ;
161155 }
162156
163- while ( this . status == 'active' && concurrentJobs . length ) {
164-
157+ while ( this . status === 'active' && concurrentJobs . length ) {
165158 // Loop over jobs and process them concurrently.
166159 const processingJobs = concurrentJobs . map ( job => {
167160 return this . processJob ( job ) ;
@@ -179,11 +172,9 @@ export class Queue {
179172 } else {
180173 concurrentJobs = await this . getConcurrentJobs ( ) ;
181174 }
182-
183175 }
184176
185177 this . status = 'inactive' ;
186-
187178 }
188179
189180 /**
@@ -206,22 +197,16 @@ export class Queue {
206197 * @return {promise } - Promise that resolves to a collection of all the jobs in the queue.
207198 */
208199 async getJobs ( sync = false ) {
209-
210200 if ( sync ) {
211-
212201 let jobs = null ;
213202 this . realm . write ( ( ) => {
214-
215203 jobs = Array . from ( this . realm . objects ( 'Job' ) ) ;
216-
217204 } ) ;
218205
219206 return jobs ;
220-
221207 } else {
222208 return Array . from ( await this . realm . objects ( 'Job' ) ) ;
223209 }
224-
225210 }
226211
227212 /**
@@ -239,11 +224,9 @@ export class Queue {
239224 * @return {promise } - Promise resolves to an array of job(s) to be processed next by the queue.
240225 */
241226 async getConcurrentJobs ( queueLifespanRemaining = 0 ) {
242-
243227 let concurrentJobs = [ ] ;
244228
245229 this . realm . write ( ( ) => {
246-
247230 // Get next job from queue.
248231 let nextJob = null ;
249232
@@ -294,9 +277,7 @@ export class Queue {
294277 . sorted ( [ [ 'priority' , true ] , [ 'created' , false ] ] ) ) ;
295278
296279 concurrentJobs = reselectedJobs . slice ( 0 , concurrency ) ;
297-
298280 }
299-
300281 } ) ;
301282
302283 return concurrentJobs ;
@@ -319,7 +300,6 @@ export class Queue {
319300 * @param job {object} - Job realm model object
320301 */
321302 async processJob ( job ) {
322-
323303 // Data must be cloned off the realm job object for several lifecycle callbacks to work correctly.
324304 // This is because realm job is deleted before some callbacks are called if job processed successfully.
325305 // More info: https://github.com/billmalarky/react-native-queue/issues/2#issuecomment-361418965
@@ -331,27 +311,21 @@ export class Queue {
331311 this . worker . executeJobLifecycleCallback ( 'onStart' , jobName , jobId , jobPayload ) ;
332312
333313 try {
334-
335314 await this . worker . executeJob ( job ) ;
336315
337316 // On successful job completion, remove job
338317 this . realm . write ( ( ) => {
339-
340318 this . realm . delete ( job ) ;
341-
342319 } ) ;
343320
344321 // Job has processed successfully, fire onSuccess and onComplete job lifecycle callbacks.
345322 this . worker . executeJobLifecycleCallback ( 'onSuccess' , jobName , jobId , jobPayload ) ;
346323 this . worker . executeJobLifecycleCallback ( 'onComplete' , jobName , jobId , jobPayload ) ;
347-
348324 } catch ( error ) {
349-
350325 // Handle job failure logic, including retries.
351326 let jobData = JSON . parse ( job . data ) ;
352327
353328 this . realm . write ( ( ) => {
354-
355329 // Increment failed attempts number
356330 if ( ! jobData . failedAttempts ) {
357331 jobData . failedAttempts = 1 ;
@@ -375,7 +349,6 @@ export class Queue {
375349 if ( jobData . failedAttempts >= jobData . attempts ) {
376350 job . failed = new Date ( ) ;
377351 }
378-
379352 } ) ;
380353
381354 // Execute job onFailure lifecycle callback.
@@ -386,9 +359,7 @@ export class Queue {
386359 this . worker . executeJobLifecycleCallback ( 'onFailed' , jobName , jobId , jobPayload ) ;
387360 this . worker . executeJobLifecycleCallback ( 'onComplete' , jobName , jobId , jobPayload ) ;
388361 }
389-
390362 }
391-
392363 }
393364
394365 /**
@@ -398,34 +369,24 @@ export class Queue {
398369 * If jobName is supplied, only jobs associated with that name
399370 * will be deleted. Otherwise all jobs in queue will be deleted.
400371 *
401- * @param jobName {string} - Name associated with job (and related job worker).
372+ * @param jobName {string|null } - Name associated with job (and related job worker).
402373 */
403374 flushQueue ( jobName = null ) {
404-
405375 if ( jobName ) {
406-
407376 this . realm . write ( ( ) => {
408-
409377 let jobs = Array . from ( this . realm . objects ( 'Job' )
410378 . filtered ( 'name == "' + jobName + '"' ) ) ;
411379
412380 if ( jobs . length ) {
413381 this . realm . delete ( jobs ) ;
414382 }
415-
416383 } ) ;
417-
418384 } else {
419385 this . realm . write ( ( ) => {
420-
421386 this . realm . deleteAll ( ) ;
422-
423387 } ) ;
424388 }
425-
426389 }
427-
428-
429390}
430391
431392/**
@@ -435,10 +396,8 @@ export class Queue {
435396 * @return {Queue } - A queue instance.
436397 */
437398export default async function queueFactory ( ) {
438-
439399 const queue = new Queue ( ) ;
440400 await queue . init ( ) ;
441401
442402 return queue ;
443-
444403}
0 commit comments