-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: add Acknowledgments section to README.md * fix: tsconfig.json with importHelpers and baseUrl changes * fix: concurrency.ts with Pulse import statement and job definitions * refactor: define function in define.ts to accept optional options parameter * fix: JobAttributes interface in index.ts to allow any type for the 'data' property * docs: Update Pulse example code in README.md
- Loading branch information
1 parent
8495aac
commit b5a67e9
Showing
1 changed file
with
68 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,71 +179,90 @@ Pulse is a new fork of the [Agenda](https://github.com/agenda/agenda) project, c | |
#### Example | ||
|
||
```typescript | ||
/** | ||
* @file Illustrate concurrency and locking | ||
*/ | ||
import Pulse from '@pulsecron/pulse'; | ||
|
||
function time() { | ||
return new Date().toTimeString().split(' ')[0]; | ||
} | ||
const mongoConnectionString = 'mongodb://localhost:27017/pulse'; | ||
|
||
function sleep(ms) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
} | ||
const pulse = new Pulse({ db: { address: mongoConnectionString } }); | ||
|
||
const pulse = new Pulse({ | ||
db: { | ||
address: 'mongodb://localhost:27017/pulse-concurrency', | ||
collection: `pulseJobs-${Math.random()}`, | ||
}, | ||
}); | ||
// Or override the default collection name: | ||
// const pulse = new Pulse({db: {address: mongoConnectionString, collection: 'jobCollectionName'}}); | ||
|
||
let jobRunCount = 1; | ||
pulse.define( | ||
'long-running job', | ||
{ | ||
lockLifetime: 5 * 1000, // Max amount of time the job should take | ||
concurrency: 3, // Max number of job instances to run at the same time | ||
}, | ||
async (job, done) => { | ||
const thisJob = jobRunCount++; | ||
console.log(`#${thisJob} started`); | ||
// or pass additional connection options: | ||
// const pulse = new Pulse({db: {address: mongoConnectionString, collection: 'jobCollectionName', options: {ssl: true}}}); | ||
|
||
// or pass in an existing mongodb-native MongoClient instance | ||
// const pulse = new Pulse({mongo: myMongoClient}); | ||
|
||
// 3 job instances will be running at the same time, as specified by `concurrency` above | ||
await sleep(30 * 1000); | ||
// Comment the job processing statement above, and uncomment one of the blocks below | ||
/** | ||
* Example of defining a job | ||
*/ | ||
pulse.define('delete old users', async (job) => { | ||
console.log('Deleting old users...'); | ||
return; | ||
}); | ||
|
||
/** | ||
* Example of repeating a job | ||
*/ | ||
(async function () { | ||
// IIFE to give access to async/await | ||
await pulse.start(); | ||
|
||
await pulse.every('3 minutes', 'delete old users'); | ||
|
||
// Only one job will run at a time because 3000 < lockLifetime | ||
// await sleep(3 * 1000); | ||
// Alternatively, you could also do: | ||
await pulse.every('*/3 * * * *', 'delete old users'); | ||
})(); | ||
|
||
console.log(`#${thisJob} finished`); | ||
done(); | ||
} | ||
/** | ||
* Example of defining a job with options | ||
*/ | ||
pulse.define( | ||
'send email report', | ||
async (job) => { | ||
const { to } = job.attrs.data; | ||
|
||
console.log(`Sending email report to ${to}`); | ||
}, | ||
{ lockLifetime: 5 * 1000, priority: 'high', concurrency: 10 } | ||
); | ||
|
||
/** | ||
* Example of scheduling a job | ||
*/ | ||
(async function () { | ||
console.log(time(), 'Pulse started'); | ||
pulse.processEvery('1 second'); | ||
await pulse.start(); | ||
await pulse.every('1 second', 'long-running job'); | ||
|
||
// Log job start and completion/failure | ||
pulse.on('start', (job) => { | ||
console.log(time(), `Job <${job.attrs.name}> starting`); | ||
}); | ||
pulse.on('success', (job) => { | ||
console.log(time(), `Job <${job.attrs.name}> succeeded`); | ||
}); | ||
pulse.on('fail', (error, job) => { | ||
console.log(time(), `Job <${job.attrs.name}> failed:`, error); | ||
}); | ||
await pulse.schedule('in 20 minutes', 'send email report', { to: '[email protected]' }); | ||
})(); | ||
|
||
/** | ||
* Example of repeating a job | ||
*/ | ||
(async function () { | ||
const weeklyReport = pulse.create('send email report', { to: '[email protected]' }); | ||
await pulse.start(); | ||
await weeklyReport.repeatEvery('1 week').save(); | ||
})(); | ||
|
||
/** | ||
* Check job start and completion/failure | ||
*/ | ||
pulse.on('start', (job) => { | ||
console.log(time(), `Job <${job.attrs.name}> starting`); | ||
}); | ||
pulse.on('success', (job) => { | ||
console.log(time(), `Job <${job.attrs.name}> succeeded`); | ||
}); | ||
pulse.on('fail', (error, job) => { | ||
console.log(time(), `Job <${job.attrs.name}> failed:`, error); | ||
}); | ||
|
||
function time() { | ||
return new Date().toTimeString().split(' ')[0]; | ||
} | ||
|
||
|
||
``` | ||
|
||
|
||
|