How It Works
This guide is an overview of how Serviceberry works internally. This knowledge is not a prerequisite to building Serviceberry services, but it could be helpful and may be of some interest.
Serviceberry can be broken down into three phases.
- Phase 1: You build your service tree and start your service using Serviceberry's API
- Phase 2: Serviceberry walks your service tree and builds a queue of handlers on each request
- Phase 3: Serviceberry calls your handlers one after another giving each control of the request
Phase 1
You calling Serviceberry
Phase 1 happens only once each time your service is started. It would include code such as requiring
Serviceberry, calling createTrunk([options])
and using the
trunk, branches, and leaves to build a tree.
Phase 2
A client calling Serviceberry
After phase 1, your service is listening for incoming requests from clients. Phase 2 happens once per request. When a request is made Serviceberry walks the service tree and builds a queue of handlers. None of your code executes in this phase. This work is done entirely by Serviceberry.
Phase 3
Serviceberry calling your handlers
Phase 3 begins immediately after phase 2 and likewise happens once per request. Serviceberry runs through the handlers queue and passes control of the request to each handler as it's called. The request ends when one of the handlers sends the response or Serviceberry reaches the end of the queue and implicitly sends the response using request.latestResult.
To learn about all the classes, properties, and methods available in the API, checkout the API Reference starting with the main Serviceberry object.