Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the current executing job is lost when force terminate app(in android) #24

Open
fatfatson opened this issue Jun 13, 2018 · 8 comments
Open

Comments

@fatfatson
Copy link

if I set a very short timeout for my job for test,
the job will always fail and retry,
but if I terminate the app and restart,
that job has gone! the next job is executing.
is this a bug??

@billmalarky
Copy link
Owner

If I had to guess the job is stuck in "active" state so it is not being picked up by the queue the next time the queue tries to grab a job off the stack.

See this section of code for the realm query that grabs the next job(s) to process off the queue stack.

https://github.com/billmalarky/react-native-queue/blob/master/Models/Queue.js#L273-L275

The solution, which would have to be developed, would be some sort of process that looks for "stuck" jobs and marks them not active anymore. The question at that point is what interval to check for stuck jobs, because technically you can have a job with no timeout set (indefinite timeout) that could run forever, and we wouldn't want to randomly set that job to no longer active if it truly was still running.

The popular node.js library "kue" has dealt with this problem before so perhaps we can get inspiration there.

https://github.com/Automattic/kue#unstable-redis-connections

@fatfatson
Copy link
Author

why not marking all job as inactive on startup??
just because the unexpected termination, their state were not reset as normal.

@fatfatson
Copy link
Author

@billmalarky
I have add the reset function as my suggestion and it works correctly with force termination.
but there's another problem:
the failed jobs will live in realm forever, what about if their amount grows very fast??

@billmalarky
Copy link
Owner

billmalarky commented Jun 14, 2018 via email

@fatfatson
Copy link
Author

thanks very much, I have understood the problem. wish for improvings~

@carllippert
Copy link

@fatfatson how did you make your reset function?

@famousfilm
Copy link

This is roughly what I did to "reset" things @carllippert but I removed a lot of things that were extraneous to this library so it's probably not exactly how you should implement it.

import queueFactory from 'react-native-queue'
import Database from 'react-native-queue/config/Database'

restartQueue = () => {
    return queueFactory().then(queue => {
        // whatever action you do to start up your queue
    })
}

export function resetActiveJobs() {
    // Reset the jobs that react-native-queue immediately marked as "active" in realm. 
    // Else, if the user killed the app before the job finished, when the app restarts 
    // the job is still "active" so it doesn't requeue.
    Database.getRealmInstance().then(realm => {
        realm.write(() => {
            let jobs = realm.objects('Job').filtered('active == TRUE AND failed == null');
            if (jobs.length > 0) {
                for (let job of jobs) {
                    job.active = false
                }
            }
        })
    }).then(() => restartQueue())
}

... and I run resetActiveJobs() when I start up the app.

@rdgomt
Copy link

rdgomt commented Nov 5, 2019

Any improvements?

In my case, this functionality is very important because I want all jobs to get complete even if the user closes app/restarts phone/etc.

For now, @famousfilm implementation has worked. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants