[#267] Resolve "add application module for DI"
Closes #267 (closed)
context
- in the old world, we passed around all the stateful resources in our app (eg,
db
,sock
) as the first argument to any function that needed them - this was easy enough to reason about, but clunky to use. you have to thread argumens all the way down the callstack through several layers that don't use the resource, and as we start to accumulate more stateful resources (a registry for metrics, etc.) this is quickly growing unweidly
- so... we want to steal a page from the stellar playbook and use a top-level mutable
application
module that:- contains instances of all stateful subcomponents of the app
- begins with all subcomponents null
- mutates each subcomponent to a running state on startup
- allows for mock subcomponents to be injected in any combination for testing purposes
- provides a default testing spec that defaults all subocomponents to empty instances
changes
- extract
api
andjobs
services fromregistrar
and eliminateregistrar.run
- move all stateful services (
db
,sock
,api
,jobs
,dispatcher
) into anapp
module, which lives inapp/index
- let this module live in
app/index
(and move app entrypoint toapp/run
-
app.run
intializes all services and exports its subcomponents for use in other modules-
app.run
also accepts overrides for all stateful services - services which must be cleaned up or return a reference to a running resource after starup (eg:
db
,sock
, andapi
are stored as fields onapp
. services that have "fire and forget" semantics (eg:job
anddispatcher
) are not stored as fields - all services that store a reference (and are stored as fields on
app
) must provide astop
method that shuts them down -
app.stop
callsstop
on all subcomponents
-
-
test.support.testApp
provides default implementations of stub services to be injected as overrides intoapp.run
- let this module live in
- payoff: do a big shitstorm of renaming to make all functions that used to require
db
orsock
as arguments now importapp
and useapp.db
orapp.sock
(wahoo!) - we also take the opportunity of this massive refactor to shuffle the filesystem a bit:
- eliminate the
services
directory and simply move all modules directly into theapp
directory - move
dispatcher/run
todispatcher/index
- eliminate the
- and to clean up our tooling a bit:
- run the app in daemon mode from both
make dev.up
andmake dev.restart
- use dot notation instead of kebab-case in
make test.lint.fix
- run the app in daemon mode from both
Edited by aguestuser