# level-jobs

> Job Queue in LevelDB

Latest version **2.1.1** (published 2022-10-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install level-jobs
pnpm add level-jobs
yarn add level-jobs
bun add level-jobs
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2022-10-20 |
| First published | 2013-08-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 19.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 85 |
| Author | pgte |
| Maintainers | pgte |
| Keywords | leveldb, job, queue |

## Links

- npm: https://www.npmjs.com/package/level-jobs
- Repository: https://github.com/pgte/level-jobs
- Homepage: https://github.com/pgte/level-jobs#readme
- Issues: https://github.com/pgte/level-jobs/issues
- npm.io page: https://npm.io/package/level-jobs

## Dependencies (6)

- [xtend](https://npm.io/package/xtend.md) ~2.0.6
- [backoff](https://npm.io/package/backoff.md) ~2.3.0
- [level-hooks](https://npm.io/package/level-hooks.md) ~4.5.0
- [level-sublevel](https://npm.io/package/level-sublevel.md) ~6.4.6
- [level-write-stream](https://npm.io/package/level-write-stream.md) ~1.0.0
- [json-stringify-safe](https://npm.io/package/json-stringify-safe.md) ~5.0.0

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 2.1.1 (latest) — 2022-10-20
- 2.0.1 — 2022-10-20
- 2.1.0 — 2018-04-08
- 2.0.0 — 2017-06-28
- 1.0.0 — 2017-06-28
- 0.6.0 — 2015-11-27
- 0.5.1 — 2015-06-30
- 0.5.0 — 2015-03-29
- 0.4.2 — 2013-12-04
- 0.4.1 — 2013-12-04
- 0.4.0 — 2013-12-04
- 0.3.0 — 2013-11-29
- 0.2.1 — 2013-08-29
- 0.2.0 — 2013-08-29
- 0.1.4 — 2013-08-22
- … 6 more at https://npm.io/package/level-jobs/versions

## README

# level-jobs

> Job Queue in LevelDB for Node.js

[![Build Status](https://travis-ci.org/pgte/level-jobs.png?branch=master)](https://travis-ci.org/pgte/level-jobs)

* Define worker functions
* Persist work units
* Work units are retried when failed
* Define maximum concurrency

## Install

```bash
$ npm install level-jobs --save
```

## Use

### Create a levelup database

```javascript
var levelup = require('levelup');
var db = levelup('./db')
```

### Require level-jobs

```javascript
var Jobs = require('level-jobs');
```

### Define a worker function

This function will take care of a work unit.

```javascript
function worker(id, payload, cb) {
  doSomething(cb);
}
```

This function gets 3 arguments:

- `id` uniquely identifies a job to be executed.
- `payload` contains everyting `worker` need to process the job.
- `cb` is the callback function that must be called when the job is done.

This callback function accepts an error as the first argument. If an error is provided, the work unit is retried.


### Wrap the database

```javascript
var queue = Jobs(db, worker);
```

This database will be at the mercy and control of level-jobs, don't use it for anything else!

(this database can be a root levelup database or a sublevel)

You can define a maximum concurrency (the default is `Infinity`):

```javascript
var maxConcurrency = 2;
var queue = Jobs(db, worker, maxConcurrency);
```

### More Options

As an alternative the third argument can be an options object with these defaults:

```javascript
var options = {
  maxConcurrency: Infinity,
  maxRetries:     10,
  backoff: {
    randomisationFactor: 0,
    initialDelay: 10,
    maxDelay: 300
  }
};

var queue = Jobs(db, worker, options);
```

### Push work to the queue

```javascript
var payload = {what: 'ever'};

var jobId = queue.push(payload, function(err) {
  if (err) console.error('Error pushing work into the queue', err.stack);
});
```

or in batch:
```javascript
var payloads = [
  {what: 'ever'},
  {what: 'ever'}
];

var jobIds = queue.pushBatch(payloads, function(err) {
  if (err) console.error('Error pushing works into the queue', err.stack);
});
```

### Delete pending job

(Only works for jobs that haven't started yet!)

```javascript
queue.del(jobId, function(err) {
  if (err) console.error('Error deleting job', err.stack);
});
```

or in batch:
```javascript
queue.delBatch(jobIds, function(err) {
  if (err) console.error('Error deleting jobs', err.stack);
});
```

### Traverse jobs

`queue.pendingStream()` emits queued jobs. `queue.runningStream()` emits currently running jobs.

```javascript
var stream = queue.pendingStream();
stream.on('data', function(d) {
  var jobId = d.key;
  var work = d.value;
  console.log('pending job id: %s, work: %j', jobId, work);
});
```

### Events

A queue object emits the following event:

* `drain` — when there are no more jobs pending. Also happens on startup after consuming the backlog work units.
* `error` - when something goes wrong.
* `retry` - when a job is retried because something goes wrong.


## Client isolated API

If you simply want a pure queue client that is only able to push jobs into the queue, you can use `level-jobs/client` like this:

```javascript
var QueueClient = require('level-jobs/client');

var client = QueueClient(db);

client.push(work, function(err) {
  if (err) throw err;
  console.log('pushed');
});
```

## License

MIT

---
_Source: https://npm.io/package/level-jobs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
