|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const {statSync, readFileSync} = require('fs'); |
| 4 | +const stripJsonComments = require('strip-json-comments'); |
| 5 | +const JsonLogger = require('kad-logger-json'); |
| 6 | +const {fork} = require('child_process'); |
| 7 | + |
| 8 | +/** Class representing a local RPC API's handlers */ |
| 9 | +class RPC { |
| 10 | + |
| 11 | + /** |
| 12 | + * Creates a environment to manage share processes |
| 13 | + * @param {Object} options |
| 14 | + * @param {Number} options.logVerbosity |
| 15 | + */ |
| 16 | + constructor(options) { |
| 17 | + this.logger = new JsonLogger(options.logVerbosity); |
| 18 | + this.shares = new Map(); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Logs the message by pushing it out the stream |
| 23 | + * @param {String} message |
| 24 | + * @param {String} level |
| 25 | + */ |
| 26 | + _log(msg, level='info') { |
| 27 | + this.logger[level](msg); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Starts a share process with the given configuration |
| 32 | + * @param {String} configPath |
| 33 | + * @param {RPC~startCallback} |
| 34 | + * @see https://storj.github.io/core/FarmerInterface.html |
| 35 | + */ |
| 36 | + start(configPath, callback) { |
| 37 | + this._log(`attempting to start share with config at path ${configPath}`); |
| 38 | + |
| 39 | + try { |
| 40 | + statSync(configPath); |
| 41 | + } catch (err) { |
| 42 | + return callback(new Error(`failed to read config at ${configPath}`)); |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + JSON.parse(stripJsonComments(readFileSync(configPath).toString())); |
| 47 | + } catch (err) { |
| 48 | + return callback(new Error(`failed to parse config at ${configPath}`)); |
| 49 | + } |
| 50 | + |
| 51 | + // TODO: Fork the actual farmer process, passing it the configuration |
| 52 | + // TODO: Pipe the stdio to the configured log file |
| 53 | + // TODO: Listen for state changes to update the shares record |
| 54 | + // |
| 55 | + |
| 56 | + callback(null); |
| 57 | + } |
| 58 | + /** |
| 59 | + * @callback RPC~startCallback |
| 60 | + * @param {Error|null} error |
| 61 | + */ |
| 62 | + |
| 63 | + /** |
| 64 | + * Stops the share process for the given node ID |
| 65 | + * @param {String} nodeId |
| 66 | + * @param {RPC~stopCallback} |
| 67 | + */ |
| 68 | + stop(nodeId, callback) { |
| 69 | + this._log(`attempting to stop share with node id ${nodeId}`); |
| 70 | + |
| 71 | + if (!this.shares.has(nodeId) || !this.shares.get(nodeId).process) { |
| 72 | + return callback(new Error(`share ${nodeId} is not running`)); |
| 73 | + } |
| 74 | + |
| 75 | + this.shares.get(nodeId).process.kill('SIGINT'); |
| 76 | + callback(null); |
| 77 | + } |
| 78 | + /** |
| 79 | + * @callback RPC~stopCallback |
| 80 | + * @param {Error|null} error |
| 81 | + */ |
| 82 | + |
| 83 | + /** |
| 84 | + * Restarts the share process for the given node ID |
| 85 | + * @param {String} nodeId |
| 86 | + * @param {RPC~restartCallback} |
| 87 | + */ |
| 88 | + restart(nodeId, callback) { |
| 89 | + this._log(`attempting to restart share with node id ${nodeId}`); |
| 90 | + this.stop(nodeId, (err) => { |
| 91 | + if (err) { |
| 92 | + return callback(err); |
| 93 | + } |
| 94 | + |
| 95 | + this.start(this.shares.get(nodeId).config, callback); |
| 96 | + }); |
| 97 | + } |
| 98 | + /** |
| 99 | + * @callback RPC~restartCallback |
| 100 | + * @param {Error|null} error |
| 101 | + */ |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns status information about the running shares |
| 105 | + * @param {RPC~statusCallback} |
| 106 | + */ |
| 107 | + status(callback) { |
| 108 | + this._log(`got status query`); |
| 109 | + |
| 110 | + let statuses = []; |
| 111 | + |
| 112 | + this.shares.forEach((share, nodeId) => { |
| 113 | + statuses.push({ |
| 114 | + nodeId: nodeId, |
| 115 | + parsedConfig: share.config, |
| 116 | + readyState: share.process ? 1 : 0 |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + callback(null, statuses); |
| 121 | + } |
| 122 | + /** |
| 123 | + * @callback RPC~statusCallback |
| 124 | + * @param {Error|null} error |
| 125 | + * @param {Object} status |
| 126 | + */ |
| 127 | + |
| 128 | + /** |
| 129 | + * Simply kills the daemon and all managed proccesses |
| 130 | + */ |
| 131 | + killall() { |
| 132 | + this._log(`received kill signal, destroying running shares`); |
| 133 | + |
| 134 | + for (let nodeId of this.shares.keys()) { |
| 135 | + this.destroy(nodeId, () => null); |
| 136 | + } |
| 137 | + |
| 138 | + process.exit(0); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Kills the share with the given node ID |
| 143 | + * @param {String} nodeId |
| 144 | + * @param {RPC~destroyCallback} |
| 145 | + */ |
| 146 | + destroy(nodeId, callback) { |
| 147 | + this._log(`received destroy command for ${nodeId}`); |
| 148 | + |
| 149 | + if (!this.shares.has(nodeId)) { |
| 150 | + return callback(new Error(`share ${nodeId} is not running`)); |
| 151 | + } |
| 152 | + |
| 153 | + share.process.kill('SIGINT'); |
| 154 | + this.shares.delete(nodeId); |
| 155 | + callback(null); |
| 156 | + } |
| 157 | + /** |
| 158 | + * @callback RPC~destroyCallback |
| 159 | + * @param {Error|null} error |
| 160 | + */ |
| 161 | + |
| 162 | + get methods() { |
| 163 | + return { |
| 164 | + start: this.start.bind(this), |
| 165 | + stop: this.stop.bind(this), |
| 166 | + restart: this.restart.bind(this), |
| 167 | + status: this.status.bind(this), |
| 168 | + killall: this.killall.bind(this), |
| 169 | + destroy: this.destroy.bind(this) |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | +} |
| 174 | + |
| 175 | +module.exports = RPC; |
0 commit comments