SimonErm / react-native-job-queue

Easy to use react-native queuing library
https://simonerm.github.io/react-native-job-queue/
122 stars 33 forks source link

onQueueFinish returns old values #53

Closed mival closed 2 years ago

mival commented 3 years ago

onQueueFinish callback returns empty failed atribute for failed jobs. Calling getJobs after queue finishing returns correct values. Look likes there are some async/timing issues while reading rawJobs value

SimonErm commented 3 years ago

Am i right that this only occurs if queue.stop() is called?

mival commented 3 years ago

Hi, we have similar code to this:

queue.configure({
  concurrency: 1,
  onQueueFinish: async (tasks) => {
    // tasks - here missing failed atribute
    const jobs = await queue.getJobs();

    if (jobs.length > 0) {
      const failedJobIds = jobs.map(item => item.failed && item.id);
      // here are failed items correct

    }
    queue.stop();
    queue.removeWorker('offlineRequestsQueueWorker', true);
  },
});

if (queue.registeredWorkers.offlineRequestsQueueWorker) {
  queue.removeWorker('offlineRequestsQueueWorker', true);
}

queue.addWorker(
  new Worker('offlineRequestsQueueWorker', async (payload: any) => {

    return new Promise((resolve, reject) => {
      // some js fetch with resolve / reject
    });
  }),
);
SimonErm commented 3 years ago

You don't have call queue.stop() in onQueueFinish, since the queue is already stopped when the callback is called.

The only case i can find where not all finished tasks would be passed to onQueueFinish would be if the queue is stopped by calling queue.stop(). To fix this i have to kind of queue the stop call until the current running jobs finished.

paulrostorp commented 2 years ago

@SimonErm I have a similar issue with the onFailure worker callback... the failed field is empty.

I think it's because this line: https://github.com/SimonErm/react-native-job-queue/blob/c817e6e95e6986f99bd2432285db011b61e0e7fe/src/Queue.ts#L330 is being called before the job has been updated with the proper data

SimonErm commented 2 years ago

@paulrostorp thank you for the addition, i will take a look at that.