Skip to content

Commit

Permalink
core/local: SyncDir helpers (#1272)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sebn committed Nov 13, 2018
1 parent 59ca4e6 commit 6be2c33
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
12 changes: 0 additions & 12 deletions core/local/atom_watcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* @flow */

const fse = require('fs-extra')
const Promise = require('bluebird')

const checksumer = require('./checksumer')
Expand All @@ -26,7 +25,6 @@ module.exports = class AtomWatcher {
syncPath: string
events: EventEmitter
checksumer: Checksumer
ensureDirInterval: *
running: Promise<void>
_runningResolve: ?Function
_runningReject: ?Function
Expand All @@ -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
Expand All @@ -72,7 +61,6 @@ module.exports = class AtomWatcher {
this._runningResolve()
this._runningResolve = null
}
clearInterval(this.ensureDirInterval)
this.source.stop()
}
}
15 changes: 2 additions & 13 deletions core/local/chokidar_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -60,7 +61,6 @@ module.exports = class LocalWatcher {
checksumer: Checksumer
watcher: any // chokidar
buffer: LocalEventBuffer<ChokidarEvent>
ensureDirInterval: *
pendingChanges: LocalChange[]
running: Promise<void>
_runningResolve: ?Function
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion core/local/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -43,6 +44,7 @@ module.exports = class Local /*:: implements Side */ {
pouch: Pouch
events: EventEmitter
syncPath: string
syncDirCheckInterval: IntervalID
tmpPath: string
watcher: Watcher
other: FileStreamProvider
Expand Down Expand Up @@ -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()
}

Expand Down
32 changes: 32 additions & 0 deletions core/local/sync_dir.js
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 0 additions & 1 deletion core/local/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export interface Watcher {
running: Promise<*>,
start (): Promise<*>,
stop (force: ?bool): Promise<*>,
ensureDirSync (): void,
}
*/

Expand Down

0 comments on commit 6be2c33

Please sign in to comment.