Skip to content

Commit adbf068

Browse files
committed
fix: add automatic folder creation
1 parent e33f027 commit adbf068

File tree

9 files changed

+74
-33
lines changed

9 files changed

+74
-33
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.DS_Store
12
config.*json
23
log/*
34
*.log

config.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
var convict = require('convict')
1+
const convict = require('convict')
2+
const fs = require('fs')
3+
const mkdirp = require('mkdirp')
4+
const path = require('path')
25

3-
var conf = convict({
6+
const conf = convict({
47
env: {
58
doc: 'The applicaton environment',
69
format: ['production', 'development', 'test'],
@@ -90,8 +93,26 @@ var conf = convict({
9093
}
9194
})
9295

93-
var env = conf.get('env')
94-
conf.loadFile('config/config.' + env + '.json')
95-
conf.validate({ strict: true })
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()
96117

97118
module.exports = conf

lib/broker.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
/**
44
* @typedef req
55
* @type {Object}
6-
* @property {String} message - the string identifying the worker that can handle this request
7-
* @property {Array} address - x
8-
* @property {Object} data -
9-
* @property {Number} retries - x
10-
* @property {Date} timeout - timestamp representing when the message should expire
11-
* @property {Date} age - timestamp representing the age of the message
12-
* @property {Date} sent - timestamp representing when the message was sent
6+
* @property {String} message - The full message
7+
* @property {Array} address - The parts of the message containing its route
8+
* @property {Object} data - Any additional parts of the message for the worker to use
9+
* @property {Number} retries - The remaining number of times that this message will be retried before being deleted
10+
* @property {Date} timeout - A timestamp representing when this message will be placed back on the queue
11+
* @property {Date} age - A timestamp representing when this message was received by the broker
12+
* @property {Date} sent - A timestamp representing when the message was sent
1313
*/
1414

1515
const colors = require('colors') // eslint-disable-line

lib/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ExceededError extends CustomWarning {
5353
}
5454

5555
/**
56-
*
56+
* After a message's timeout value passes, the message will be unhidden from the queue and may be processed by other workers.
5757
*/
5858
class TimeoutError extends CustomWarning {
5959
constructor (id) {

lib/queue-handler.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const logger = require('./logger')
44
const Broker = require('./broker')
5-
const router = require('./router')()
5+
const Router = require('./router')
66

77
/**
88
* QueueHandler
@@ -14,18 +14,19 @@ const router = require('./router')()
1414
* workers via the router, and handles errors
1515
*/
1616
var QueueHandler = function () {
17+
this.router = new Router()
1718
this.queue = new Broker(this)
1819
}
1920

2021
/**
21-
*
22+
*
2223
*/
2324
QueueHandler.prototype.handle = function (err, req, done) {
2425
if (err) {
2526
var level = err.level || 'error'
2627
logger[level](err.toJSON(), err.toString())
2728
} else {
28-
router.route(req, this.queue, done)
29+
this.router.route(req, this.queue, done)
2930
}
3031
}
3132

lib/workers.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict'
22

3-
var fs = require('fs')
4-
var path = require('path')
3+
const config = require('../config')
4+
const fs = require('fs')
5+
const mkdirp = require('mkdirp')
6+
const path = require('path')
57

68
/**
79
* Workers
@@ -12,16 +14,25 @@ var path = require('path')
1214
* @classdesc Loads worker functions into an object literal
1315
*/
1416
var Workers = function () {
17+
this.workers = {}
18+
19+
// ensure the workers folder exists
20+
mkdirp(config.get('workers.path'), (err, made) => {
21+
if (err) {
22+
console.log(err)
23+
}
1524

25+
if (made) {
26+
console.log('\nCreated workers folder at ' + made + '\n')
27+
}
28+
})
1629
}
1730

1831
/**
1932
* @param {string} root - xx
2033
* @returns {Array} workers - xx
2134
*/
2235
Workers.prototype.load = function (root) {
23-
this.workers = {}
24-
2536
// files must be read before directories so that deeper levels can be
2637
// assigned as properties to the initial worker functions
2738
fs.readdirSync(root).forEach(fileName => this.loadPath(root, fileName, 'file'))

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"colors": "^1.1.2",
1616
"console-stamp": "^0.2.5",
1717
"convict": "^1.4.0",
18+
"mkdirp": "^0.5.1",
1819
"rsmq-worker": "^0.4.3"
1920
},
2021
"devDependencies": {

readme.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# DADI Queue
22

33
[![npm (scoped)](https://img.shields.io/npm/v/@dadi/queue.svg?maxAge=10800&style=flat-square)](https://www.npmjs.com/package/@dadi/queue)
4-
[![coverage](https://img.shields.io/badge/coverage-95%25-brightgreen.svg?style=flat-square)](https://github.com/dadi/queue)
4+
[![coverage](https://img.shields.io/badge/coverage-94%25-brightgreen.svg?style=flat-square)](https://github.com/dadi/queue)
55
[![Build Status](https://travis-ci.org/dadi/queue.svg?branch=master)](https://travis-ci.org/dadi/queue)
66
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)
77
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)
@@ -24,8 +24,8 @@ See the [Why do I need a task queue?](#why-do-i-need-a-task-queue) section for m
2424

2525
## Requirements
2626

27-
- Node
28-
- Note: RSMQ uses the Redis EVAL command (LUA scripts) so the minimum Redis version is 2.6+.
27+
- Node.js version 4.7 or greater
28+
- Redis version 2.6 or greater, due to RSMQ's use of the Redis EVAL command (LUA scripts)
2929

3030
## Installation
3131

@@ -35,23 +35,19 @@ See the [Why do I need a task queue?](#why-do-i-need-a-task-queue) section for m
3535

3636
2. Ensure you have a Redis server running and accessible
3737

38-
3. Ensure your project contains a *config* directory in the root, plus a *log* directory and a *workers* directory
38+
3. *config*, *log* and *workers* directories will be automatically created in the root of your project, if they don't already exist
3939

40-
4. Copy and rename the sample config file from the **@dadi/queue** module:
40+
4. Amend the configuration file *config/config.development.json* according to the following section
4141

42-
*config.development.json*
43-
44-
5. Amend the config file according to the following section
45-
46-
6. Require the **@dadi/queue** module from your project:
42+
5. Require the **@dadi/queue** module from your project:
4743

4844
`require('@dadi/queue')`
4945

50-
7. Run the project to start listening for messages:
46+
6. Run the project to start listening for messages:
5147

5248
`npm start`
5349

54-
## Config
50+
## Configuration
5551

5652
* **queue**
5753
* **host**: (*ipaddress; default = 127.0.0.1*) The queue server host IP
@@ -137,6 +133,16 @@ A worker should export a function that receives 3 parameters:
137133
* `queue` : An instance of the queue itself for sending further messages
138134
* `done`: A function to call when processing is complete
139135

136+
**An example worker**
137+
```js
138+
// ./workers/hello-world.js
139+
140+
module.exports = (req, queue, done) => {
141+
console.log('hello world')
142+
done()
143+
}
144+
```
145+
140146
### Success
141147

142148
On success, a worker should call `done()`, which will notify the broker to delete the message. This will also release the throttle if it is currently in operation.

scripts/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)