Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
Improved storage buckets watching
Browse files Browse the repository at this point in the history
  • Loading branch information
greyarch committed Sep 5, 2017
1 parent d09fc87 commit 897158a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
26 changes: 10 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
{
"name": "docker-dashboard-agent-compose",
"version": "3.4.6",
"version": "3.4.7",
"description": "",
"main": "index.js",
"scripts": {
"start": "./node_modules/.bin/forever --minUptime 2500 --spinSleepTime 5000 ./node_modules/.bin/coffee index.coffee",
"test": "mocha --recursive --compilers coffee:coffee-script/register --require coffee-coverage/register-istanbul tests && istanbul report text lcov",
"test:watch": "mocha --recursive --compilers coffee:coffee-script/register --watch-extensions test.coffee --watch tests ",
"start":
"./node_modules/.bin/forever --minUptime 2500 --spinSleepTime 5000 ./node_modules/.bin/coffee index.coffee",
"test":
"mocha --recursive --compilers coffee:coffee-script/register --require coffee-coverage/register-istanbul tests && istanbul report text lcov",
"test:watch":
"mocha --recursive --compilers coffee:coffee-script/register --watch-extensions test.coffee --watch tests ",
"precommit": "npm test",
"prepush": "npm test"
},
"author": "",
"license": "Apache-2.0",
"dependencies": {
"chokidar": "^1.7.0",
"coffee-script": "^1.12.4",
"docker-dashboard-agent-api": "^4.5.1",
"forever": "^0.15.2",
Expand All @@ -21,7 +25,6 @@
"lodash": "^4.15.0",
"mkdirp": "^0.5.1",
"mqtt": "^2.4.0",
"node-watch": "^0.5.5",
"os-monitor": "^1.0.5",
"path": "^0.12.7",
"random-mac": "0.0.4",
Expand Down
14 changes: 11 additions & 3 deletions src/coffee/storage.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fs = require 'fs-extra'
path = require 'path'
watch = require 'node-watch'
chokidar = require 'chokidar'

lib = require './storage/lib.coffee'

Expand All @@ -15,10 +15,18 @@ module.exports = (agent, mqtt, config) ->
lib.runPeriodically lib.publishDataStoreUsage(mqtt, '/agent/storage/size', config.dataDir)
lib.runPeriodically lib.publishDataStoreUsage(mqtt, '/agent/docker/graph', config.docker.graph.path)

lib.listStorageBuckets fs, basePath, publishStorageBuckets
watch basePath, { recursive: false }, (eventType, filename) ->
listBucketsCb = (fileName, fileStats) ->
console.log "#{fileName} changed. Listing storage buckets..."
lib.listStorageBuckets fs, basePath, publishStorageBuckets

listBucketsCb()
opts =
usePolling: true
ignored: path.join basePath, '/*/**'

['addDir', 'unlinkDir'].map (e) -> chokidar.watch(basePath, opts).on e, listBucketsCb
chokidar.watch(path.join(basePath, '/.*'), opts).on 'unlink', listBucketsCb

agent.on '/storage/list', (params, data, callback) ->
lib.listStorageBuckets fs, basePath, (err, buckets) ->
publishStorageBuckets err, buckets
Expand Down
12 changes: 8 additions & 4 deletions tests/coffee/storage.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ td = require 'testdouble'
fs = null
storageLib = null
storage = null
watch = null
chokidar = null

chokidarWatchOpts =
usePolling: true
ignored: "/rootDir/myDomain/*/**"

describe 'Storage', ->

beforeEach ->
fs = td.replace 'fs-extra'
watch = td.replace 'node-watch'
chokidar = td.replace 'chokidar'
storageLib = td.replace '../../src/coffee/storage/lib.coffee'
storage = require '../../src/coffee/storage.coffee'
td.when(chokidar.watch(td.matchers.contains("/rootDir/myDomain"), chokidarWatchOpts)).thenReturn
on: td.function('chokidar.watch.on')

afterEach -> td.reset()

Expand Down Expand Up @@ -128,8 +134,6 @@ describe 'Storage', ->
domain: 'myDomain'
docker: graph: path: '/docker/graph'
datastore: scanEnabled: true
td.verify watch '/rootDir/myDomain', {recursive: false}, captor.capture()
captor.value()
td.verify storageLib.listStorageBuckets argIsFs, '/rootDir/myDomain', captor.capture()
captor.value null, 'some-buckets'
td.verify mqtt.publish '/agent/storage/buckets', 'some-buckets'
Expand Down

0 comments on commit 897158a

Please sign in to comment.