From a4966a798a4599edee22e89452f461577131bb82 Mon Sep 17 00:00:00 2001 From: Praneeth Bodduluri Date: Thu, 27 Aug 2015 19:50:10 +0530 Subject: [PATCH] Change the fs functions to promise --- src/utils.coffee | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/utils.coffee b/src/utils.coffee index 3ba5bc79..6585579b 100644 --- a/src/utils.coffee +++ b/src/utils.coffee @@ -8,7 +8,7 @@ blink = require('blinking')(config.ledFile) url = require 'url' Inotify = require('inotify').Inotify inotify = new Inotify() -fs = require 'fs' +fs = Promise.promisifyAll require 'fs' utils = exports @@ -102,10 +102,10 @@ exports.disableCheck = (disable) -> # Call back for inotify triggered when the VPN status is changed. vpnStatusInotifyCallback = (arg) -> - try - stats = fs.lstatSync(config.vpnStatusPath+'/active') + fs.lstatAsync(config.vpnStatusPath+'/active') + .then -> pauseConnectivityCheck=true - catch error + .catch -> pauseConnectivityCheck=false vpn_status = @@ -113,19 +113,14 @@ vpn_status = watch_for: Inotify.IN_DELETE | Inotify.IN_CREATE callback: vpnStatusInotifyCallback -# Helper to create the vpn status path if it does not already exist -mkdirSync = (path) -> - try - fs.mkdirSync(path) - catch error - if error.code != 'EEXIST' - throw error - -mkdirSync(vpn_status.path) - exports.connectivityCheck = _.once -> parsedUrl = url.parse(config.apiEndpoint) - inotify.addWatch(vpn_status) + fs.mkdirAsync(vpn_status.path) + .then -> + inotify.addWatch(vpn_status) + .catch (error) -> + if error.code != 'EEXIST' + throw error # Manually trigger the call back to detect cases when VPN was switched on before the supervisor starts. vpnStatusInotifyCallback() customMonitor