This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
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 #41 from gw2efficiency/refactor-background-job-queue
Refactor jobs into background queues & use configuration files
- Loading branch information
Showing
75 changed files
with
1,013 additions
and
515 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 |
---|---|---|
|
@@ -3,4 +3,4 @@ node_modules/ | |
.idea/ | ||
build/ | ||
.DS_Store | ||
newrelic.js | ||
config/environment.production.js |
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,64 @@ | ||
require('babel-polyfill') | ||
const commander = require('commander') | ||
const mongo = require('../helpers/mongo.js') | ||
const createJob = require('../helpers/createJob.js') | ||
const chalk = require('chalk') | ||
const jobList = require('../config/jobs.js') | ||
|
||
// Fire off the cli | ||
let jobName = false | ||
commander | ||
.arguments('<job>') | ||
.option('-q, --queue', 'Queue the job (by default jobs get executed directly)') | ||
.action((job) => jobName = job) | ||
.parse(process.argv) | ||
|
||
// Check if the job name is valid | ||
let jobNames = jobList.map(j => j.name) | ||
if (!jobName || jobNames.indexOf(jobName) === -1) { | ||
jobNames = jobNames.map(j => ` - ${j}`).join('\n') | ||
console.log(chalk.bold.red(`You have to specify a valid job name:\n${jobNames}`)) | ||
process.exit() | ||
} | ||
|
||
let job = jobList.find(j => j.name === jobName) | ||
let verb = commander.queue ? 'Queueing' : 'Executing' | ||
console.log(chalk.green(`${verb} job "${job.data.title}" [${job.name}]`)) | ||
|
||
// Mock the "done" functionality that jobs expect and | ||
// log all results or errors out to the user | ||
function doneMock (err, result) { | ||
if (err) { | ||
console.log(chalk.bold.red(`An error occurred in the job:\n${err}`)) | ||
process.exit() | ||
} | ||
|
||
let output = result | ||
? `Job finished successfully:\n${result}` | ||
: 'Job finished successfully' | ||
console.log(chalk.green(output)) | ||
process.exit() | ||
} | ||
|
||
// Queue the job and wait for it to be processed by the workers | ||
if (commander.queue) { | ||
createJob({ | ||
name: job.name, | ||
data: {title: `[CLI] ${job.data.title}`}, | ||
priority: 'high', | ||
callback: doneMock | ||
}) | ||
|
||
console.log(chalk.gray('Waiting till the queued job gets processed...')) | ||
} | ||
|
||
// Execute the job in this process | ||
if (!commander.queue) { | ||
const jobFunction = require(job.path) | ||
let jobMock = {log: console.log} | ||
|
||
mongo.connect().then(() => { | ||
jobFunction(jobMock, doneMock) | ||
.catch(err => console.log(chalk.bold.red(`An error occurred in the job:\n${err}`))) | ||
}) | ||
} |
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,13 @@ | ||
const kue = require('../helpers/kue.js') | ||
const express = require('express') | ||
const auth = require('basic-auth-connect') | ||
const logger = require('../helpers/logger.js') | ||
const config = require('../config/application.js') | ||
|
||
const app = express() | ||
kue.createQueue() | ||
app.use(auth(config.kue.username, config.kue.password)) | ||
app.use('/', kue.app) | ||
app.listen(config.kue.port, () => logger.info(`Server listening on port ${config.kue.port}`)) | ||
|
||
module.exports = app |
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
// This process just handles the scheduling of background jobs. | ||
// It pushes the tasks into a priority queue to be picked up by the worker processes. | ||
|
||
const createJob = require('../helpers/createJob.js') | ||
const jobList = require('../config/jobs.js') | ||
|
||
jobList | ||
.filter(job => job.schedule) | ||
.map(job => createJob(job)) | ||
|
||
module.exports = jobList |
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,9 +1,15 @@ | ||
require('babel-polyfill') | ||
const kue = require('../helpers/kue.js') | ||
const queue = kue.createQueue() | ||
const mongo = require('../helpers/mongo.js') | ||
const schedule = require('../helpers/worker.js') | ||
const scheduledTasks = require('../config/schedule.js') | ||
const wrapJob = require('../helpers/wrapJob.js') | ||
const jobList = require('../config/jobs.js') | ||
|
||
// Connect to the database and start the scheduling | ||
mongo.connect().then(() => { | ||
scheduledTasks.map(task => schedule(task[0], task[1])) | ||
jobList.map(job => { | ||
let jobFunction = require(job.path) | ||
queue.process(job.name, wrapJob(jobFunction)) | ||
}) | ||
|
||
console.log(`${jobList.length} jobs loaded, waiting for queued entries`) | ||
}) |
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,10 @@ | ||
// This is the file that the application loads, which then in turn | ||
// loads the environment configuration for the specific NODE_ENV | ||
|
||
const path = process.env.NODE_ENV === 'production' | ||
? '../config/environment.production.js' | ||
: '../config/environment.js' | ||
|
||
const configuration = require(path) | ||
|
||
module.exports = configuration |
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,37 @@ | ||
const environment = { | ||
server: { | ||
// port of the api server | ||
port: 8080, | ||
// languages to support and the default language (needs to be in the languages) | ||
languages: ['de', 'en', 'fr', 'es'], | ||
defaultLanguage: 'en' | ||
}, | ||
kue: { | ||
// port, username and password of the job monitoring server | ||
port: 4000, | ||
username: 'username', | ||
password: 'password', | ||
// redis prefix for queued jobs | ||
prefix: 'kue-' | ||
}, | ||
mongo: { | ||
// mongodb connection url | ||
url: 'mongodb://127.0.0.1:27017/gw2api' | ||
}, | ||
redis: { | ||
// redis connection settings, you can see all options here: | ||
// https://github.com/NodeRedis/node_redis#options-object-properties | ||
port: 6379, | ||
host: '127.0.0.1' | ||
}, | ||
keymetrics: { | ||
// enable/disable logging http requests to http://keymetrics.io | ||
logging: false | ||
}, | ||
gw2api: { | ||
// retries for failed api requests (to the official api) | ||
retries: 5 | ||
} | ||
} | ||
|
||
module.exports = environment |
Oops, something went wrong.