Skip to content
This repository has been archived by the owner on May 15, 2019. It is now read-only.

Commit

Permalink
Fix/207 linux hot reloading (#223)
Browse files Browse the repository at this point in the history
* Prettier

* Fix(worker): Use chokidar for file watching fixes#207

* fix(worker): Re-added missing debug conditional
  • Loading branch information
sdbondi authored and jmdobry committed Nov 16, 2018
1 parent eb59d32 commit 9937c28
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"adm-zip": "^0.4.11",
"ajv": "^6.5.2",
"body-parser": "^1.18.3",
"chokidar": "^2.0.4",
"cli-table2": "0.2.0",
"colors": "^1.3.1",
"configstore": "^4.0.0",
Expand Down
37 changes: 20 additions & 17 deletions src/supervisor/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
const _ = require('lodash');
const bodyParser = require('body-parser');
const express = require('express');
const fs = require('fs');
const chokidar = require('chokidar');
const path = require('path');
const querystring = require('querystring');
const serializerr = require('serializerr');
Expand All @@ -40,13 +40,13 @@ const loadHandler = {
_originalLoader = Module._load;
Module._load = function (...args) {
const override = handler.onRequire(process.env['FUNCTION_NAME'], args[0]);
return (override || _originalLoader.apply(this, args));
return override || _originalLoader.apply(this, args);
};
}
};

function main () {
process.on('message', (message) => {
process.on('message', message => {
const name = message.name;
const cloudfunction = message.cloudfunction;
const localdir = getLocaldir(cloudfunction);
Expand All @@ -67,7 +67,9 @@ function main () {
console.log('Mock handler found. Require calls will be intercepted');
}
} catch (e) {
console.error('Mocks enabled but no mock handler found. Require calls will NOT be intercepted');
console.error(
'Mocks enabled but no mock handler found. Require calls will NOT be intercepted'
);
console.error(e);
}
}
Expand Down Expand Up @@ -155,7 +157,7 @@ function main () {
} else {
return Promise.resolve()
.then(() => handler(req.body))
.then((result) => {
.then(result => {
errback(null, result);
})
.catch(errback);
Expand All @@ -178,26 +180,27 @@ function main () {

// Only start watching for file changes if the funciton is not in debug mode
if (localdir && !message.debug && message.watch) {
fs.watch(localdir, {
recursive: true
}, (event, filename) => {
// Ignore node_modules
if (Array.isArray(message.watchIgnore)) {
for (let i = 0; i < message.watchIgnore.length; i++) {
if ((new RegExp(message.watchIgnore[i])).test(filename)) {
return;
}
}
}
const watcher = chokidar.watch(localdir, {
ignoreInitial: true,
persistent: false,
ignored: message.watchIgnore
});

const reloadServer = () => {
process.send({
close: true
});
server.close(() => {
console.log(`Worker for ${name} closed due to file changes.`);
process.exit();
});
});
};

watcher
.on('change', reloadServer)
.on('add', reloadServer)
.on('unlink', reloadServer)
.on('error', err => console.error(err));
}
});

Expand Down

0 comments on commit 9937c28

Please sign in to comment.