React Native Queue
Forked from billmalarky/react-native-queue as appears abandoned in 2018. Keeping up to date until we no longer use it.
Simple. Powerful. Persistent.
![License](https://img.shields.io/badge/license-MIT-blue.svg)
A React Native at-least-once priority job queue / task queue backed by persistent Realm storage. Jobs will persist until completed, even if user closes and re-opens app. React Native Queue is easily integrated into OS background processes (services) so you can ensure the queue will continue to process until all jobs are completed even if app isn't in focus. It also plays well with Workers so your jobs can be thrown on the queue, then processed in dedicated worker threads for greatly improved processing performance.
Table of Contents
Features
- Simple API: Set up job workers and begin creating your jobs in minutes with just two basic API calls
- queue.addWorker(name, workerFunction, options = {})
- queue.createJob(name, payload = {}, options = {}, startQueue = true)
- Powerful options: Easily modify default functionality. Set job timeouts, number of retry attempts, priority, job lifecycle callbacks, and worker concurrency with an options object. Start queue processing with a lifespan to easily meet OS background task time limits.
- Persistent Jobs: Jobs are persisted with Realm. Because jobs persist, you can easily continue to process jobs across app restarts or in OS background tasks until completed or failed (or app is uninstalled).
- Powerful Integrations: React Native Queue was designed to play well with others. The queue quickly integrates with a variety of OS background task and Worker packages so processing your jobs in a background service or dedicated thread have never been easier.
React Native Compatibility
At the core this package leverages Realm which maintains its own compatibility. This produces
an interesting problem as we depend on a package which enforces React Native compatibility, but peer to react native.
This means it's very crucial to respect to select the proper version and respect the peering.
Queue Version | Realm Version | React Native | Hermes Support |
---|
2.2.0 | 11.10.1 | => 0.71.4 | Yes |
2.1.1 | 11.5.2 | => 0.71.4 | Yes |
2.1.0 | 11.5.1 | => 0.71.0 | Yes |
2.0.0 | 10.21.1 | => 0.64.0 | No |
Example Use Cases
React Native Queue is designed to be a swiss army knife for task management in React Native. It abstracts away the many annoyances related to processing complex tasks, like durability, retry-on-failure, timeouts, chaining processes, and more. Just throw your jobs onto the queue and relax - they're covered.
Need advanced task functionality like dedicated worker threads or OS services? Easy:
Example Queue Tasks:
- Downloading content for offline access.
- Media processing.
- Cache Warming.
- Durable API calls to external services, such as publishing content to a variety of 3rd party distribution channel APIs.
- Complex and time-consuming jobs that you want consistently processed regardless if app is open, closed, or repeatedly opened and closed.
- Complex tasks with multiple linked dependant steps (job chaining).
Installation
$ npm install --save @sourcetoad/react-native-queue
Or
$ yarn add @sourcetoad/react-native-queue
Basic Usage
React Native Queue is a standard job/task queue built specifically for react native applications. If you have a long-running task, or a large number of tasks, consider turning that task into a job(s) and throwing it/them onto the queue to be processed in the background instead of blocking your UI until task(s) complete.
Creating and processing jobs consists of:
- Importing and initializing React Native Queue
- Registering worker functions (the functions that execute your jobs).
- Creating jobs.
- Starting the queue (note this happens automatically on job creation, but sometimes the queue must be explicitly started such as in a OS background task or on app restart). Queue can be started with a lifespan in order to limit queue processing time.
import queueFactory from '@sourcetoad/react-native-queue';
const queue = await queueFactory();
queue.addWorker('example-job', async (id, payload) => {
console.log('EXECUTING "example-job" with id: ' + id);
console.log(payload, 'payload');
await new Promise((resolve) => {
setTimeout(() => {
console.log('"example-job" has completed!');
resolve();
}, 5000);
});
});
queue.createJob('example-job', {
emailAddress: 'foo@bar.com',
randomData: {
random: 'object',
of: 'arbitrary data'
}
}, {}, false);
queue.createJob('example-job', {
emailAddress: 'example@gmail.com',
randomData: {
random: 'object',
of: 'arbitrary data'
}
}, {
timeout: 1000
}, false);
queue.createJob('example-job', {
emailAddress: 'another@gmail.com',
randomData: {
random: 'object',
of: 'arbitrary data'
}
});
console.log('The above jobs are processing in the background of app now.');
Options and Job Lifecycle Callbacks
Worker Options (includes async job lifecycle callbacks)
queue.addWorker() accepts an options object in order to tweak standard functionality and allow you to hook into asynchronous job lifecycle callbacks.
IMPORTANT: Job Lifecycle callbacks are called asynchronously. They do not block job processing or each other. Don't put logic in onStart that you expect to be completed before the actual job process begins executing. Don't put logic in onFailure you expect to be completed before onFailed is called. You can, of course, assume that the job process has completed (or failed) before onSuccess, onFailure, onFailed, or onComplete are asynchonrously called.
queue.addWorker('job-name-here', async (id, payload) => { console.log(id); }, {
concurrency: 5,
onStart: async (id, payload) => {
console.log('Job "job-name-here" with id ' + id + ' has started processing.');
},
onSuccess: async (id, payload) => {
console.log('Job "job-name-here" with id ' + id + ' was successful.');
},
onFailure: async (id, payload) => {
console.log('Job "job-name-here" with id ' + id + ' had an attempt end in failure.');
},
onFailed: async (id, payload) => {
console.log('Job "job-name-here" with id ' + id + ' has failed.');
},
onComplete: async (id, payload) => {
console.log('Job "job-name-here" with id ' + id + ' has completed processing.');
}
});
Job Options
queue.createJob() accepts an options object in order to tweak standard functionality.
queue.createJob('job-name-here', {foo: 'bar'}, {
priority: 10,
timeout: 30000,
attempts: 4,
});
Testing with Jest
Because realm will write database files to the root test directory when running jest tests, you will need to add the following to your gitignore file if you use tests.
/reactNativeQueue.realm*
Caveats
Jobs must be idempotent. As with most queues, there are certain scenarios that could lead to React Native Queue processing a job more than once. For example, a job could timeout locally but remote server actions kicked off by the job could continue to execute. If the job is retried then effectively the remote code will be run twice. Furthermore, a job could fail due to some sort of exception halfway through then the next time it runs the first half of the job has already been executed once. Always design your React Native Queue jobs to be idempotent. If this is not possible, set job "attempts" option to be 1 (the default setting), and then you will have to write custom logic to handle the event of a job failing (perhaps via a job chain).
Advanced Usage Examples
Advanced Job Full Example
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
Button
} from 'react-native';
import queueFactory from '@sourcetoad/react-native-queue';
export default class App extends Component<{}> {
constructor(props) {
super(props);
this.state = {
queue: null
};
this.init();
}
async init() {
const queue = await queueFactory();
queue.addWorker('standard-example', async (id, payload) => {
console.log('standard-example job '+id+' executed.');
});
let recursionCounter = 1;
queue.addWorker('recursive-example', async (id, payload) => {
console.log('recursive-example job '+ id +' started');
console.log(recursionCounter, 'recursionCounter');
recursionCounter++;
await new Promise((resolve) => {
setTimeout(() => {
console.log('recursive-example '+ id +' has completed!');
if (recursionCounter <= 3) {
queue.createJob('recursive-example');
}
resolve();
}, 1000);
});
});
queue.addWorker('start-job-chain', async (id, payload) => {
console.log('start-job-chain job '+ id +' started');
console.log('step: ' + payload.step);
await new Promise((resolve) => {
setTimeout(() => {
console.log('start-job-chain '+ id +' has completed!');
queue.createJob('job-chain-2nd-step', {
callerJobName: 'start-job-chain',
step: payload.step + 1
});
resolve();
}, 1000);
});
});
queue.addWorker('job-chain-2nd-step', async (id, payload) => {
console.log('job-chain-2nd-step job '+ id +' started');
console.log('step: ' + payload.step);
await new Promise((resolve) => {
setTimeout(() => {
console.log('job-chain-2nd-step '+ id +' has completed!');
queue.createJob('job-chain-final-step', {
callerJobName: 'job-chain-2nd-step',
step: payload.step + 1
});
resolve();
}, 1000);
});
});
queue.addWorker('job-chain-final-step', async (id, payload) => {
console.log('job-chain-final-step job '+ id +' started');
console.log('step: ' + payload.step);
await new Promise((resolve) => {
setTimeout(() => {
console.log('job-chain-final-step '+ id +' has completed!');
console.log('Job chain is now completed!');
resolve();
}, 1000);
});
});
queue.start();
this.setState({
queue
});
}
makeJob(jobName, payload = {}) {
this.state.queue.createJob(jobName, payload);
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
{this.state.queue && <Button title={"Press For Standard Example"} onPress={ () => { this.makeJob('standard-example') } } /> }
{this.state.queue && <Button title={"Press For Recursive Example"} onPress={ () => { this.makeJob('recursive-example') } } /> }
{this.state.queue && <Button title={"Press For Job Chain Example"} onPress={ () => { this.makeJob('start-job-chain', { step: 1 }) } } /> }
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
OS Background Task Full Example
For the purpose of this example we will use the React Native Background Task module, but you could integrate React Native Queue with any acceptable OS background task module.
Follow the installation steps for React Native Background Task.
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
Button,
AsyncStorage
} from 'react-native';
import BackgroundTask from 'react-native-background-task'
import queueFactory from '@sourcetoad/react-native-queue';
BackgroundTask.define(async () => {
queue = await queueFactory();
queue.addWorker('background-example', async (id, payload) => {
if (payload.name == 'luke') {
await AsyncStorage.setItem('lukeData', 'Luke Skywalker arbitrary data loaded!');
} else {
await AsyncStorage.setItem('c3poData', 'C-3PO arbitrary data loaded!');
}
});
await queue.start(20000);
BackgroundTask.finish();
});
export default class App extends Component<{}> {
constructor(props) {
super(props);
this.state = {
queue: null,
data: null
};
this.init();
}
async init() {
const queue = await queueFactory();
queue.addWorker('background-example', async (id, payload) => {
console.log(id);
});
this.setState({
queue
});
}
componentDidMount() {
BackgroundTask.schedule();
}
makeJob(jobName, payload = {}) {
console.log('job is created but will not execute until the above OS background task runs in ~15 min');
this.state.queue.createJob(jobName, payload, {
timeout: 5000
}, false);
}
async checkData() {
const lukeData = await AsyncStorage.getItem('lukeData');
const c3poData = await AsyncStorage.getItem('c3poData');
this.setState({
data: {
lukeData: (lukeData) ? lukeData : 'No data loaded from OS background task yet for Luke Skywalker.',
c3poData: (c3poData) ? c3poData : 'No data loaded from OS background task yet for C-3PO.'
}
});
}
render() {
let output = 'No data loaded from OS background task yet.';
if (this.state.data) {
output = JSON.stringify(this.state.data);
}
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text>Click buttons below to add OS background task jobs.</Text>
<Text>Then Close App (task will not fire if app is in focus).</Text>
<Text>Job will exec in ~15 min in OS background.</Text>
{this.state.queue && <Button title={"Press To Queue Luke Skywalker Job"} onPress={ () => { this.makeJob('background-example', { name: 'luke' }) } } /> }
{this.state.queue && <Button title={"Press To Queue C-3PO Job"} onPress={ () => { this.makeJob('background-example', { name: 'c3po' }) } } /> }
<Button title={"Check if Data was loaded in OS background"} onPress={ () => { this.checkData() } } />
<Text>{output}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});