A behaviour for command handlers: evoq_command_handler #3
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Purpose (feature): a desk's command handler (
maybe_<command>) gets an evoq behaviour, so its contract is checked at compile time and the copied dispatch boilerplate lives once, in evoq.Refs macula-services/mcl-bookclub-phoenix#4
Why
No evoq behaviour fits a command handler today. The analysis is on macula-services/mcl-bookclub-phoenix#4 (comment 6791):
evoq_commandis the command's data contract,evoq_aggregateis the consistency boundary several desks share, andevoq_decisionappends to the DCB stream, not the aggregate's (src/evoq_decision.erl:39-44).guides/integration_actors.md:243-251).handle_from_map/2(mcl-bookclub, mcl-chess),handle_payload/2(mcl-kanban),handle_from_map/1with no state (macula-realm, mcl-whiteboard). About 200maybe_*modules each copy the sameevoq_command:new+evoq_command_router:dispatchblock, and a typo in the aggregate'sfun maybe_x:handle_from_map/2shows only when that command first runs.Ruling
DECIDED 2026-12-01 "Add evoq_command_handler (Recommended)", relayed by the Supervisor.
Plan
command_handlers/0), the library functions, the guide sections, the corpus glossary line.Done when
evoq_command_handleris in a released evoq minor version, with its guide sections (1.31.1 on hex.pm, tag v1.31.1 on56755af1, run 6159; 1.31.0 was tagged but never published){ok, [Event]}return (macula-services/mcl-corpus#13, main 90d9fe4)Design:
evoq_command_handler(for review before any code)Base: evoq main
bb1be23(1.30.8). Target: evoq 1.31.0, additive. No existing module changes behaviour, and a service that adopts nothing sees no difference.1. The behaviour
That is all a desk must export. Two callbacks:
command_module/0ties the desk to its command. The library reads the command type,new/1/from_map/1,validate/1andto_map/1from that module, so they are never written twice.handle/2is the one callback that holds the rule.The stateless desk (macula-realm's
maybe_admit_realm_member, mcl-whiteboard'smaybe_place_text) gets no separate callback. It implementshandle(_State, Cmd). The aggregate still owns the state, and a desk that does not need it ignores it. A second, optionalhandle/1would give the aggregate two ways to call a desk and the library two paths to test, and it would buy nothing.2. Library functions (in
evoq_command_handler)dispatch/1. It knows the stream id and the store options, and they are the service's, not evoq's. In Erlang:dispatch(Cmd) -> evoq_command_handler:dispatch(?MODULE, Cmd, ?OPTS#{aggregate_id => procure_book_v1:stream_id(Cmd)}).metadatadefaults to#{}. A service that stamps a timestamp, as mcl-bookclub does today, passes it. The library does not read the clock.check_handlers/1is a plain function that a service's own test calls, one line per aggregate. It catches thefun maybe_x:handle_from_map/2typo, which today surfaces only when the command first runs. It is not a CI gate.3. The optional aggregate callback
Like
state_module/0, evoq never calls it. Onlyroute/3andcheck_handlers/1read it. An aggregate keeps its lifecycle guard and routes after it:Elixir looks the same:
@behaviour :evoq_command_handler,@impl trueoncommand_module/0andhandle/2, and:evoq_command_handler.route(__MODULE__, state, payload). The map is keyed bycommand_type/0, never by module name: in Elixir the two differ (:procure_book_v1vsHostBookclub.ProcureBook.ProcureBookV1).4. What one desk loses (mcl-bookclub
maybe_procure_book.erl)handle_from_map/2andhandled/2(:12-19) go;execute/3does them.dispatch/1validate anddo_dispatch/1(:56-71) shrink to the one-liner above.handle/2(:24-46) stays as it is. It was already the rule.book_aggregate:execute/2(:22-33) loses one clause per command and gainscommand_handlers/0.Across both bookclubs: 8 desks × 2 repos and 4 aggregates × 2 repos.
5. Tests in evoq (EUnit, no store)
execute/3:from_map/1preferred overnew/1; a command module withoutfrom_map/1goes throughnew/1; a constructor's{error, _}comes back unchanged; the handler's{ok, [_]}and{error, _}come back unchanged.dispatch/3: avalidate/1refusal never reaches the router; the#evoq_command{}handed on carriescommand_type/0, the aggregate, the id, theto_map/1payload and the metadata. The router is the seam:meckonevoq_command_router:dispatch/2, as evoq's aggregate unit tests already do (meck 0.9.2 is in the test profile,rebar.config:33).route/3: unknown type →{error, unknown_command}; a known type reaches its handler.check_handlers/1: ok for a good map; it names a module that is not loaded, one with no behaviour, and one whosecommand_module():command_type()differs from its key.6. Docs
guides/aggregates.md: a section "Desks:evoq_command_handler" covering the behaviour,execute/3,route/3andcommand_handlers/0, with the guard-then-route example above.guides/integration_actors.md: the Complete Desk Example (:243-251) marksmaybe_initiate_venture.erl# Handler (evoq_command_handler). Line :93's listener calls the desk'sdispatch/1, unchanged.CHANGELOG.md: 1.31.0, added, optional.GLOSSARY.md(handler entry, :150-155): "A desk's command handler implementsevoq_command_handler:handle(State, Command) -> {ok, [Event]} | {error, Reason}", which also fixes the drifted{ok, Event}return. This is its own corpus commit, made after the evoq release.7. Order
~> 1.31and the 8 desks plus 4 aggregates. Then mcl-bookclub-phoenix, the same.Other services adopt later, desk by desk, and not in this package.
Open points for the reviewer
execute/3. It mirrors the aggregate'sexecute/2.handle_payload/3is the other candidate.check_handlers/1in the library or in each service's tests. I put it in the library so that the same check is not written N times.Review r1 (Uranus): FAIL, small. Three amendments to the design, written out here. Read them and say they are folded, and it is a PASS.
Checked against evoq main
bb1be23and mcl-bookclub's current desks and aggregates.Required
A1.
dispatch/3must hand the router every option it reads, not three.maps:with([store_id, adapter, consistency], Opts)drops options the router reads:middleware(evoq_command_router.erl:159,197);timeout,retry_attemptsandexpected_version(evoq_execution_context.erl:39-42).A service that passes its own middleware would lose it with no error.
Amend:
maps:without([aggregate, aggregate_id, metadata], Opts).dispatch_opts()with_ => term().timeout => 1234, reaches the mockedevoq_command_router:dispatch/2.A2. A
validate/1that returns{ok, Command1}must dispatchCommand1.The callback allows
ok | {ok, Command} | {error, Reason}(evoq_command.erl:31), and §2 step 1 handles only the error.Amend:
ok→Command;{ok, C1}→C1;{error, _}→ stop.to_map/1ofC1is what reaches the router.A3.
route/3keys oncommand_typein the payload.dispatch/3must put it there.An aggregate's
execute/2gets only#evoq_command.payload, theto_map/1output (evoq_aggregate.erl:298).command_typeis in it today only because the bookclubs'to_map/1writes it by convention (e.g.procure_book_v1.erl:68).Amend:
dispatch/3builds the payload as(CommandModule:to_map(Command))#{command_type => CommandModule:command_type()}.evoq_commandmust carry it.to_map/1leaves it out still routes.Open points
execute/3: keep the name. The aggregate'sexecute/2callsroute/3→execute/3, which reads straight through.handle_payload/3is one service's word.check_handlers/1in the library: yes. One note: read bothbehaviourandbehaviorfrommodule_info(attributes). Erlang accepts both spellings, and Elixir's@behaviourwritesbehaviour.What is right as it is
handle(_State, Cmd).command_handlers/0is optional and evoq never calls it.command_type/0values, not module names (the Elixir point).Design r1 amendments, folded (per Uranus's review, comment 6802)
The text of comment 6801 is unchanged. Where these points differ from it, they replace it.
A1.
dispatch/3passes on every router option.evoq_command_router:dispatch(EvoqCommand, maps:without([aggregate, aggregate_id, metadata], Opts)).middleware,timeout,retry_attemptsandexpected_versiontherefore reach the router.dispatch_opts()gains_ => term().timeout => 1234reaches the mockedevoq_command_router:dispatch/2.A2. A
validate/1that rewrites the command is honoured.ok→ dispatchCommand;{ok, C1}→ dispatchC1;{error, _}→ stop, and the router is never called.to_map/1ofC1.A3.
dispatch/3writescommand_typeinto the payload itself.(CommandModule:to_map(Command))#{command_type => CommandModule:command_type()}. It no longer relies on a service'sto_map/1convention.guides/aggregates.mdsays that a hand-built#evoq_command{}routed byroute/3must carrycommand_typein its payload.to_map/1leavescommand_typeout still routes.Open points, as answered:
execute/3keeps its name.check_handlers/1lives in the library and reads bothbehaviourandbehaviorfrommodule_info(attributes). Its tests include one module of each spelling.Done. evoq 1.31.1 on hex.pm (tag v1.31.1,
56755af1, run 6159). Adopted in mcl-bookclub (main 5ae794c8) and mcl-bookclub-phoenix (main ac1fd97c). Corpus: GLOSSARY + EVOQ_BEHAVIOURS (mcl-corpus 90d9fe4), FAQ (5787ff0). Follow-up already filed: #6 (check_handlers undef on a missing command module).