This repository has been archived by the owner on Sep 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
386 additions
and
12 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
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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import './BaseDriver' | ||
import '../Support/SQS' | ||
|
||
/** | ||
* AWS Simple Queue Service (SQS) backed Queue Driver | ||
*/ | ||
export class SQSDriver extends BaseDriver { | ||
client = null | ||
|
||
constructor(app, config) { | ||
super(app, config) | ||
|
||
this.client = new SQS(config) | ||
} | ||
|
||
ready() { | ||
return this.client.createQueue() | ||
} | ||
|
||
connect() { | ||
return Promise.resolve() | ||
} | ||
|
||
async dispatch(job) { | ||
const payload = this.buildPayload(job) | ||
const queueUrl = this.client.queueUrls[payload.queue] | ||
|
||
if(queueUrl.isNil) { | ||
return Promise.reject(`Unable to find SQS url for job queue ${payload.queue}`) | ||
} | ||
|
||
const params = { | ||
QueueUrl: queueUrl, | ||
MessageBody: JSON.stringify(payload), | ||
DelaySeconds: payload.delay || 0 | ||
} | ||
|
||
return this.client.put(params) | ||
} | ||
|
||
listen(queues, concurrency, jobHandler, errorHandler) { | ||
// SQS can batch ingest up to 10 jobs in a single call | ||
const concurrentListens = Math.ceil(concurrency / 10) | ||
const remainderJobConcurrency = 10 - ((concurrentListens * 10) - concurrency) | ||
|
||
const listeners = [ ] | ||
|
||
for(let i = 0; i < concurrentListens; i++) { | ||
for(const queue of queues) { | ||
let concurrentJobs = 10 | ||
|
||
if(i === (concurrentListens - 1)) { | ||
concurrentJobs = remainderJobConcurrency | ||
} | ||
|
||
listeners.push(this._listen(queue, concurrentJobs, jobHandler, errorHandler)) | ||
} | ||
} | ||
|
||
return Promise.all(listeners) | ||
} | ||
|
||
_listen(queue, concurrency, jobHandler, errorHandler) { | ||
return this.client.watch(queue, concurrency, async function callback(jobData, dispatchedAt, pastTries = 1) { | ||
const job = JSON.parse(jobData) | ||
const isExpired = timeout => { | ||
return (timeout !== 0) && ((new Date - dispatchedAt) > timeout) | ||
} | ||
|
||
try { | ||
if(isExpired(job.timeout)) { | ||
return | ||
} | ||
|
||
await jobHandler(job) | ||
} catch(err) { | ||
try { | ||
const tries = Number.parseInt(job.tries) || 1 | ||
|
||
if((pastTries >= tries) || isExpired(job.timeout)) { | ||
throw err | ||
} | ||
|
||
if(job.retry_delay > 0) { | ||
await new Promise(resolve => { | ||
return setTimeout(() => { | ||
return resolve() | ||
}, job.retry_delay) | ||
}) | ||
} | ||
} catch(err) { | ||
return errorHandler(job, err) | ||
} | ||
|
||
return callback(jobData, dispatchedAt, pastTries += 1) | ||
} | ||
}) | ||
} | ||
|
||
destroy() { | ||
this.client.constructor.queueUrls = { } | ||
return super.destroy() | ||
} | ||
|
||
} |
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.