Skip to content

[#267] Resolve "add application module for DI"

aguestuser requested to merge 267-add-application-module-for-di into master

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 and jobs services from registrar and eliminate registrar.run
  • move all stateful services (db, sock, api, jobs, dispatcher) into an app module, which lives in app/index
    • let this module live in app/index (and move app entrypoint to app/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, and api are stored as fields on app. services that have "fire and forget" semantics (eg: job and dispatcher) are not stored as fields
      • all services that store a reference (and are stored as fields on app) must provide a stop method that shuts them down
      • app.stop calls stop on all subcomponents
    • test.support.testApp provides default implementations of stub services to be injected as overrides into app.run
  • payoff: do a big shitstorm of renaming to make all functions that used to require db or sock as arguments now import app and use app.db or app.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 the app directory
    • move dispatcher/run to dispatcher/index
  • and to clean up our tooling a bit:
    • run the app in daemon mode from both make dev.up and make dev.restart
    • use dot notation instead of kebab-case in make test.lint.fix
Edited by aguestuser

Merge request reports

Loading