Ticket #823 enhancement new

Opened 9 years ago

Last modified 3 days ago

twistd should wait for the application to start when daemonizing

Reported by: spiv Owned by: therve
Priority: normal Milestone:
Component: core Keywords: twistd
Cc: exarkun, spiv, therve, zooko@… Branch: branches/twistd-wait-823-4
(diff, github, buildbot, log)
Author: exarkun, therve Launchpad Bug:

Description (last modified by therve) (diff)

Currently, twistd can start successfully (exit with code 0) even if the application fails when it daemonizes. It'd be good if it waited for the application to start, returning the appropriate exit code.

Change History

1

  Changed 9 years ago by spiv

It would be nice if twistd had features like:
   -w --wait   twistd will wait until all services in the app configuration have
               started before exiting.  --nodaemon conflicts with this option.
   -k --kill <pidfile>  stop a twisted app specified by <pidfile>

--wait would be useful for scripts (e.g. functional tests) that want to spawn a
twisted app, wait for it to be listening and ready, and then proceed.  --kill
would be convenient for tearing down those tests, and probably in other places.

--wait could be implemented by opening a pipe to the child before forking, and
getting the child application to write to the pipe (or close it?) to signal when
it has started all its services.

--kill could be implemented on POSIX rather stupidly by doing:
    while True:
        try:
            os.kill(pid, 0)
        except OSError:
            break

--wait is really the one I want, though.

2

  Changed 6 years ago by exarkun

  • keywords core removed
  • component set to core

Shouldn't --wait just be the default? I don't ever want to not know when the twistd command I ran didn't actually do what I wanted it to do.

3

  Changed 5 years ago by therve

  • cc therve added

4

  Changed 5 years ago by therve

  • owner changed from spiv to therve

5

  Changed 5 years ago by therve

  • branch set to branches/twistd-wait-823
  • author set to therve

(In [22480]) Branching to 'twistd-wait-823'

6

  Changed 5 years ago by therve

(In [22481]) Throw this hack somewhere: it waits for the start of a daemonized twistd process, and report its status.

Refs #823

7

  Changed 5 years ago by therve

  • branch changed from branches/twistd-wait-823 to branches/twistd-wait-823-2

(In [23470]) Branching to 'twistd-wait-823-2'

8

  Changed 5 years ago by therve

(In [23471]) Reimplement the wait slightly differently

Refs #823

9

  Changed 3 years ago by zooko

 http://tahoe-lafs.org/trac/tahoe-lafs/ticket/71 is a ticket for the Tahoe-LAFS project which I think would be fixed by using the --wait option if twistd supports that option.

10

  Changed 3 years ago by zooko

  • cc zooko@… added

11

  Changed 3 years ago by exarkun

  • keywords twistd added

12

  Changed 3 years ago by zooko

  • cc zooko@… removed
  • summary changed from Make twistd a little more scripting friendly to add --wait and --kill to twistd (Make twistd a little more scripting friendly)

13

  Changed 3 years ago by zooko

  • cc zooko@… added

14

  Changed 3 years ago by zooko

There is discussion in the Tahoe-LAFS project about whether to adapt therve's idea into Tahoe-LAFS source code base and/or to contribute such patches to this ticket:  http://tahoe-lafs.org/trac/tahoe-lafs/ticket/71#comment:24

15

  Changed 3 years ago by zooko

  • keywords tests-needed added

jml suggested on IRC that this ticket might be ready for review. I looked at the patch and it is missing tests. The patch adds the functionality of having the parent wait until the child process has started and has signalled success or failure to the parent through a pipe. To test that added functionality, we might extend test_daemonize().

The tests could check whether the parent correctly exits with 0 when the child's startApplication() returns successfully, and whether the parent correctly exits with non-zero when the child's startApplication() raises an exception. I think that would be sufficient testing for this patch.

In order to do this the new test_daemonize() would have to not patch out the daemonization (as currently done in UnixApplicationRunnerSetupEnvironmentTests.setUp(). Instead it could probably patch out os._exit().

I think that would be sufficient testing for this patch.

16

  Changed 3 years ago by exarkun

  • branch changed from branches/twistd-wait-823-2 to branches/twistd-wait-823-3
  • branch_author changed from therve to exarkun, therve

(In [29952]) Branching to 'twistd-wait-823-3'

17

  Changed 2 years ago by <automation>

  • owner therve deleted

18

  Changed 3 months ago by therve

  • branch changed from branches/twistd-wait-823-3 to branches/twistd-wait-823-4

(In [37525]) Branching to 'twistd-wait-823-4'

19

  Changed 3 months ago by therve

  • owner set to therve

20

  Changed 3 months ago by therve

  • owner therve deleted
  • keywords review added; tests-needed removed

Let's do this!

21

  Changed 3 months ago by jerub

  • keywords review removed
  • owner set to therve

Some small comments:

The soup nazi seinfeld sketch is no longer funny. Please update this message to be something like "Daemonization tests are only run on unix platforms when twisted.scripts._twistd_unix is available"

if _twistd_unix is None:
    DaemonizeTests.skip = "No daemonize for you"

The alignment in protocols/test/test_basic.py is wrong for these lines:

                          [b'produce', b'hello world', b'unproduce',
                            b'goodbye'])

Please fix.

I really don't like flags with names that start with 'no', and the not optionsnowait? parameter illustrates why. Can you think of a better name for this option so that we don't have to employ double negatives?

22

follow-up: ↓ 24   Changed 3 months ago by jerub

Okay, I did a poor review: the code I looked at was a bad diff.

Test run is available here.

23

follow-up: ↓ 25   Changed 2 months ago by therve

  • keywords review added
  • owner therve deleted

OK, back to review. Unfortunately I haven't found a better name than no-wait, still opened to suggestions though.

24

in reply to: ↑ 22   Changed 4 weeks ago by glyph

Replying to jerub:

Test run is available here.

FYI: "here" isn't a link.

25

in reply to: ↑ 23   Changed 4 weeks ago by glyph

Replying to therve:

OK, back to review. Unfortunately I haven't found a better name than no-wait, still opened to suggestions though.

--rush? --hurry? :-)

26

  Changed 4 weeks ago by glyph

27

  Changed 3 days ago by tom.prince

If I were to use this in an application, I'd ideally like

  1. any logs generated while starting up.
  2. an application hook to indicate when startup is complete. Since .startService isn't asynchronous, often an application won't be ready by the time it returns.

I've got some hackish code for buildbot that does it  here.

28

  Changed 3 days ago by radix

  • keywords review removed

Questions:

  1. Can you explain why --no-wait is necessary? I'd rather not have it unless there's a really compelling reason.
  2. Why limit the message to 100 bytes?

Code changes:

  1. You removed the 'os' parameter from daemonize, but the docstring still documents it.
  2. There are some conflicts with trunk

Discussion:

  1. I'm not super into monkeypatch-the-import; it's pretty sensitive to changes. But I guess we already do it elsewhere.
  2. the move of the daemonize method was hard to review.
  3. I notice that this branch doesn't implement --kill, but it's clear to me that --kill should be done in a separate ticket/branch, so that's cool.

Everything else about the change seems pretty reasonable, but I'm a little worried that there may be weird edge cases that this would break for. Oh well!

To address tom's point:

  1. I think logs would be pretty cool, but a notification that the user should look at the log file is pretty good for now.

29

  Changed 3 days ago by therve

  • owner set to therve

30

  Changed 3 days ago by therve

  • description modified (diff)
  • summary changed from add --wait and --kill to twistd (Make twistd a little more scripting friendly) to twistd should wait for the application to start when daemonizing

Tom: your second point is interesting, but really unrelated to anything happening in that ticket. It's already 9 years old, let's not add random feature requests.

Note: See TracTickets for help on using tickets.