Skip to content

Commit 9454ed1

Browse files
authored
Merge pull request #1 from dadi/refactor/test-suite
Refactor for testability
2 parents 9f9bd15 + adbf068 commit 9454ed1

27 files changed

+1189
-264
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
config.*json
1+
.DS_Store
2+
config.*json
3+
log/*
4+
*.log
5+
node_modules/
6+
coverage/
7+
docs/

.travis.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
language: node_js
2+
cache:
3+
directories:
4+
- node_modules
5+
notifications:
6+
email: false
7+
node_js:
8+
- '6'
9+
- '5'
10+
- '4'
11+
before_script:
12+
- npm prune
13+
after_success:
14+
- npm run semantic-release
15+
branches:
16+
except:
17+
- /^v\d+\.\d+\.\d+$/
18+
env:
19+
- CXX=g++-4.8
20+
addons:
21+
apt:
22+
sources:
23+
- ubuntu-toolchain-r-test
24+
packages:
25+
- g++-4.8
26+
services:
27+
- redis-server
28+
before_install: if [[ `npm -v` != 3* ]]; then npm i -g npm@latest; fi

config.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
const convict = require('convict')
2+
const fs = require('fs')
3+
const mkdirp = require('mkdirp')
4+
const path = require('path')
5+
6+
const conf = convict({
7+
env: {
8+
doc: 'The applicaton environment',
9+
format: ['production', 'development', 'test'],
10+
default: 'development',
11+
env: 'NODE_ENV'
12+
},
13+
queue: {
14+
host: {
15+
doc: 'The queue server host IP',
16+
format: 'ipaddress',
17+
default: '127.0.0.1'
18+
},
19+
port: {
20+
doc: 'The queue server port number',
21+
format: 'port',
22+
default: 6379
23+
}
24+
},
25+
broker: {
26+
queue: {
27+
doc: 'The queue name',
28+
format: String,
29+
default: '',
30+
arg: 'queue'
31+
},
32+
interval: {
33+
doc: 'The polling intervals, in seconds',
34+
format: Array,
35+
default: [ 0, 1, 5, 10 ]
36+
},
37+
retries: {
38+
doc: 'The number of times a message will be retried after failing',
39+
format: Number,
40+
default: 10
41+
},
42+
timeout: {
43+
doc: 'The number of seconds until a message is placed back on the queue',
44+
format: Number,
45+
default: 30
46+
},
47+
throttle: {
48+
doc: 'The number of workers that should execute concurrently',
49+
format: Number,
50+
default: 5
51+
}
52+
},
53+
workers: {
54+
path: {
55+
doc: 'The absolute or relative path to the directory for worker modules',
56+
format: String,
57+
default: './workers'
58+
}
59+
},
60+
logging: {
61+
enabled: {
62+
doc: 'Enable or disable logging',
63+
format: Boolean,
64+
default: false
65+
},
66+
level: {
67+
doc: 'The minimum error level to be logged',
68+
format: String,
69+
default: 'info'
70+
},
71+
path: {
72+
doc: 'The absolute or relative path to the directory for log files',
73+
format: String,
74+
default: './log'
75+
},
76+
filename: {
77+
doc: 'The name to use for the log file, without extension',
78+
format: String,
79+
default: 'error'
80+
},
81+
extension: {
82+
doc: 'The extension to use for the log file',
83+
format: String,
84+
default: 'log'
85+
},
86+
accessLog: {
87+
enabled: {
88+
doc: 'Enable or disable access logging',
89+
format: Boolean,
90+
default: false
91+
}
92+
}
93+
}
94+
})
95+
96+
function loadConfig () {
97+
const configPath = path.join(process.cwd(), 'config/config.development.json')
98+
const configSamplePath = path.join(__dirname, 'config/config.development.json.sample')
99+
const sampleConfig = fs.readFileSync(configSamplePath, { encoding: 'utf-8'})
100+
101+
try {
102+
var s = fs.readFileSync(configPath, { encoding: 'utf-8'})
103+
} catch (err) {
104+
if (err.code === 'ENOENT') {
105+
var made = mkdirp.sync(path.join(process.cwd(), 'config'))
106+
fs.writeFileSync(configPath, sampleConfig)
107+
console.log('\nCreated configuration file at ' + configPath + '\n')
108+
}
109+
} finally {
110+
const env = conf.get('env')
111+
conf.loadFile('config/config.' + env + '.json')
112+
conf.validate({ strict: true })
113+
}
114+
}
115+
116+
loadConfig()
117+
118+
module.exports = conf

config/config.development.json.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"enabled": true,
1818
"level": "info",
1919
"path": "./log",
20-
"filename": "error",
20+
"filename": "myQueue",
2121
"extension": "log",
2222
"accessLog": {
2323
"enabled": false

doc-config.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"tags": {
3+
"allowUnknownTags": true,
4+
"dictionaries": ["jsdoc","closure"]
5+
},
6+
"source": {
7+
"includePattern": ".+\\.js(doc)?$",
8+
"excludePattern": "(^|\\/|\\\\)_"
9+
},
10+
"plugins": [
11+
12+
],
13+
"templates": {
14+
"cleverLinks": false,
15+
"monospaceLinks": false,
16+
"systemName" : "DADI Queue",
17+
"footer" : "{string}",
18+
"copyright" : "{string}",
19+
"includeDate" : false,
20+
"navType" : "inline",
21+
"theme" : "journal",
22+
"linenums" : true,
23+
"collapseSymbols" : true,
24+
"inverseNav" : false,
25+
"outputSourceFiles" : true ,
26+
"outputSourcePath" : false,
27+
"syntaxTheme" : "default",
28+
"sort" : true
29+
}
30+
}

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
const app = require('./lib/app')
2-
module.exports = app()
1+
const QueueHandler = require('./lib/queue-handler')
2+
module.exports = new QueueHandler()

lib/app.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)