Repository: Pylons/pyramid

Issue:
pserve --reload with --daemon
I'm not sure if this is supposed to work, but tizoc in #pyramid claimed it may be a problem. If this is supposed to work we should verify that it does. If it is not supposed to work (which I think would be fine) we should add an error message when trying to execute `pserve` with both options enabled.



Discussion:
It is not supposed to work AFAIK.

https://github.com/mmerickel/pyramid/compare/fix.pserve-monitor-daemon

Contains a fix adding support for the full blown combination of `--reload --monitor-restart --daemon`.

Would this also solve the problem if the reload fails due to a Python exception? Right now I have `pserve` for development sitting in a loop with `--reload` so that if it fails, it `sleep 10` and retries. This way I don't need to go restart it when I save a file and miss a comma or something along those lines.

This does allow for that. Although it attempts to restart with no delay, so we'd probably want to add a throttle to the `--monitor-restart` option or combine the logic such that we "don't restart until a file changes". A more ideal implementation would be something like https://pypi.python.org/pypi/ReloadWSGI which does not crash when your application fails to start, but continues to serve the old version. I had issues using ReloadWSGI on OS X last time I tried, and I think that it has no windows support at all.

At least in my case, I have no need for it to continue serving the old version, but having it wait till a file has changed after failure would probably be best.

Slightly related (getting daemon functionality to be slightly more managable)
The pserve daemon feature needs some clean up, particularly when checking for a PID (which should be done early on). I'm not sure of the benefits of read_pidfile and live_pidfile being seperate functions? It seems to add some complexity (only care about a valid PID). 
But this will break some test scripts that check for 1) The pid file 2) That the file contains a value 3) That the value is a valid PID.
Is the pserve daemon functionality and the subcommands widely used? {start|stop|restart|status}

From what I see --reload does not work with --daemon in Pyramid 1.4.5
What's preventing @mmerickel's fix from getting merged?

My $.02: I'd rather get rid of `--daemon` and `--stop-daemon` than trying to make `pserve` a full-blown process manager or add complexity to make daemon-stopping work with `--monitor-restart`.  Very good process managers already exist and pserve doesn't really need to do their jobs.

That said, if @mmerickel wants to merge the patch, I'd be fine with it.

I'm a big fan of removing `--daemon` but it may be controversial.

Side note: my fix is something I would not consider production ready due to the infinite loop caused by saving a file with broken code while `--monitor-restart` is active.

I'd be all for removing `--daemon`. I never understood why we try to be everything with `pserve`. Then again, I run on top of `uWSGI`.


Resolving patch:
diff --git a/pyramid/scripts/pserve.py b/pyramid/scripts/pserve.py
--- a/pyramid/scripts/pserve.py
+++ b/pyramid/scripts/pserve.py
@@ -225,6 +225,10 @@ def run(self):  # pragma: no cover
             cmd = None
 
         if self.options.reload:
+            if self.options.daemon or cmd in ('start', 'stop', 'restart'):
+                self.out(
+                    'Error: Cannot use reloading while running as a dameon.')
+                return 2
             if os.environ.get(self._reloader_environ_key):
                 if self.options.verbose > 1:
                     self.out('Running reloading file monitor')


Test patch: