Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
Release v0.1.1
  • Loading branch information
adishegde committed Jul 28, 2018
2 parents 27c074e + c0830d9 commit 760e9d9
Show file tree
Hide file tree
Showing 17 changed files with 559 additions and 217 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "media_hub",
"license": "MIT",
"author": "Aditya Shridhar Hegde <[email protected]>",
"version": "0.1.0",
"version": "0.1.1",
"licence": "MIT",
"scripts": {
"precommit": "pretty-quick --staged",
Expand Down Expand Up @@ -31,6 +31,7 @@
"electron": "^2.0.3",
"electron-builder": "^20.24.4",
"electron-devtools-installer": "^2.2.4",
"electron-updater": "^3.0.3",
"eslint": "^4.19.1",
"file-loader": "^1.1.11",
"fuse.js": "^3.2.0",
Expand Down Expand Up @@ -60,8 +61,7 @@
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.4",
"webpack-node-externals": "^1.7.2",
"winston": "^3.0.0-rc5",
"electron-updater": "^3.0.3"
"winston": "^3.0.0-rc5"
},
"main": "app/index.js",
"description": "Tool for sharing files over LAN",
Expand Down
124 changes: 124 additions & 0 deletions src/app/daemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* This script starts the daemon and listens to messages by the main
* process. It is intended to be spawned as a child process */

import Level from "level";
import Winston from "winston";

import { addLogFile } from "core/utils/log";
import { daemonChannels as Dc } from "app/utils/constants";
import Server from "core/daemon/server";

const logger = Winston.loggers.get("daemon");

// Reference to server
let server;

// Reference to db
let db;

function restartDaemon({ config }) {
logger.debug("Daemon: Received restart message.");

let promises = [];

// Stop server if it's running
if (server) promises = server.stop();

// We accumulate all errors here so that it can be passed with the
// callback
let errors = [];

Promise.all(promises)
.catch(err => {
// We don't want this error to stop creation of new server. We
// catch it and continue with creation of new server.
errors.push(err.message);
logger.error(`Daemon.restart: Error while stopping server: ${err}`);
logger.debug(`${err.stack}`);
})
.then(() => {
// Create new server using app settings
// Passes existing db instance
server = new Server(db, config);
return Promise.all(Object.values(server.start()));
})
.catch(err => {
// we combine this with existing errors so that it can be
// passed with the finsih event
errors.push(err.message);
logger.error(`Daemon: Error while starting server: ${err}`);
logger.debug(`${err.stack}`);
})
.then(err => {
// if no errors have occured then don't send an array (which is a
// (truthy value)
if (errors.length === 0) errors = undefined;

// Signal the main process that server restart is over.
// Pass the array of errors which is somewhat similar to the
// nodejs pattern.
process.send({ type: Dc.RESTART, errors });
});
}

function init({ dbPath, logPath, logLevel = "error" }) {
let error;

try {
// Create db
// Once created here, it's not closed for the lifetime of the app
db = Level(dbPath, { valueEncoding: "json" });

// Add log file
addLogFile("daemon", logPath, logLevel);
} catch (err) {
// Capture error so that it can be sent with the finished event
error = err;
} finally {
process.send({ type: Dc.INIT, error });
}
}

function cleanup() {
logger.debug("Daemon: Received cleanup message.");

let promises = [];
let errors = [];

if (server) promises = Object.values(server.stop());

Promise.all(promises)
.catch(err => {
// An error in stopping server shouldn't affect the closing
// of db
errors.push(err.message);
logger.error(`Daemon: Error while stopping server: ${err}`);
logger.debug(`${err.stack}`);
})
.then(() => {
if (db) return db.close();
})
.catch(err => {
errors.push(err.message);
logger.error(`Daemon: Error while closing db: ${err}`);
logger.debug(`${err.stack}`);
})
.then(() => {
// If no error occured then send falsy value for errors
if (errors.length === 0) errors = undefined;

process.send({ type: Dc.CLEANUP, errors });
});
}

const mapTypeToHandler = {
[Dc.RESTART]: restartDaemon,
[Dc.INIT]: init,
[Dc.CLEANUP]: cleanup
};

process.on("message", mssg => {
if (mapTypeToHandler[mssg.type]) {
mapTypeToHandler[mssg.type](mssg.payload);
}
});
Loading

0 comments on commit 760e9d9

Please sign in to comment.