Skip to content
Browse files

Touch ~/.pow/restart.txt to restart Pow

  • Loading branch information...
1 parent facf7aa commit 6f69a83b5ec1b99f8b85f411a7a1c1ca5859c108 @sstephenson sstephenson committed Jun 4, 2012
Showing with 81 additions and 3 deletions.
  1. +3 −0 lib/command.js
  2. +38 −1 lib/daemon.js
  3. +3 −1 src/command.coffee
  4. +21 −0 src/daemon.coffee
  5. +16 −1 test/test_daemon.coffee
View
3 lib/command.js
@@ -78,6 +78,9 @@
}
} else {
daemon = new Daemon(configuration);
+ daemon.on("restart", function() {
+ return process.exit();
+ });
return daemon.start();
}
});
View
39 lib/daemon.js
@@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.3.3
(function() {
- var Daemon, DnsServer, EventEmitter, HttpServer,
+ var Daemon, DnsServer, EventEmitter, HttpServer, fs, path,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
@@ -11,21 +11,58 @@
DnsServer = require("./dns_server");
+ fs = require("fs");
+
+ path = require("path");
+
module.exports = Daemon = (function(_super) {
__extends(Daemon, _super);
function Daemon(configuration) {
+ var hostRoot,
+ _this = this;
this.configuration = configuration;
this.stop = __bind(this.stop, this);
+ this.hostRootChanged = __bind(this.hostRootChanged, this);
+
this.httpServer = new HttpServer(this.configuration);
this.dnsServer = new DnsServer(this.configuration);
process.on("SIGINT", this.stop);
process.on("SIGTERM", this.stop);
process.on("SIGQUIT", this.stop);
+ hostRoot = this.configuration.hostRoot;
+ this.restartFilename = path.join(hostRoot, "restart.txt");
+ this.on("start", function() {
+ return _this.watcher = fs.watch(hostRoot, {
+ persistent: false
+ }, _this.hostRootChanged);
+ });
+ this.on("close", function() {
+ var _ref;
+ return (_ref = _this.watcher) != null ? _ref.close() : void 0;
+ });
}
+ Daemon.prototype.hostRootChanged = function() {
+ var _this = this;
+ return path.exists(this.restartFilename, function(exists) {
+ if (exists) {
+ return _this.restart();
+ }
+ });
+ };
+
+ Daemon.prototype.restart = function() {
+ var _this = this;
+ return fs.unlink(this.restartFilename, function(err) {
+ if (!err) {
+ return _this.emit("restart");
+ }
+ });
+ };
+
Daemon.prototype.start = function() {
var dnsPort, flunk, httpPort, pass, startServer, _ref,
_this = this;
View
4 src/command.coffee
@@ -78,7 +78,9 @@ Configuration.getUserConfiguration (err, configuration) ->
installer.install (err) ->
throw err if err
- # Start up the Pow daemon if no arguments were passed.
+ # Start up the Pow daemon if no arguments were passed. Terminate the
+ # process if the daemon requests a restart.
else
daemon = new Daemon configuration
+ daemon.on "restart", -> process.exit()
daemon.start()
View
21 src/daemon.coffee
@@ -4,6 +4,8 @@
{EventEmitter} = require "events"
HttpServer = require "./http_server"
DnsServer = require "./dns_server"
+fs = require "fs"
+path = require "path"
module.exports = class Daemon extends EventEmitter
# Create a new `Daemon` with the given `Configuration` instance.
@@ -17,6 +19,25 @@ module.exports = class Daemon extends EventEmitter
process.on "SIGTERM", @stop
process.on "SIGQUIT", @stop
+ # Watch for changes to the host root directory once the daemon has
+ # started. When the directory changes and the `restart.txt` file
+ # is present, remove it and emit a `restart` event.
+ hostRoot = @configuration.hostRoot
+ @restartFilename = path.join hostRoot, "restart.txt"
+ @on "start", => @watcher = fs.watch hostRoot, persistent: false, @hostRootChanged
+ @on "close", => @watcher?.close()
+
+ hostRootChanged: =>
+ path.exists @restartFilename, (exists) =>
+ @restart() if exists
+
+ # Remove the `~/.pow/restart.txt` file, if present, and emit a
+ # `restart` event. The `pow` command observes this event and
+ # terminates the process in response, causing Launch Services to
+ # restart the server.
+ restart: ->
+ fs.unlink @restartFilename, (err) =>
+ @emit "restart" unless err
# Start the daemon if it's stopped. The process goes like this:
#
View
17 test/test_daemon.coffee
@@ -1,7 +1,8 @@
net = require "net"
+path = require "path"
{testCase} = require "nodeunit"
{Configuration, Daemon} = require ".."
-{prepareFixtures, fixturePath} = require "./lib/test_helper"
+{prepareFixtures, fixturePath, touch} = require "./lib/test_helper"
module.exports = testCase
setUp: (proceed) ->
@@ -36,3 +37,17 @@ module.exports = testCase
test.ok !daemon.started
server.close()
test.done()
+
+ "touching restart.txt removes the file and emits a restart event": (test) ->
+ test.expect 1
+
+ restartFilename = path.join fixturePath("tmp"), "restart.txt"
+ configuration = new Configuration POW_HOST_ROOT: fixturePath("tmp"), POW_HTTP_PORT: 0, POW_DNS_PORT: 0
+ daemon = new Daemon configuration
+
+ daemon.start()
+ touch restartFilename, ->
+ daemon.once "restart", ->
+ path.exists restartFilename, (exists) ->
+ test.ok !exists
+ test.done()

0 comments on commit 6f69a83

Please sign in to comment.
Something went wrong with that request. Please try again.