From 6be2c33985f57d6b301c11f2c2406b4b4421bc30 Mon Sep 17 00:00:00 2001 From: sebn Date: Tue, 13 Nov 2018 13:31:07 +0100 Subject: [PATCH] core/local: SyncDir helpers (#1272) core/local: Extract syncDir from Local & Watchers - DRY - 1 method less in Watcher interface & subclasses - Local doesn't need LocalWatcher anymore to ensure sync dir exists - AtomWatcher module doesn't use fs-extra anymore core/local: Manage syncDir interval check in Local - So we don't need to do it for each watcher subclass (DRY) - AtomWatcher doesn't need syncDir anymore. --- core/local/atom_watcher.js | 12 ------------ core/local/chokidar_watcher.js | 15 ++------------- core/local/index.js | 6 +++++- core/local/sync_dir.js | 32 ++++++++++++++++++++++++++++++++ core/local/watcher.js | 1 - 5 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 core/local/sync_dir.js diff --git a/core/local/atom_watcher.js b/core/local/atom_watcher.js index 753a05a07..aa254aaec 100644 --- a/core/local/atom_watcher.js +++ b/core/local/atom_watcher.js @@ -1,6 +1,5 @@ /* @flow */ -const fse = require('fs-extra') const Promise = require('bluebird') const checksumer = require('./checksumer') @@ -26,7 +25,6 @@ module.exports = class AtomWatcher { syncPath: string events: EventEmitter checksumer: Checksumer - ensureDirInterval: * running: Promise _runningResolve: ?Function _runningReject: ?Function @@ -46,17 +44,8 @@ module.exports = class AtomWatcher { this.source = new LinuxSource(syncPath, identifier) } - ensureDirSync () { - // This code is duplicated in local/index#start - if (!fse.existsSync(this.syncPath)) { - this.events.emit('syncdir-unlinked') - throw new Error('Syncdir has been unlinked') - } - } - start () { log.debug('starting...') - this.ensureDirInterval = setInterval(this.ensureDirSync.bind(this), 5000) this.running = new Promise((resolve, reject) => { this._runningResolve = resolve this._runningReject = reject @@ -72,7 +61,6 @@ module.exports = class AtomWatcher { this._runningResolve() this._runningResolve = null } - clearInterval(this.ensureDirInterval) this.source.stop() } } diff --git a/core/local/chokidar_watcher.js b/core/local/chokidar_watcher.js index d1930ce24..f82bfc9dd 100644 --- a/core/local/chokidar_watcher.js +++ b/core/local/chokidar_watcher.js @@ -14,6 +14,7 @@ const LocalEventBuffer = require('./event_buffer') const logger = require('../logger') const metadata = require('../metadata') const {sameDate, fromDate} = require('../timestamp') +const syncDir = require('./sync_dir') /*:: import type { Metadata } from '../metadata' @@ -60,7 +61,6 @@ module.exports = class LocalWatcher { checksumer: Checksumer watcher: any // chokidar buffer: LocalEventBuffer - ensureDirInterval: * pendingChanges: LocalChange[] running: Promise _runningResolve: ?Function @@ -90,21 +90,11 @@ module.exports = class LocalWatcher { }) } - ensureDirSync () { - // This code is duplicated in local/index#start - if (!fs.existsSync(this.syncPath)) { - this.events.emit('syncdir-unlinked') - throw new Error('Syncdir has been unlinked') - } - } - // Start chokidar, the filesystem watcher // https://github.com/paulmillr/chokidar start () { log.debug('Starting...') - this.ensureDirInterval = setInterval(this.ensureDirSync.bind(this), 5000) - this.watcher = chokidar.watch('.', { // Let paths in events be relative to this base path cwd: this.syncPath, @@ -174,7 +164,7 @@ module.exports = class LocalWatcher { if (this.initialScan) this.initialScan.flushed = true this.events.emit('buffering-end') - this.ensureDirSync() + syncDir.ensureExistsSync(this) this.events.emit('local-start') let events = rawEvents.filter((e) => e.path !== '') // @TODO handle root dir events @@ -374,7 +364,6 @@ module.exports = class LocalWatcher { this._runningResolve() this._runningResolve = null } - clearInterval(this.ensureDirInterval) this.buffer.switchMode('idle') if (force) return Promise.resolve() // Give some time for awaitWriteFinish events to be managed diff --git a/core/local/index.js b/core/local/index.js index 1ed33e09d..87715cdae 100644 --- a/core/local/index.js +++ b/core/local/index.js @@ -16,6 +16,7 @@ const sentry = require('../sentry') const watcher = require('./watcher') const measureTime = require('../perftools') const { withContentLength } = require('../file_stream_provider') +const syncDir = require('./sync_dir') bluebird.promisifyAll(fs) @@ -43,6 +44,7 @@ module.exports = class Local /*:: implements Side */ { pouch: Pouch events: EventEmitter syncPath: string + syncDirCheckInterval: IntervalID tmpPath: string watcher: Watcher other: FileStreamProvider @@ -73,12 +75,14 @@ module.exports = class Local /*:: implements Side */ { // Start initial replication + watching changes in live start () { - this.watcher.ensureDirSync() + syncDir.ensureExistsSync(this) + this.syncDirCheckInterval = syncDir.startIntervalCheck(this) return this.watcher.start() } // Stop watching the file system stop () { + clearInterval(this.syncDirCheckInterval) return this.watcher.stop() } diff --git a/core/local/sync_dir.js b/core/local/sync_dir.js new file mode 100644 index 000000000..1f88f270b --- /dev/null +++ b/core/local/sync_dir.js @@ -0,0 +1,32 @@ +/* @flow */ + +var fs = require('fs') + +/*:: +import type EventEmitter from 'events' +*/ + +module.exports = { + ensureExistsSync, + startIntervalCheck +} + +/** Make sure syncPath actually exists. + * + * In case it doesn't, emit 'syncdir-unlinked' and throws. + * Any other error occuring during the check will be thrown too. + */ +function ensureExistsSync ({syncPath, events} /*: {syncPath: string, events: EventEmitter} */) /*: void */ { + if (!fs.existsSync(syncPath)) { + events.emit('syncdir-unlinked') + throw new Error('Syncdir has been unlinked') + } +} + +/** Start regularly checking that syncPath actually exists. + * + * Caller should stop the regular check at some point with clearInterval(). + */ +function startIntervalCheck (context /*: {syncPath: string, events: EventEmitter} */) /*: IntervalID */ { + return setInterval(() => ensureExistsSync(context), 5000) +} diff --git a/core/local/watcher.js b/core/local/watcher.js index 5260f63df..9cfba7655 100644 --- a/core/local/watcher.js +++ b/core/local/watcher.js @@ -16,7 +16,6 @@ export interface Watcher { running: Promise<*>, start (): Promise<*>, stop (force: ?bool): Promise<*>, - ensureDirSync (): void, } */