Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from timgit/wildcard-subs
3.1
- Loading branch information
Showing
16 changed files
with
238 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,39 @@ | ||
<!-- TOC --> | ||
- [Wildcard completion subscription](#wildcard-completion-subscription) | ||
- [Batch job fetch](#batch-job-fetch) | ||
|
||
- [Async Readme](#async-readme) | ||
## Wildcard completion subscription | ||
|
||
<!-- /TOC --> | ||
|
||
## Async Readme | ||
|
||
Same as readme, but with async await | ||
Subscribe based on matched pattern when jobs are completed to handle common completion logic. | ||
|
||
```js | ||
async function wildcardCompletion() { | ||
|
||
const PgBoss = require('pg-boss'); | ||
|
||
try { | ||
await readme(); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
await boss.onComplete(`worker-register-*`, commonRegistrationCompletion) | ||
|
||
async function readme() { | ||
|
||
const boss = new PgBoss('postgres://user:pass@host/database'); | ||
boss.on('error', error => console.error(error)); | ||
|
||
await boss.start(); | ||
async function commonRegistrationCompletion(job) { | ||
if(job.data.failed) | ||
return console.log(`job ${job.data.request.id} failed`); | ||
|
||
await logRegistration(job.data.response); | ||
} | ||
|
||
} | ||
``` | ||
|
||
const queue = 'some-queue'; | ||
## Batch job fetch | ||
|
||
let jobId = await boss.publish(queue, {param1: 'parameter1'}); | ||
|
||
console.log(`created job in queue ${queue}: ${jobId}`); | ||
Fetch an array of jobs in a subscription. Returning a promise ensures no more jobs are fetched until it resolves. | ||
|
||
await boss.subscribe(queue, job => await onJob(job)); | ||
```js | ||
async function highVolumeSubscription() { | ||
|
||
await boss.onComplete(queue, job => { | ||
console.log(`job ${job.data.request.id} completed`); | ||
console.log(` - in state ${job.data.state}`); | ||
console.log(` - responded with '${job.data.response.value}'`); | ||
}); | ||
await boss.subscribe('send-text-message', {batchSize: 1000}, handleSend); | ||
|
||
async function onJob(job) { | ||
console.log(`job ${job.id} received`); | ||
console.log(` - with data: ${JSON.stringify(job.data)}`); | ||
|
||
return 'got it'; | ||
async function handleSend(jobs) { | ||
await Promise.all(jobs.map(job => textMessageService.send(job.data))); | ||
} | ||
|
||
} | ||
|
||
``` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.