Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 58s
The motivating end-to-end demonstration (fed-sx-triggers-loop.md Phase 4): one trigger arriving in the pipeline drives a multi-step business flow with a branch, a timer suspension, an injected effect, and a follow-up activity emit — all in the kernel's own runtime. - flow.erl: flow:wait/1 — a timer-style suspend that PRESERVES the value on resume (vs flow:suspend/1, which returns the logged result), so a "wait until morning" step lets the env flow through to later steps. - next/flow/flows/blog_publish_digest.erl: the flow. Branches on the article :category (newsletter -> wait-until-morning -> send + emit; urgent -> send + emit now; else -> skip), fetches followers (injected), builds a digest email per follower, and emits a DigestSent activity OBJECT. Effect-as-data: a flow can't call kernel gen_servers from inside the drive (a blocking call there deadlocks the scheduler), so it returns the emails + DigestSent object for a driver to dispatch and append — which can then trigger downstream flows, closing the loop. Test: triggers_e2e.sh (10) — urgent completes in one cycle with 3 emails + a DigestSent object; newsletter suspends on the morning timer, then resumes to the same on "advancing the clock"; draft takes the else branch (no emails); a non-Article note is rejected by the guard; a duplicate activity fires once. flow:wait covered in next/flow (36/36). plans/fed-sx-design.md §13.10 documents the trigger fan-out as a kernel convention. lib/erlang 771/771. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
82 lines
3.3 KiB
Erlang
82 lines
3.3 KiB
Erlang
-module(blog_publish_digest).
|
|
-export([build/1]).
|
|
|
|
%% A motivating multi-step business flow for the fed-sx-triggers e2e:
|
|
%% when an Article is published, decide a batch policy by category,
|
|
%% (for newsletters) wait until morning, fetch the author's followers,
|
|
%% build a digest email for each, and emit a DigestSent activity — the
|
|
%% flow's own output, which a driver appends, closing the loop so it can
|
|
%% trigger downstream flows.
|
|
%%
|
|
%% Demonstrates: a branch on an activity field (:category), a timer
|
|
%% suspension (flow:wait/1, resumed by advancing the clock), an injected
|
|
%% effect (fetch_followers), and a follow-up activity emit.
|
|
%%
|
|
%% Effect-as-data: a flow runs inside flow_store's drive, where a
|
|
%% blocking call (e.g. into nx_kernel) would deadlock this scheduler, so
|
|
%% the flow does NOT perform IO itself. It DESCRIBES the effects in its
|
|
%% result — {digest_sent, Emails, DigestActivityObject} — and the driver
|
|
%% (the fan-out caller) dispatches the emails and appends the DigestSent
|
|
%% activity. fetch_followers is injected (the one external read) as a
|
|
%% pure function so the e2e can supply a deterministic list.
|
|
%%
|
|
%% Input env (from flow_dispatch): [{activity, A}, {actor, Actor}, ...].
|
|
%% Result: {digest_sent, [Email], DigestObject} | skipped.
|
|
|
|
build(Effects) ->
|
|
FetchFollowers = field(fetch_followers, Effects),
|
|
flow_spec:branch(
|
|
fun (Env) -> is_article(Env) end,
|
|
flow_spec:branch(
|
|
fun (Env) -> category_is(Env, newsletter) end,
|
|
%% newsletter: hold until morning, then send + emit
|
|
flow_spec:sequence([flow:wait(morning), send_emit(FetchFollowers)]),
|
|
flow_spec:branch(
|
|
fun (Env) -> category_is(Env, urgent) end,
|
|
%% urgent: send + emit now (no wait)
|
|
send_emit(FetchFollowers),
|
|
%% any other category: skip
|
|
flow_spec:flow_const(skipped))),
|
|
%% not an Article: skip
|
|
flow_spec:flow_const(skipped)).
|
|
|
|
%% send_emit(FetchFollowers) — the terminal step: build one digest email
|
|
%% per follower and the DigestSent emit object. Pure given the injected
|
|
%% follower list, so it is replay-safe (and it sits after the only
|
|
%% suspend point, so it runs exactly once).
|
|
send_emit(FetchFollowers) ->
|
|
flow_spec:flow_node(
|
|
fun (Env) ->
|
|
Activity = env_activity(Env),
|
|
Actor = env_actor(Env),
|
|
ArtId = activity_id(Activity),
|
|
Followers = FetchFollowers(Actor),
|
|
Emails = [ [{to, F}, {article, ArtId}] || F <- Followers ],
|
|
Digest = [{type, digest_sent},
|
|
{for, ArtId},
|
|
{follower_count, length(Followers)}],
|
|
{digest_sent, Emails, Digest}
|
|
end).
|
|
|
|
%% ── predicates / accessors ──────────────────────────────────────
|
|
|
|
is_article(Env) ->
|
|
object_type(object_of(env_activity(Env))) =:= article.
|
|
|
|
category_is(Env, Cat) ->
|
|
object_category(object_of(env_activity(Env))) =:= Cat.
|
|
|
|
env_activity(Env) -> field(activity, Env).
|
|
env_actor(Env) -> field(actor, Env).
|
|
|
|
object_of(Activity) -> field(object, Activity).
|
|
object_type(Obj) -> field(type, Obj).
|
|
object_category(Obj) -> field(category, Obj).
|
|
activity_id(Activity) -> field(id, Activity).
|
|
|
|
field(Key, Proplist) ->
|
|
case envelope:get_field(Key, Proplist) of
|
|
{ok, V} -> V;
|
|
_ -> undefined
|
|
end.
|