Skip to content

Commit

Permalink
Merge pull request #442 from dadi/feature/live-reload-domain-configs
Browse files Browse the repository at this point in the history
Reload domain configs when directory changes
  • Loading branch information
jimlambie committed Oct 15, 2018
2 parents ffa4ce4 + a5b41f5 commit a22068d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
18 changes: 16 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,21 @@ const Config = function () {
this.domainSchema = {}
this.createDomainSchema(schema, this.domainSchema)

this.loadDomainConfigs()
let domainsDirectory = this.get('multiDomain.directory')

// Watch the domains directory for new & removed domain configurations.
this.domainsWatcher = chokidar.watch(domainsDirectory, {
awaitWriteFinish: true,
depth: 1,
usePolling: true
}).on('addDir', (event, filePath) => {
this.loadDomainConfigs()
}).on('unlinkDir', (event, filePath) => {
// Wait 3 sec for the delete to finish before rescanning
setTimeout(() => {
this.loadDomainConfigs()
}, 3000)
})
}

Config.prototype = convict(schema)
Expand Down Expand Up @@ -738,8 +752,8 @@ Config.prototype.loadDomainConfigs = function () {
return {}
}

let configs = {}
let domainsDirectory = this.get('multiDomain.directory')
let configs = {}

domainManager
.scanDomains(domainsDirectory)
Expand Down
43 changes: 40 additions & 3 deletions test/acceptance/file-change-monitor.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
const domainManager = require('../../dadi/lib/models/domain-manager')
const exec = require('child_process').exec
const fs = require('fs')
const nock = require('nock')
const path = require('path')
const should = require('should')
const sinon = require('sinon')
const request = require('supertest')

const cache = require('./../../dadi/lib/cache')
const help = require('./help')
const app = require('./../../dadi/lib/')
const workspace = require('./../../dadi/lib/models/workspace')

let config = require('./../../config')
let configBackup = config.get()
let cdnUrl = `http://${config.get('server.host')}:${config.get('server.port')}`
let newDomainSubdirectory = path.join(__dirname, '../../domains/xxx-domain')
let testConfig

const cleanup = (dir, done) => {
exec(`rm -rf ${dir}`, (err, stdout, stderr) => {
if (err) console.log(err)
console.log(stdout, stderr)
done()
})
}

describe('File change monitor', function () {
this.timeout(15000)

describe('Config', () => {
before(done => {
testConfig = JSON.parse(
Expand Down Expand Up @@ -61,6 +71,33 @@ describe('File change monitor', function () {
done()
}, 1000)
})

it('should regenerate the configured domains when a new subdirectory is added', done => {
config.set('multiDomain.enabled', true)

// Get initial domain list
let domains = domainManager.getDomains()

// Make a new domain directory
fs.mkdir(newDomainSubdirectory, err => {
if (err) console.log(err)

setTimeout(() => {
// Get new domain list and compare with initial
domainManager.getDomains().length.should.be.above(domains.length)

// Remove new domain directory
cleanup(newDomainSubdirectory, () => {
// wait a while...
setTimeout(() => {
config.set('multiDomain.enabled', false)

done()
}, 6000)
})
}, 1000)
})
})
})

describe('Workspace', () => {
Expand Down

0 comments on commit a22068d

Please sign in to comment.