Skip to content

Add lifecycle callbacks #1184

Closed
Closed
@tobymurray-nanometrics

Description

Description

Looking at the (admittedly not super popular) questions on StackOverflow like this and this, I think it'd be handy to have some usable lifecycle callbacks for Sinatra. In particular, a callback when Sinatra is completely set up and a callback when Sinatra has completely stopped.

Use case

We use JRuby for a Rails application, but we also use Java code. The Ruby code calls out to a Java HTTP client, so when we're writing tests for that code we can't use any Ruby mocking libraries. Instead of an elaborate refactor whereby a mock Java HTTP client can be injected for testing, we use a number of tiny Sinatra servers that have very simple and predetermined responses to "mock" the third party servers. So far it has worked excellently, and allows us to keep our testing and production code very similar (mocks have bitten us by not failing in ways their production equivalents would).

To accomplish this, we've mashed together something like the linked answers above whereby we block until Sinatra has started. Unfortunately, there doesn't seem to be a similar solution for stop!, so rapidly starting and stopping Sinatra servers occasionally fails (client can't connect). My suspicion right now is that the previous Sinatra server has not fully stopped before the next one is attempting to start.

For us it'd be extremely useful to have an on_start and on_quit (with whatever names are appropriate for the lifecycle which would indicate Sinatra is completely up, running, and ready to go and completely stopped).

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions