Service which downloads and publishes messages from Zendesk as a stream.
Run sbt run. Head over to http://localhost:8080 to see the OpenAPI documenation.
The service can be roughly said to consist of the following components:
-
StreamStateRepoResponsibility: Persisting state of the stream. -
StreamApiResponsibility: Starting and stopping of a streams. Ensures that only 1 stream max per domain is running. Note that theStreamApiis not dependent on theStreamStateRepo. This is by design, to not have coupling between these components. The invariant of 1 stream max per domain is enforced using Software Transactional Memory (STM), which prevents race conditions when checking if a stream is already running. Tradeoffs: Arguably, it is possible to let theStreamApibe directly dependent on the persistence layer (StreamStateRepo). If the persistence layer is implemented using a transactional database, that could be used instead of STM to prevent race conditions. But as mentioned above, that introduces tight coupling between the persistence layer and the rest of the application, which might be unwanted. It also causes the updating of the state of the stream to be spread out over different places in the application, which could also be undesirable (now onlyDownloadManagerApiupdates the state of the stream).Currently, the
StreamApionly exposesstartandstopmethods, which behave statefull: ifstartwas already called before, the second invocation ofstartresults in an error. The advantage of this behaviour, is that is becomes easier to detect programmer mistakes (i.e. unintentional calls to start/stop). This is opposed to idempotent behaviour, where callingstartfirst stops any existing stream, then starts a new one. Ideally,StreamApishould expose methods for both behaviours: e.g.startandstopact statefull, while there could be a methodrestartwhich idempotently starts a stream. Onlystartandstopare implemented, and notrestartdue to time constraints. -
DownloadManagerApiResponsibility: Starting and stopping of streams, persisting their state as they run. Streams can also be added or removed. Removal can be seen as a permanent stop of a stream, where the stream can no longer resume from a previous offset when started/added again. Dependent onStreamStateRepoandStreamApi
By 'api' we mean the same as 'service' in DDD (we don't use the name 'service' to prevent the confusion with
ZIO's Service).
There are some improvements which could have been implemented given more time:
- Retrying of the requests to Zendesk. This can be achieved very easily at http client level. Since we are using using
https://sttp.softwaremill.com, it is done straightforwardly by creating a
RetryingBackend(https://sttp.softwaremill.com/en/v2.0.0-rc6/backends/custom.html) - Input sanitization of the http requests. Right now
startTimecould be negative,tokenempty etc. These can be sanitized using e.g. https://github.com/fthomas/refined - As mentioned above, endpoints to idempotently start new streams.
errorcould be part of theStreamState. I.e. when a stream fails, it is stopped but not due to a user command. This field would be optional and show the error status. A stream which was stopped due to an error, could be restarted like a regular stopped stream.- New types https://github.com/estatico/scala-newtype, so we don't mix up parameters being e.g.
String(liketokenanddomain).
- https://zio.dev/ effect management and streams
- https://http4s.org/ http server
- https://github.com/endpoints4s/endpoints4s http routes and OpenAPI derivation
- https://sttp.softwaremill.com wraps the http client to unified interface