Anniepoo/swiplwebtut

add handling of startup

Opened this issue · 0 comments

I have a web app which takes over a minute to initialise, reporting
various interesting stats and warnings; if successful, I call
http_daemon, using the output(File) option, but this is too late to
redirect msgs from init.

What is best practice here? If I start the daemon first, might requests
be (mis)handled before the app is ready?

Normally I'd not use --output=file, but use the logging facility of the
daemon manager (e.g., systemd). One way to deal with slow-starting
apps is to setup a temporary handler while you initialize the application.
That is what ClioPatria is doing while restoring the RDF database:

setup_call_cleanup(
    http_handler(root(.), busy_loading,
        [ priority(1000),
          hide_children(true),
          id(busy_loading),
          prefix
        ]),
    rdf_attach_store(QOptions, AfterLoad),
    http_delete_handler(id(busy_loading))).

Here, rdf_attach_store/2 is the slow call. The handler does:

busy_loading(_Request) :-
rdf_statistics(triples(Triples)),
( loading_done(Nth, Total)
-> Extra = [ '; ~D of ~D graphs.'-[Nth, Total] ]
; Extra = [ '.' ]
),
HTML = p([ 'This service is currently restoring its ',
'persistent database.', br([]),
'Loaded ~D triples'-[Triples]
| Extra
]),
throw(http_reply(unavailable(HTML))).

Cheers --- Jan