Availability
Availability decides whether a part of the application exists for the caller in front of you. A part is a command or a query; parts are grouped in modules, features and slices. It is the same model as Arc for .NET and Screenplay: audiences, blocks, gates and suspension, written in the same words.
It is one gate and it runs first: before binding, Cratis.Arc.Filter, authorization and validation. There is no second gating mechanism.
| Decision | Result |
|---|---|
| Available | The request continues to authorization and validation. |
| Not available, or unknown | Not authorized, authorizationFailureReason NotAvailable, HTTP 403. |
| Suspended | Not authorized, reason Suspended, HTTP 403. |
| The decision could not be made | A retryable error: isRetryable is true, the result stays authorized, HTTP 503. |
An unavailable and an unknown decision are reported identically; which gate closed it is logged at debug level and never sent. A store that is down is never reported as a denial.
The vocabulary
Section titled “The vocabulary”Name a definition in the configuration:
config :my_app, MyApp.Arc, availability: [ definition: MyApp.Availability, audience_source: MyApp.AudienceSource, suspension_source: MyApp.SuspensionSource, pubsub: MyApp.PubSub ]defmodule MyApp.Availability do use Cratis.Arc.Availability.Definition
audience "Plus", "Customers on the Plus package" audience "Admin", "Operators"
block "Paid", "Sold with Plus", for: ["Plus", "Admin"]
# a gate on a module, a feature or a slice: everything below the namespace scope "MyApp.Fleet", with: "Paid"
# a context value: read lazily, once per request value "vessel", MyApp.VesselValueendDeclaring a gate
Section titled “Declaring a gate”A command, a read model, or one query declares its gate with the words of the language. Every line holds.
defmodule MyApp.Fleet.Vessels.DockVessel do use Cratis.Arc.Command available for: ["Plus", "Admin"], not: "Preview", with: "Reported" available when: MyApp.HasPaidInvoiceendfor:an audience, or a list of which one is enoughnot:an audience, or a list of which none may be heldwith:a named blockwhen:a condition: restricted data fromCratis.Arc.Availability.Condition, shared with other languages, or a module implementing that behaviour for anything else
available when: Cratis.Arc.Availability.Condition.equal("vessel.package", "Plus")available when: Cratis.Arc.Availability.Condition.equal("arguments.vesselId", "v-17")On a read model available gates every query of the module; @query available: [...] gates one query. Module, feature and slice come from Cratis.Arc.Descriptor.slice_location/2, the same as in generated proxies.
A condition reads tenant, identity.user, arguments.<name> (what the part is about, by its wire name, camelCase) and <name>.<path> of a provided value. The tenant and the identity always come from the server; arguments never replace them.
Three values
Section titled “Three values”An expression is available, unavailable or unknown. Only available opens a gate. An absent value is unknown, for every operator, including not-equal; so is an audience that could not be decided. A value is read at most once per request. Cratis.Arc.Availability.Engine is a port of the .NET engine and passes the shared conformance cases.
Audiences: granted and withdrawn
Section titled “Audiences: granted and withdrawn”Implement Cratis.Arc.Availability.AudienceSource over your storage: grants(subject) returns the names of the audiences the tenant and user hold. A grant for an audience the definition does not declare never matches. Cratis.Arc.Availability.Memory is a complete source for specifications and samples (grant/2, withdraw/2).
Parts: suspended and resumed
Section titled “Parts: suspended and resumed”Implement Cratis.Arc.Availability.SuspensionSource: suspended/0 returns paths. A path is a module ("Fleet"), a feature ("Fleet/Vessels"), a slice ("Fleet/Vessels/Dock"), or the full name of a part (a command’s module, a query as Module.QueryName; the module name suspends all its queries). A suspension is decided before any gate. Memory.suspend/1 and Memory.resume/1 do it in memory.
When a source changes, call Cratis.Arc.Availability.changed/1. Open sessions decide again, on every node when :pubsub (a Phoenix.PubSub) is configured.
The client: /.cratis/availability
Section titled “The client: /.cratis/availability”An observable query that lists what is not available to the caller:
{ "unavailable": [ { "name": "MyApp.Fleet.Vessels.DockVessel", "module": "Fleet", "feature": "Vessels", "slice": "Dock", "reason": "NotAvailable" } ] }It emits the current answer, and again whenever audiences or suspensions change. ?part=<name>&arguments=<json> asks about one part and the data it is about. Generated proxies carry static readonly part, so a client knows what to ask about. A decision that cannot be made is a retryable error for the query, never a list.
Exploring availability while developing
Section titled “Exploring availability while developing”A tool such as Lens can switch availability live. In development, the application serves three routes, the same as Arc for .NET:
GET /.cratis/structureanswers the modules, features, sub-features and slices, each with apathof its names joined by/, and the commands and read models of each slice. It does not need availability.GET /.cratis/availability/modelanswers the audiences and blocks of the definition and the overrides in force.POST /.cratis/availability/overridesreplaces the overrides:
{ "enabledAudiences": ["Preview"], "disabledAudiences": ["Plus"], "enabledPaths": ["Fleet/Billing"], "disabledPaths": ["Fleet/Vessels/Dock"] }An enabled audience is held by every caller, a disabled one by none (disabled wins). An enabled path is available whatever its gates say, a disabled one is not available (disabled wins), and a path covers everything beneath it by whole segments. A change tells the open /.cratis/availability sessions, so clients decide again. The overrides apply to the whole application.
The availability routes are served only when development_tools of the availability configuration allows it: :auto (default) under Mix in :dev, true or false otherwise. Everywhere else they are not served and the overrides are never applied. They follow the :introspection setting like the catalogs.
LiveView
Section titled “LiveView”<Cratis.Arc.LiveView.Availability.available socket={@socket} version={@arc_availability} command={MyApp.Fleet.Vessels.DockVessel}> <button phx-click="dock">Dock</button></Cratis.Arc.LiveView.Availability.available>The component asks the same gate (Cratis.Arc.LiveView.available?/3). Hiding is presentation; the server still refuses.
The manifest
Section titled “The manifest”Cratis.Arc.Availability.Manifest.build/1 (and to_json/1) writes the cratis-availability-manifest file, version 1, that every Arc implementation shares; decode/1 reads one, decide/3 decides a part of it. The shared vectors of Cratis/Arc run in test/cratis/arc/availability_conformance_test.exs against pinned copies in test/support/availability.