This repository has been archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(1319): add scheduler mode (#106)
- Loading branch information
Showing
11 changed files
with
6,066 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM node:8 | ||
|
||
# Create our application directory | ||
RUN mkdir -p /usr/src/app | ||
WORKDIR /usr/src/app | ||
|
||
COPY package.json /usr/src/app/ | ||
RUN npm install --production | ||
|
||
# Copy everything else | ||
COPY . /usr/src/app | ||
|
||
# Run the service | ||
CMD [ "npm", "start" ] |
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,24 @@ | ||
'use strict'; | ||
|
||
const config = require('config'); | ||
|
||
const rabbitmqConfig = config.get('scheduler').rabbitmq; | ||
const { protocol, username, password, host, port, exchange, exchangeType, vhost } = rabbitmqConfig; | ||
const amqpURI = `${protocol}://${username}:${password}@${host}:${port}${vhost}`; | ||
const schedulerMode = config.get('scheduler').enabled; | ||
|
||
/** | ||
* get configurations for rabbitmq | ||
* @method getConfig | ||
* @return {Object} | ||
*/ | ||
function getConfig() { | ||
return { | ||
schedulerMode, | ||
amqpURI, | ||
exchange, | ||
exchangeType | ||
}; | ||
} | ||
|
||
module.exports = { getConfig }; |
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,67 @@ | ||
'use strict'; | ||
|
||
const NodeResque = require('node-resque'); | ||
const { queuePrefix } = require('../config/redis'); | ||
const rabbitmqConf = require('../config/rabbitmq'); | ||
|
||
class Filter extends NodeResque.Plugin { | ||
/** | ||
* Construct a new Filter plugin | ||
* @method constructor | ||
*/ | ||
constructor(worker, func, queue, job, args, options) { | ||
super(worker, func, queue, job, args, options); | ||
|
||
this.name = 'Filter'; | ||
} | ||
|
||
/** | ||
* Checks if the job belongs to this queue-worker | ||
* If no, re-enqueue. | ||
* @method beforePerform | ||
* @return {Promise} | ||
*/ | ||
async beforePerform() { | ||
const { buildId } = this.args[0]; | ||
const buildConfig = await this.queueObject | ||
.connection.redis.hget(`${queuePrefix}buildConfigs`, buildId).then(JSON.parse); | ||
|
||
// if schedulerMode enabled, don't take anything without buildClusterName | ||
if (rabbitmqConf.getConfig().schedulerMode) { | ||
if (!buildConfig.buildClusterName) { | ||
await this.reEnqueue(); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
if (buildConfig.buildClusterName) { | ||
await this.reEnqueue(); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Re-enqueue job if it doesn't belong to this queue worker | ||
* @method reEnqueue | ||
* @return {Promise} | ||
*/ | ||
async reEnqueue() { | ||
await this.queueObject.enqueueIn(this.reenqueueTimeout(), this.queue, this.func, this.args); | ||
} | ||
|
||
reenqueueTimeout() { | ||
if (this.options.enqueueTimeout) { | ||
return this.options.enqueueTimeout; | ||
} | ||
|
||
return 1000; // in ms | ||
} | ||
} | ||
|
||
exports.Filter = Filter; |
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.