event_rail 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e59345edd1e6e7570345e9b68df5be4b565e5705051d35f854076a64e8fc08b8
4
- data.tar.gz: d1391a4e11a36c9a81df720b6cd1376e4ddc79a126fab0fa8988db435a4823b4
3
+ metadata.gz: 1a5dd4da961fafe8b7a42cadb79cc9fcad3f9da337181a6e53f93ac2e2495f80
4
+ data.tar.gz: adea011ca9a862030527c8178d67e7165e2d9e17b8902328ed96f698910c9835
5
5
  SHA512:
6
- metadata.gz: a1bc401719f38d3f8fa654c1aba1df05451aaed81f813c7af74bb6c865ce90f35ccf48d579ec982627d973cd9f705e076b546303fd4b9759d5107dd5d99b9025
7
- data.tar.gz: 6200d549898556da58c5d09ebe252373a10d73e4876494850cba5ae6f5603c2d2175b2744801eb02ab8ab57cd8ac9a7c65b7e2c9c34e63fc1906e872636bb8fa
6
+ metadata.gz: 382236795f226f5dad4979cb2fbb29cac28fd31a4bd0ca7bc77bd8b7b0f7f7279275a4e97f3df1550d3f7dd5fd826e2ed96a682ccdd8b792d0c6d6bd7af73e3a
7
+ data.tar.gz: b84bba4b3e33f2fd809c7f833cd8913655f555f298828f7080bc8c27e8925cbd02125e3503d2658444d27f16061bc555749235229935dfa1e9cf4342e2be470b
data/CHANGELOG.md CHANGED
@@ -13,6 +13,63 @@ here unless a release changes how they behave.
13
13
 
14
14
  ## [Unreleased]
15
15
 
16
+ ## [0.3.0] - 2026-09-21
17
+
18
+ ### Changed
19
+
20
+ - **`subscribes_to` now raises `EventRail::DeclarationError` when the event class it names
21
+ declares neither `event_type` nor `version`**, at the declaration itself. Previously such
22
+ a subscription was accepted and then never fired: publication matches subscribers by exact
23
+ event class, so a concrete subclass never reached the base's registration, and the class
24
+ without a contract could not be constructed to be published on its own. Because neither
25
+ declaration is inherited, this covers a shared abstract base such as `ApplicationEvent`
26
+ and equally a subclass of a concrete event that adds nothing of its own. Subscribe to each
27
+ concrete event class instead.
28
+
29
+ ## [0.2.0] - 2026-09-17
30
+
31
+ ### Added
32
+
33
+ - `EventRail::TestHelper`, loaded by an explicit `require "event_rail/test_helper"` and not
34
+ by requiring the library. `EventRail::TestHelper.declare` opens a window in which event
35
+ contracts and subscriptions may be declared after application preparation has sealed the
36
+ registry, and `with_subscribers` activates a declared fixture subscriber for the duration
37
+ of one block, restoring the previous registry afterwards. Contracts declared in a window
38
+ go live when it closes; subscribers stay dormant until activated, so a fixture cannot fan
39
+ out in a later test that never mentions it.
40
+ - `config.event_rail.roots`, defaulting to `%w[app/events app/jobs]` and appendable, naming
41
+ the directories discovery scans. Each entry is matched as a path suffix against the main
42
+ autoloader's roots, so one entry covers the host application and every engine. A
43
+ configured root beyond the default that is not an autoload root fails preparation with the
44
+ new `EventRail::ConfigurationError`; a default root the application has not created is
45
+ skipped.
46
+
47
+ ### Changed
48
+
49
+ - **A concrete `EventRail::Event` subclass that declares `event_type` or `version` after
50
+ preparation now raises `EventRail::DeclarationError` at the declaration**, the same rule
51
+ subscriptions have always followed. Previously such a class was silently absent from the
52
+ contract index: it published cleanly, serialized cleanly, and failed only when a worker
53
+ tried to reconstruct it, with `UnknownEventTypeError`. Move the class under `app/events`,
54
+ add its root to `config.event_rail.roots`, or -- for a test fixture -- declare it inside
55
+ `EventRail::TestHelper.declare`. Abstract bases and classes never assigned to a constant
56
+ are unaffected.
57
+ - A subscription declared on a class with no name now raises where it is declared. Active
58
+ Job cannot enqueue a job it cannot name, so this was never a working configuration; it was
59
+ previously dropped from the registry without comment.
60
+ - Both late-declaration messages now name the declaration's own file and line, state that
61
+ discovery runs before `eager_load!` so `config.eager_load_paths` alone is never enough,
62
+ and offer both remedies. The advice to load such code from an autoload-once path is gone
63
+ as general guidance: it never applied to reloadable application code.
64
+
65
+ ### Fixed
66
+
67
+ - A declaration arriving between a reload's constant unload and the registry rebuild is no
68
+ longer rejected as late. Rails deletes constants before running prepare callbacks and
69
+ nothing cleared the snapshot in between, so that window was indistinguishable from a
70
+ sealed registry -- reachable by any prepare callback ordered ahead of EventRail's,
71
+ including one belonging to a gem that loads earlier in the Gemfile.
72
+
16
73
  ## [0.1.0] - 2026-09-15
17
74
 
18
75
  ### Added
@@ -43,5 +100,7 @@ here unless a release changes how they behave.
43
100
  - A typed error hierarchy rooted at `EventRail::Error`, distinguishing declaration,
44
101
  casting, context, serialization, and publication failures.
45
102
 
46
- [Unreleased]: https://github.com/alexdmtv/event-rail/compare/v0.1.0...HEAD
103
+ [Unreleased]: https://github.com/alexdmtv/event-rail/compare/v0.3.0...HEAD
104
+ [0.3.0]: https://github.com/alexdmtv/event-rail/compare/v0.2.0...v0.3.0
105
+ [0.2.0]: https://github.com/alexdmtv/event-rail/compare/v0.1.0...v0.2.0
47
106
  [0.1.0]: https://github.com/alexdmtv/event-rail/releases/tag/v0.1.0
data/README.md CHANGED
@@ -2,9 +2,43 @@
2
2
 
3
3
  [![CI](https://github.com/alexdmtv/event-rail/actions/workflows/ci.yml/badge.svg)](https://github.com/alexdmtv/event-rail/actions/workflows/ci.yml)
4
4
 
5
- EventRail is an early-stage Rails library for immutable domain events and durable fanout through ordinary Active Job subscribers. It builds on Rails conventions instead of introducing a transport, command bus, dependency-injection container, or replacement job runtime.
5
+ When a Rails application grows, the code that places an order ends up naming everything that should happen next: send the confirmation email, update the loyalty balance, notify the warehouse, write the analytics row. Each addition is another dependency in the one place that can least afford them.
6
6
 
7
- EventRail is not ready for production use yet. The public API is still being implemented and validated.
7
+ There are two ways for one part of an application to reach another. The synchronous one — call a method, get an answer — Ruby hands you for free, and it is the one above: the caller names the callee. The other inverts it. One part announces a fact, and whoever cares subscribes. The dependency does not vanish: a subscriber still names the event, and the event belongs to the code that publishes it. But it points the other way, and it is far narrower: a dependency on something that happened, not on how the other side works. What matters is the side that accumulates. Code that invokes every interested party learns a new name for each one; code that announces a fact learns nothing.
8
+
9
+ Rails ships no way to express the second. `ActiveSupport::Notifications` is in-process and untyped, Active Record callbacks are the coupling you were trying to escape, and an event store asks you to adopt a storage model in order to send one message across a boundary.
10
+
11
+ EventRail is that second interface. An event is an immutable, versioned value. Every subscriber is an ordinary Active Job that keeps its own queue, retry policy, and concurrency limits, and delivery goes through the queue adapter you already run. The publisher never names a subscriber. There is no transport, no registration API, and no runtime of its own.
12
+
13
+ EventRail is pre-1.0: the public API may change in a minor release, and every change is documented in the [CHANGELOG](CHANGELOG.md). Delivery semantics, safety limits, and the notification contract are settled and documented below.
14
+
15
+ ## When to use EventRail
16
+
17
+ EventRail is narrow on purpose: durable fanout across a boundary inside one application, where each subscriber retries independently and the same fact keeps the same identity across every retry and every hop. Reaching for an event store to get that means adopting its storage model too: entities as streams, state rebuilt from history. That is a large commitment for what is, at the boundary, one message. If you want that commitment, or something else entirely, these fit better.
18
+
19
+ | If you need | Use | Because |
20
+ | --- | --- | --- |
21
+ | An event log: audit history, replay, rebuilding read models | [Rails Event Store](https://railseventstore.org) | It is a store. Events persist in the same transaction as the state that produced them, which also closes the dual-write gap below. |
22
+ | Handlers that run inside the same request | [downstream](https://github.com/palkan/downstream) | Delivery is synchronous by default, async opt-in per subscriber. |
23
+ | In-process decoupling and nothing more | `ActiveSupport::Notifications` | Already in your application. No gem, no contract, no queue. |
24
+ | Events to be the system of record, not a message | [Sequent](https://www.sequent.io) | A full event-sourcing framework: commands, aggregates, projectors. |
25
+
26
+ **Why not call `perform_later` three times?** That is already durable fanout, and for three jobs in one method it is the right answer. EventRail earns its place when the publisher should not know its subscribers, when the payload needs a versioned contract that outlives a worker running older code, and when a retry must not produce a second copy of a fact already recorded.
27
+
28
+ ### What it gives you
29
+
30
+ - **Retry-stable identity**, derived from the publishing execution rather than generated, so a redelivered cause produces the same event rather than a second copy of it. Idempotency holds across hops, not only across one subscriber's retries.
31
+ - **Subscribers that are the jobs**, each keeping its own queue, retry policy, and concurrency limits rather than sharing the single wrapper job other gems route subscribers through.
32
+ - **Boot-time validation**: a wrong `perform` arity, a missing `EventRail::JobContext`, or a late declaration fails the boot that introduced it rather than the first publication in production.
33
+ - **A typed boundary**: a `Date`, a `BigDecimal`, a record, or a GlobalID is refused rather than serialized into something a worker cannot restore.
34
+ - **Your own layout**: discovery matches a path suffix against the autoload roots Rails already has, covering engines and a packwerk-style `packs/billing/app/jobs` with no configuration.
35
+ - **No Active Record**, table, migration, or initializer.
36
+
37
+ ### What it does not do
38
+
39
+ - **No event log and no synchronous handlers.** No history, replay, read-model rebuild, or browser UI, and every subscriber crosses the queue.
40
+ - **No outbox, and so a dual-write gap.** Publication is refused inside a transaction, so a publisher commits and then publishes, and a process that dies between the two loses the event. Publishing from a job makes that recoverable, because the retry republishes under the same identity (see [Replay-safe publishers](#replay-safe-publishers)); from a controller action there is no such guarantee. An application that wants recovery beyond that can record an intent and republish it from a sweeper, but a republication from a different job derives a different event ID, so the intent's own key belongs in the payload for consumers to deduplicate on.
41
+ - **Discovery is enforced, not advisory.** A subscriber outside a [discovery root](#where-discovery-looks) fails the boot rather than being quietly ignored: a silent delivery bug traded for a loud startup error, at the price of a layout rule.
8
42
 
9
43
  ## Requirements
10
44
 
@@ -20,7 +54,7 @@ EventRail depends on Active Support, Active Model, Active Job, Railties, and Zei
20
54
  gem "event_rail"
21
55
  ```
22
56
 
23
- Then run `bundle install`. There is no initializer to generate and nothing to configure: EventRail initializes itself through a Railtie, and every safety limit is a fixed documented constant.
57
+ Then run `bundle install`. There is no initializer to generate and nothing you have to configure: EventRail initializes itself through a Railtie, every safety limit is a fixed documented constant, and the one option it does expose, [where it looks for events and subscribers](#where-discovery-looks), has a default that most applications never change.
24
58
 
25
59
  ## Job integration
26
60
 
@@ -88,13 +122,13 @@ event.metadata # immutable metadata; not yet stamped
88
122
 
89
123
  `attributes` is Active Model's own meaning and is what application code reads. `data` is the single written form that both the queue representation and the public envelope use, so a `Date`, a `BigDecimal`, or a nested Ruby object never reaches a queue adapter or a codec.
90
124
 
91
- Casting refuses to discard information rather than substituting a plausible value. `"abc"` is not `0`, `true` is not `"t"`, a timestamp needs an explicit offset, and a date attribute will not silently drop a time of day. An untyped attribute accepts only recursively JSON-like values — strings, integers, finite floats, booleans, `nil`, arrays, string-keyed hashes — bounded in nesting depth and forbidden from using the `_aj_` prefix Active Job reserves in its own argument encoding. Records, GlobalID values, arbitrary Ruby objects, non-finite numbers, undeclared attributes, and callable defaults are rejected.
125
+ Casting refuses to discard information rather than substituting a plausible value. `"abc"` is not `0`, `true` is not `"t"`, a timestamp needs an explicit offset, and a date attribute will not silently drop a time of day. An untyped attribute accepts only recursively JSON-like values: strings, integers, finite floats, booleans, `nil`, arrays, and string-keyed hashes. Those are bounded in nesting depth and forbidden from using the `_aj_` prefix Active Job reserves in its own argument encoding. Records, GlobalID values, arbitrary Ruby objects, non-finite numbers, undeclared attributes, and callable defaults are rejected.
92
126
 
93
127
  Constructed events, nested data, metadata, and every contained value are recursively immutable. Two events of one class with equal payload and equal metadata are equal values, which is what lets `assert_enqueued_with(args: [event])` match a published event.
94
128
 
95
129
  ### Custom attribute types
96
130
 
97
- A custom Active Model type may be used as an attribute type. Rails' `serialize` is documented as producing a value "usable by the database", and a database driver accepts a `Date` or a `BigDecimal` object -- a queue does not, and Active Job does not recurse into a serializer's output, so such a value would reach the adapter raw. Including `EventRail::PortableType` narrows the promise to a JSON primitive, array, or string-keyed hash, and `portable_examples` makes the promise checkable:
131
+ A custom Active Model type may be used as an attribute type. Rails' `serialize` is documented as producing a value "usable by the database", and a database driver accepts a `Date` or a `BigDecimal` object. A queue does not, and Active Job does not recurse into a serializer's output, so such a value would reach the adapter raw. Including `EventRail::PortableType` narrows the promise to a JSON primitive, array, or string-keyed hash, and `portable_examples` makes the promise checkable:
98
132
 
99
133
  ```ruby
100
134
  module Docs
@@ -124,17 +158,37 @@ Docs::ParcelShipped.new(parcel_id: "P-1", weight: 2500).data
124
158
  # => { "parcel_id" => "P-1", "weight" => 2500 }
125
159
  ```
126
160
 
127
- Every example is round-tripped through JSON when the attribute is declared, so a type that cannot hold up fails at class definition rather than at the first enqueue. A type whose cast value is already a portable scalar -- a plain `:string` subclass, say -- needs none of this.
161
+ Every example is round-tripped through JSON when the attribute is declared, so a type that cannot hold up fails at class definition rather than at the first enqueue. A type whose cast value is already a portable scalar (a plain `:string` subclass, say) needs none of this.
128
162
 
129
163
  ## Publishing
130
164
 
131
165
  ```ruby
132
166
  module Docs
133
- class OnOrderPlacedJob < ApplicationJob
134
- subscribes_to OrderPlaced
167
+ # app/jobs/billing/charge_card_job.rb
168
+ module Billing
169
+ class ChargeCardJob < ApplicationJob
170
+ queue_as :payments
171
+ retry_on Timeout::Error, attempts: 10
172
+
173
+ subscribes_to OrderPlaced
135
174
 
136
- def perform(event)
137
- Rails.logger.info("charging #{event.order_id} idempotently on #{event.id}")
175
+ def perform(event)
176
+ Rails.logger.info("charging #{event.order_id} idempotently on #{event.id}")
177
+ end
178
+ end
179
+ end
180
+
181
+ # packs/analytics/app/jobs/analytics/record_order_job.rb
182
+ module Analytics
183
+ class RecordOrderJob < ApplicationJob
184
+ queue_as :low
185
+ discard_on ActiveJob::DeserializationError
186
+
187
+ subscribes_to OrderPlaced
188
+
189
+ def perform(event)
190
+ Rails.logger.info("recording #{event.order_id}")
191
+ end
138
192
  end
139
193
  end
140
194
  end
@@ -148,11 +202,31 @@ publication.accepted_subscribers # subscribers whose enqueue was accepted
148
202
  publication.skipped_subscribers # subscribers whose own enqueue callback declined
149
203
  ```
150
204
 
205
+ Billing and analytics do not know about each other, and the publishing code names neither. Adding a third subscriber is adding a file; the code that publishes `OrderPlaced` never changes. Each one keeps its own queue and its own failure policy, because each one is just a job. A payment that retries for ten attempts and an analytics write that is discarded when its event can no longer be loaded are the same fanout, configured separately.
206
+
151
207
  Subscribers are discovered from the conventional `app/events` and `app/jobs` roots of the host application and every engine, during Rails preparation. There is no registration API, no initializer, and no registry to query.
152
208
 
153
- `subscribes_to` is exact and not inherited: a subclass of a subscriber is a different job and receives nothing. A subscriber must define its own `perform` taking exactly one required positional event parameter, must not have subclasses, and must include `EventRail::JobContext`. Each of those is checked during preparation, so a mistake fails the boot that introduced it rather than the first publication.
209
+ ### Where discovery looks
210
+
211
+ ```ruby
212
+ # doc:illustrative
213
+ # config/application.rb
214
+ config.event_rail.roots << "app/subscribers" # default: %w[app/events app/jobs]
215
+ ```
216
+
217
+ Each entry is matched as a path suffix against the autoload roots Rails already has, so one entry covers the host application, every engine, and a packwerk-style `packs/billing/app/jobs` without naming any of them. Appending is additive: the defaults stay in effect.
218
+
219
+ Two things to know before reaching for it. A configured root is eager-loaded during preparation in **every** environment, so naming something broad like `app/models` loads that directory for the application and every engine on each boot and each reload; moving the file is usually the better fix. And a root you name that is not an autoload root of the application or any engine fails preparation with `EventRail::ConfigurationError`, so a typo is a boot error rather than a directory that silently discovers nothing. A *default* root the application has not created is simply skipped, so a fresh application with no `app/events` directory boots normally.
220
+
221
+ `subscribes_to` is exact and not inherited on either side. A subclass of a subscriber is a different job and receives nothing. A subscriber must define its own `perform` taking exactly one required positional event parameter, must not have subclasses, and must include `EventRail::JobContext`. Each of those is checked during preparation, so a mistake fails the boot that introduced it rather than the first publication. The event class a subscription names must declare a contract of its own. Neither `event_type` nor `version` is inherited, so a shared abstract base and a subclass that adds nothing of its own are both refused, and refused at the declaration itself rather than during preparation, because the class is already loaded by the time the macro reads it. A class that declares one of the two and not the other is a broken contract rather than a base, and still fails preparation naming the event class.
222
+
223
+ A subscriber must also be reachable by name, because Active Job enqueues a job by name. Declaring a subscription on a class that has none raises immediately, so `Foo.const_set(:Bar, Class.new(ApplicationJob) { subscribes_to Baz })` is not supported; name the class first. The check is on the name the class carries, not on whether a constant resolves to it, so a class with a name nothing resolves to is still dropped from the registry without comment; that is a deliberate limit, not an oversight.
154
224
 
155
- A subscriber declared outside those roots must be loaded before preparation finishes, or declaring it raises: EventRail refuses to run with a subscriber it cannot see at boot. A subscriber required from an initializer works, but initializers run before the main autoloader exists, so such a file has to bring its own event class and job base rather than referencing autoloaded constants. Declaring a subscriber after preparation -- from a test file, or from a lazily autoloaded path outside `app/events` and `app/jobs` -- raises for the same reason, so a test that needs a throwaway subscriber should define it in a file under a conventional root of the test application instead.
225
+ One rule covers both halves of discovery: **an event contract or a subscription declared after preparation has sealed the registry raises**, naming the file and line and the two ways to fix it. EventRail refuses to run with a subscriber that would receive nothing, or with an event class a worker could not reconstruct from the queue.
226
+
227
+ Adding the directory to `config.eager_load_paths` is never enough on its own, and the reason is Rails' own ordering: prepare callbacks run *before* `eager_load!`, so a reloadable class outside a discovery root cannot be loaded in time in any environment. Either move the file under a discovery root, or [add its root](#where-discovery-looks).
228
+
229
+ Two exceptions. Code that is not reloadable at all, such as a gem or a file plainly required from an initializer, ran its declaration before preparation, so it is already registered; such a file has to bring its own event class and job base, because initializers run before the main autoloader exists. And a test suite loads after preparation by definition, which is what [`EventRail::TestHelper`](#tests-that-need-their-own-fixtures) is for.
156
230
 
157
231
  ### At-least-once delivery, and what that means for subscribers
158
232
 
@@ -166,7 +240,7 @@ Retries, backoff, discarding, and dead-letter handling stay where they already a
166
240
 
167
241
  Don't. Publication raises `EventRail::TransactionalPublicationError` when a transaction is open, in every environment, and the fix is to publish after the transaction commits.
168
242
 
169
- Both queue deferral settings are wrong inside a transaction, in opposite directions. With `enqueue_after_transaction_commit` on, the enqueue is deferred past the point where its failure can be reported, so a publication that silently enqueued nothing looks successful. With it off, the enqueue announces a fact that a rollback then contradicts. The check is on the open transaction itself, so it does not depend on the setting — or on Active Record being present at all.
243
+ Both queue deferral settings are wrong inside a transaction, in opposite directions. With `enqueue_after_transaction_commit` on, the enqueue is deferred past the point where its failure can be reported, so a publication that silently enqueued nothing looks successful. With it off, the enqueue announces a fact that a rollback then contradicts. The check is on the open transaction itself, so it does not depend on the setting, or on Active Record being present at all.
170
244
 
171
245
  ### Replay-safe publishers
172
246
 
@@ -187,7 +261,7 @@ Guarding publication behind "did I already transition?" is the failure mode to a
187
261
 
188
262
  ## Identity, occurrence time, and source
189
263
 
190
- Inside an opted-in job, an event's ID is derived from a permanent EventRail namespace and the resolved source, executing job class, execution scope, event type, version, and logical identity. The execution scope is the same value the event records as its causation: a regular job's own ID, or, for a subscriber, the ID of the event it is handling. That is what makes identity survive more than one hop — a follow-up event published while handling a redelivered cause derives the ID it derived the first time.
264
+ Inside an opted-in job, an event's ID is derived from a permanent EventRail namespace and the resolved source, executing job class, execution scope, event type, version, and logical identity. The execution scope is the same value the event records as its causation: a regular job's own ID, or, for a subscriber, the ID of the event it is handling. That is what makes identity survive more than one hop: a follow-up event published while handling a redelivered cause derives the ID it derived the first time.
191
265
 
192
266
  Logical identity is chosen in this order: an explicit event ID (an inbound external event), an explicit `key:` passed to `publish`, the class's `identity_by` attributes, or a singleton marker for the first publication of that type in the execution. Provide an explicit key when neither declared identity nor the singleton default can tell two legitimate publications apart:
193
267
 
@@ -202,7 +276,7 @@ Changing `identity_by`, changing `default_source`, or renaming a subscriber clas
202
276
 
203
277
  `occurred_at` is the logical publication time: an explicit timezone-aware value is preserved as the same instant, and otherwise it is the start of the current execution, stable across that execution's retries and never inherited from a cause. Use a persisted domain timestamp when business occurrence time matters. Stored times are UTC at microsecond precision.
204
278
 
205
- `source` identifies a logical producer — not an environment, queue, topic, cluster, or deployment. Any bounded non-empty string is accepted; a stable namespaced value such as `acme.orders` is recommended, and EventRail does no URI parsing.
279
+ `source` identifies a logical producer, not an environment, queue, topic, cluster, or deployment. Any bounded non-empty string is accepted; a stable namespaced value such as `acme.orders` is recommended, and EventRail does no URI parsing.
206
280
 
207
281
  ## Logical context
208
282
 
@@ -216,7 +290,7 @@ end
216
290
 
217
291
  A nested scope inherits lineage and may add extensions or repeat identical values; replacing an inherited identifier, origin time, or extension value fails rather than rewriting the lineage of a flow already in progress.
218
292
 
219
- Context extensions are durable baggage: opted-in child jobs carry them, and published events merge them with event-local extensions, rejecting conflicting values. They are not the place for domain data — that belongs in declared attributes.
293
+ Context extensions are durable baggage: opted-in child jobs carry them, and published events merge them with event-local extensions, rejecting conflicting values. They are not the place for domain data; that belongs in declared attributes.
220
294
 
221
295
  ### Reading context
222
296
 
@@ -250,9 +324,9 @@ class SendReceiptJob < ApplicationJob
250
324
  end
251
325
  ```
252
326
 
253
- Reach for `Current` in a subscriber only to hand lineage to something that does not take the event -- a log line, an outbound request header, an APM tag.
327
+ Reach for `Current` in a subscriber only to hand lineage to something that does not take the event: a log line, an outbound request header, an APM tag.
254
328
 
255
- Lineage is isolated per unit of concurrent execution through `ActiveSupport::IsolatedExecutionState`, which defaults to thread isolation. **A host running fibers -- a fiber-per-request server, or a worker that runs jobs on fibers -- must set `config.active_support.isolation_level = :fiber`**, or lineage will be shared between concurrent fibers. That setting is Rails-wide rather than EventRail's, and it governs both `EventRail::Current` and EventRail's own publication state. EventRail documents the requirement rather than claiming an isolation it cannot provide.
329
+ Lineage is isolated per unit of concurrent execution through `ActiveSupport::IsolatedExecutionState`, which defaults to thread isolation. **A host running fibers, whether a fiber-per-request server or a worker that runs jobs on fibers, must set `config.active_support.isolation_level = :fiber`**, or lineage will be shared between concurrent fibers. That setting is Rails-wide rather than EventRail's, and it governs both `EventRail::Current` and EventRail's own publication state. EventRail documents the requirement rather than claiming an isolation it cannot provide.
256
330
 
257
331
  Leaving it wrong fails loudly rather than silently: under thread isolation two concurrent fibers each establishing context raise `InvalidContext`, because the second inherits the first's identifiers and replacing them is refused. The rule that stops lineage being rewritten mid-flow doubles as a misconfiguration detector.
258
332
 
@@ -289,11 +363,67 @@ Application tests use Active Job's own helpers and nothing from EventRail:
289
363
  # doc:illustrative
290
364
  publication = EventRail.publish(Docs::OrderPlaced.new(order_id: "A-1005", total: "1.00", placed_at: Time.now.utc.iso8601))
291
365
 
292
- assert_enqueued_with(job: Docs::OnOrderPlacedJob, args: [ publication.event ])
366
+ assert_enqueued_with(job: Docs::Billing::ChargeCardJob, args: [ publication.event ])
293
367
  perform_enqueued_jobs
294
368
  ```
295
369
 
296
- `assert_enqueued_with(args:)` works because events are value objects. For observability assertions, subscribe to the notifications below. EventRail ships no assertion library, observer, or contract-test helper.
370
+ `assert_enqueued_with(args:)` works because events are value objects. For observability assertions, subscribe to the notifications below. EventRail ships no assertion library, observer, or contract-test helper; Active Job's helpers are the whole assertion surface.
371
+
372
+ ### Tests that need their own fixtures
373
+
374
+ A test file loads after preparation, so declaring a throwaway event or subscriber in one would raise. `EventRail::TestHelper` is the door for it, and it is opt-in:
375
+
376
+ ```ruby
377
+ # doc:illustrative
378
+ # test/test_helper.rb
379
+ require "event_rail/test_helper"
380
+
381
+ class ActiveSupport::TestCase
382
+ include EventRail::TestHelper
383
+ end
384
+ ```
385
+
386
+ Declare fixtures at file scope, in a window:
387
+
388
+ ```ruby
389
+ EventRail::TestHelper.declare do
390
+ module OrderTests
391
+ class Placed < EventRail::Event
392
+ event_type "docs.order_tests_placed"
393
+ version 1
394
+ default_source "tests"
395
+
396
+ attribute :order_id, :string
397
+ end
398
+
399
+ class AuditJob < ApplicationJob
400
+ subscribes_to Placed
401
+
402
+ def perform(event) = Rails.logger.info(event.order_id)
403
+ end
404
+ end
405
+ end
406
+ ```
407
+
408
+ **A subscriber declared in a window receives nothing until you activate it.** That is deliberate: a fixture that went live on declaration would fan out in every later test in the process, including tests that never mention it. Activate it for one block:
409
+
410
+ ```ruby
411
+ # doc:illustrative
412
+ test "publication fans out to the audit job" do
413
+ with_subscribers(OrderTests::AuditJob) do
414
+ publication = EventRail.publish(OrderTests::Placed.new(order_id: "o-1"))
415
+
416
+ assert_enqueued_with(job: OrderTests::AuditJob, args: [ publication.event ])
417
+ perform_enqueued_jobs
418
+ end
419
+ end
420
+ ```
421
+
422
+ The previous registry is restored when the block exits, including when it raises, and nested activations are additive. Activation applies the same validation preparation does, so an abstract fixture fails there rather than at delivery.
423
+
424
+ Event contracts behave differently from subscribers on purpose: they are registered when the window closes and stay registered, because `assert_enqueued_with` deserializes the job it is comparing and so needs the contract outside any block. A class cannot be unloaded, which has one consequence worth knowing: **give each fixture a unique `event_type`, and declare it once, at file scope.** Two live classes claiming one type and version fail every later rebuild for the rest of the process, and a declaration inside a test method reopens the same constant and runs the writer again on a sealed registry.
425
+
426
+ Activation replaces a process-wide registry, so it is not safe under `parallelize(with: :threads)`. Process-based parallelisation, the Rails default, is unaffected.
297
427
 
298
428
  ## Notifications
299
429
 
@@ -342,7 +472,7 @@ The private Active Job representation and the job context entry each carry their
342
472
  2. Only then deploy a release that writes the new form.
343
473
  3. Remove the old reader only after every queued message in the old form is drained or expired.
344
474
 
345
- Rolling back reverses that: a rollback is safe only to a release that can still read what the newer release wrote. A change to how an individual value is written is a different matter — that encoding is shared with the public envelope, so it is a breaking public change rather than a private format bump.
475
+ Rolling back reverses that: a rollback is safe only to a release that can still read what the newer release wrote. A change to how an individual value is written is a different matter: that encoding is shared with the public envelope, so it is a breaking public change rather than a private format bump.
346
476
 
347
477
  ## Development
348
478
 
@@ -13,6 +13,12 @@ module EventRail
13
13
  class DeclarationError < Error
14
14
  end
15
15
 
16
+ # An application's own configuration is wrong: a discovery root that is not an autoload
17
+ # root of the application or any engine. Distinct from a declaration fault, which is about
18
+ # a class body, and raised at preparation so it fails the boot that introduced it.
19
+ class ConfigurationError < Error
20
+ end
21
+
16
22
  # A value cannot become the declared type without discarding information, or is not
17
23
  # portable at all.
18
24
  class CastingError < Error
@@ -43,6 +43,12 @@ module EventRail
43
43
  raise DeclarationError, "event_type exceeds #{Limits::MAX_EVENT_TYPE_BYTES} bytes"
44
44
  end
45
45
 
46
+ # Only a change is a declaration. Re-running a class body that sets the same value --
47
+ # a `load` rather than a `require`, a reopened class -- alters no contract, so the
48
+ # index is already correct and rejecting it would be a false positive.
49
+ unless @event_type == value
50
+ Internal::Registry.declare_contract(self, caller_locations(1, 1).first)
51
+ end
46
52
  @event_type = value.dup.freeze
47
53
  end
48
54
 
@@ -53,6 +59,9 @@ module EventRail
53
59
  raise DeclarationError, "version must be a positive integer"
54
60
  end
55
61
 
62
+ unless @event_version == value
63
+ Internal::Registry.declare_contract(self, caller_locations(1, 1).first)
64
+ end
56
65
  @event_version = value
57
66
  end
58
67
 
@@ -12,7 +12,14 @@ module EventRail
12
12
  # to have no subscribers: the first is a boot-order bug and raises, the second is a
13
13
  # legitimate zero-delivery publication.
14
14
  class Registry
15
- CONVENTIONAL_ROOTS = %w[app/events app/jobs].freeze
15
+ # The conventional discovery roots. Matched as a path suffix against the main
16
+ # autoloader's roots, which is what makes one entry cover the host application, every
17
+ # engine, and packwerk-style `packs/*/app/jobs` without naming any of them.
18
+ #
19
+ # An application may append through `config.event_rail.roots`. The default is copied
20
+ # rather than shared, so `<<` works and cannot mutate this constant.
21
+ DEFAULT_ROOTS = %w[app/events app/jobs].freeze
22
+ CONVENTIONAL_ROOTS = DEFAULT_ROOTS
16
23
 
17
24
  Snapshot = Struct.new(:subscribers, :contracts, keyword_init: true) do
18
25
  def subscribers_for(event_class)
@@ -34,23 +41,69 @@ module EventRail
34
41
  @pending = []
35
42
  @snapshot = nil
36
43
  @building = false
44
+ # A test declaration window, opened by EventRail::TestHelper.declare. Distinct from
45
+ # @building: a window routes subscriptions to the dormant fixture set rather than to
46
+ # the live pending list, which is what keeps a fixture from fanning out to every
47
+ # later test in the process.
48
+ @window = false
49
+ @fixtures = []
50
+ # Set from `before_class_unload`, which fires first in a reload cycle, and cleared when
51
+ # preparation finishes. Rails deletes the constants and only then runs prepare
52
+ # callbacks, and nothing clears the snapshot in between -- so without this flag the
53
+ # window between the two is indistinguishable from a sealed registry, and a legal
54
+ # declaration arriving there would be rejected for lateness.
55
+ @reloading = false
56
+ # Subscribers activated for the duration of a block, and the snapshot and fixture set
57
+ # to restore when it exits. A stack, so nested activations are additive.
58
+ @active_fixtures = []
59
+ @activations = []
37
60
 
38
61
  class << self
39
62
  # Appending is idempotent per job class: the macro may be called more than once
40
63
  # in one body, and the declarations themselves live on the job class.
41
- def declare(job_class)
64
+ def declare(job_class, location = nil)
42
65
  @monitor.synchronize do
43
- if @snapshot && !@building
66
+ if @window
67
+ @fixtures << job_class unless @fixtures.include?(job_class)
68
+ next
69
+ end
70
+
71
+ if @snapshot && !@building && !@reloading
44
72
  raise DeclarationError,
45
- "#{job_class} declared a subscription after EventRail finished preparing, so it would receive no " \
46
- "deliveries. Move it under a conventional app/events or app/jobs root, or load it from an " \
47
- "autoload-once path or plain require before application preparation."
73
+ "#{named(job_class)}#{" (#{at(location)})" if location} declared a subscription after EventRail finished " \
74
+ "preparing, so it would receive no deliveries. #{discovery_advice}. A subscriber in a gem that is " \
75
+ "not loaded at boot can be required from an initializer. In a test, define it inside " \
76
+ "EventRail::TestHelper.declare."
48
77
  end
49
78
 
50
79
  @pending << job_class unless @pending.include?(job_class)
51
80
  end
52
81
  end
53
82
 
83
+ # The contract half of the sealing rule, called from the writer form of
84
+ # `EventRail::Event.event_type` and `.version`. A check only: contracts are collected
85
+ # by rescanning `EventRail::Event.descendants` when a snapshot is built, so there is
86
+ # no pending list for events and nothing here to keep reload-safe.
87
+ #
88
+ # Named classes only. An unnamed class can never enter the index -- `build_contracts`
89
+ # selects through `live?`, which resolves the constant the name denotes -- so checking
90
+ # one would reject every inline event definition in a test suite for no guarantee
91
+ # gained.
92
+ def declare_contract(event_class, location = nil)
93
+ return if event_class.name.nil?
94
+
95
+ @monitor.synchronize do
96
+ next unless @snapshot
97
+ next if @building || @reloading || @window
98
+
99
+ raise DeclarationError,
100
+ "#{named(event_class)}#{" (#{at(location)})" if location} declared an event contract after EventRail " \
101
+ "finished preparing, so a worker could not reconstruct it from the queue. #{discovery_advice}. An " \
102
+ "event class in a gem that is not loaded at boot can be required from an initializer. In a test, " \
103
+ "define it inside EventRail::TestHelper.declare."
104
+ end
105
+ end
106
+
54
107
  def snapshot
55
108
  snapshot = @snapshot
56
109
  return snapshot if snapshot
@@ -64,6 +117,17 @@ module EventRail
64
117
  !@snapshot.nil?
65
118
  end
66
119
 
120
+ # Marks the start of a reload cycle. Registered once from a Railtie initializer, never
121
+ # from preparation: a callback registered inside `prepare` would be added again on
122
+ # every reload and accumulate for the life of the process.
123
+ def reloading!
124
+ @monitor.synchronize { @reloading = true }
125
+ end
126
+
127
+ def reloading?
128
+ @reloading
129
+ end
130
+
67
131
  def subscribers_for(event_class)
68
132
  snapshot.subscribers_for(event_class)
69
133
  end
@@ -80,6 +144,11 @@ module EventRail
80
144
  @snapshot = build_snapshot
81
145
  ensure
82
146
  @building = previously_building
147
+ # Cleared here rather than from a reloader callback because the two reload paths
148
+ # differ: a console `reload!` runs prepare twice, the executor path that serves a
149
+ # request runs it once. The end of preparation is the only point correct for
150
+ # both. In `ensure`, so a failed prepare does not leave sealing disabled.
151
+ @reloading = false
83
152
  end
84
153
  end
85
154
  @snapshot
@@ -100,11 +169,90 @@ module EventRail
100
169
  end
101
170
  end
102
171
 
172
+ # Opens a test declaration window. Subscriptions declared inside become dormant
173
+ # fixtures; event contracts declared inside are indexed when the window closes,
174
+ # because `build_contracts` rescans `EventRail::Event.descendants` and a named class
175
+ # stays there for the life of the process.
176
+ #
177
+ # The monitor is held across the yield, as `reopen` does, because a window is opened
178
+ # at file scope before any test runs. `activate` deliberately does not, since it
179
+ # yields to arbitrary test code.
180
+ def declare_fixtures
181
+ @monitor.synchronize do
182
+ unless @activations.empty?
183
+ raise ArgumentError,
184
+ "a declaration window cannot be opened inside with_subscribers, because closing it rebuilds the " \
185
+ "registry and would discard the activation"
186
+ end
187
+
188
+ previously = @window
189
+ @window = true
190
+ begin
191
+ yield
192
+ ensure
193
+ @window = previously
194
+ end
195
+
196
+ # Rebuilding here is what puts window-declared contracts in the index. Nothing
197
+ # is eager-loaded: the window body has already run.
198
+ @snapshot = build_snapshot(extra_subscribers: @active_fixtures) if @snapshot
199
+ end
200
+ end
201
+
202
+ # Activates dormant fixture subscribers for the duration of the block, then restores
203
+ # the previous snapshot. The block runs outside the monitor: a test may spawn a
204
+ # thread that calls `prepare`, and holding the lock across the yield would deadlock
205
+ # it.
206
+ def activate(job_classes)
207
+ restore = nil
208
+
209
+ @monitor.synchronize do
210
+ validate_activation!(job_classes)
211
+
212
+ # Built before anything is committed: `build_snapshot` applies the same
213
+ # validation preparation does, so an abstract or context-less fixture raises
214
+ # here. Assigning first would leave the failed fixture active for every later
215
+ # activation in the process.
216
+ # Deduplicated: activating a fixture that an enclosing block already activated
217
+ # must not register it twice, which would double its fanout -- the same defect
218
+ # `prune_stale_declarations` exists to prevent on the reload path.
219
+ fixtures = (@active_fixtures + job_classes).uniq
220
+ candidate = build_snapshot(extra_subscribers: fixtures)
221
+
222
+ restore = [ @snapshot, @active_fixtures ]
223
+ @snapshot = candidate
224
+ @active_fixtures = fixtures
225
+ @activations.push(restore)
226
+ end
227
+
228
+ begin
229
+ yield
230
+ ensure
231
+ @monitor.synchronize do
232
+ @snapshot, @active_fixtures = restore
233
+ @activations.pop
234
+ end
235
+ end
236
+ end
237
+
238
+ def fixtures
239
+ @fixtures.dup.freeze
240
+ end
241
+
103
242
  def reset!
104
243
  @monitor.synchronize do
105
244
  @pending = []
106
245
  @snapshot = nil
107
246
  @building = false
247
+ @window = false
248
+ @reloading = false
249
+ @active_fixtures = []
250
+ @activations = []
251
+ # @fixtures is deliberately not cleared, for the same reason the pending list is
252
+ # pruned rather than emptied: a fixture is declared once at file scope and its
253
+ # window never runs again, so clearing it would leave every later activation in
254
+ # the process unable to find it. Fixtures are dormant, so keeping them changes
255
+ # no snapshot.
108
256
  end
109
257
  end
110
258
 
@@ -118,8 +266,11 @@ module EventRail
118
266
  loader = Rails.autoloaders.main
119
267
  return unless loader.respond_to?(:eager_load_dir)
120
268
 
269
+ roots = configured_roots
270
+ validate_roots!(roots, loader)
271
+
121
272
  loader.dirs.each do |dir|
122
- next unless CONVENTIONAL_ROOTS.any? { |root| dir.end_with?("/#{root}") }
273
+ next unless roots.any? { |root| dir.end_with?("/#{root}") }
123
274
  next unless Dir.exist?(dir)
124
275
 
125
276
  begin
@@ -132,6 +283,75 @@ module EventRail
132
283
  end
133
284
  end
134
285
 
286
+ # Both late-declaration messages share this. It interpolates the configured roots
287
+ # rather than hardcoding the defaults, and it names the ordering fact that makes the
288
+ # obvious fix wrong: Rails runs prepare callbacks before `eager_load!`, so adding a
289
+ # directory to `eager_load_paths` is always too late.
290
+ def discovery_advice
291
+ # Falls back to the default rather than raising: this builds the text of another
292
+ # error, and a misconfigured roots value has its own error from `validate_roots!`
293
+ # at preparation.
294
+ roots = configured_roots
295
+ roots = DEFAULT_ROOTS.dup unless well_formed_roots?(roots)
296
+ "EventRail discovers #{roots.join(" and ")} in the application and its engines, and discovery runs " \
297
+ "before eager_load!, so adding the directory to eager_load_paths is not enough: move the file under " \
298
+ "#{roots.first}, or add its root to config.event_rail.roots (which eager-loads that directory " \
299
+ "during preparation in every environment)"
300
+ end
301
+
302
+ # `to_s` on a class whose `name` was overridden still reports the anonymous form, and
303
+ # the name is what an adopter searches for.
304
+ def named(klass)
305
+ klass.name || klass.inspect
306
+ end
307
+
308
+ # Relative to the application root, so the message names the file the way an editor
309
+ # does rather than with an absolute path.
310
+ def at(location)
311
+ path = location.path
312
+ root = Rails.root.to_s if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
313
+ path = path.delete_prefix("#{root}/") if root
314
+
315
+ "#{path}:#{location.lineno}"
316
+ end
317
+
318
+ # Copied on every read, so the registry never holds a reference the application can
319
+ # mutate afterwards and repeated reloads cannot accumulate entries.
320
+ def configured_roots
321
+ configured =
322
+ if defined?(Rails) && Rails.respond_to?(:application) && Rails.application
323
+ Rails.application.config.event_rail&.roots
324
+ end
325
+
326
+ (configured || DEFAULT_ROOTS).dup
327
+ end
328
+
329
+ def well_formed_roots?(roots)
330
+ roots.is_a?(Array) && roots.all? { |root| root.is_a?(String) && !root.empty? }
331
+ end
332
+
333
+ # Only entries beyond the default are checked. Rails registers an `app/*` directory
334
+ # with the main loader only when it exists, so a freshly generated application that
335
+ # has not created `app/events` yet has no such root -- validating the defaults would
336
+ # fail its boot before it had written a single event. A missing default is a
337
+ # convention not yet used; a missing entry the application typed is a mistake.
338
+ def validate_roots!(roots, loader)
339
+ unless well_formed_roots?(roots)
340
+ raise ConfigurationError,
341
+ "config.event_rail.roots must be an array of non-empty strings; got #{roots.inspect}"
342
+ end
343
+
344
+ dirs = loader.dirs.map(&:to_s)
345
+
346
+ (roots - DEFAULT_ROOTS).each do |root|
347
+ next if dirs.any? { |dir| dir.end_with?("/#{root}") }
348
+
349
+ raise ConfigurationError,
350
+ "config.event_rail.roots names #{root.inspect}, which is not an autoload root of the application " \
351
+ "or any engine"
352
+ end
353
+ end
354
+
135
355
  # A reload replaces class objects while leaving the previous ones reachable
136
356
  # from this list. An entry survives only if the constant its own name denotes
137
357
  # is still this exact object, which is what distinguishes a live class from a
@@ -156,11 +376,17 @@ module EventRail
156
376
  resolved.equal?(klass)
157
377
  end
158
378
 
159
- def build_snapshot
379
+ # `extra_subscribers` carries fixtures activated for a block. They are not added to
380
+ # the pending list, so the next rebuild without them drops them again.
381
+ def build_snapshot(extra_subscribers: EMPTY_SUBSCRIBERS)
160
382
  contracts = build_contracts
161
383
  subscribers = {}
162
384
 
163
- @pending.each do |job_class|
385
+ # The pending list is filtered here, not only pruned by preparation. A snapshot
386
+ # may be built without a preceding prune -- `activate` does exactly that -- and
387
+ # the list can hold an entry whose macro ran before a later argument raised, so
388
+ # a snapshot must never validate a class the constant no longer denotes.
389
+ (@pending.select { |job_class| live?(job_class) } + extra_subscribers).uniq.each do |job_class|
164
390
  validate_subscriber!(job_class)
165
391
 
166
392
  job_class.event_rail_subscriptions.each do |event_class|
@@ -186,6 +412,30 @@ module EventRail
186
412
  ContractIndex.build(concrete)
187
413
  end
188
414
 
415
+ # These are caller mistakes in a test, not failures of the library's declaration
416
+ # rules, so they raise ArgumentError: an adopter rescuing EventRail::Error should
417
+ # not catch them.
418
+ def validate_activation!(job_classes)
419
+ job_classes.each do |job_class|
420
+ if job_class.name.nil?
421
+ raise ArgumentError,
422
+ "#{job_class.inspect} has no name, and Active Job cannot enqueue a job it cannot name; " \
423
+ "assign the class to a constant"
424
+ end
425
+
426
+ next if @fixtures.include?(job_class)
427
+
428
+ if @pending.include?(job_class)
429
+ raise ArgumentError,
430
+ "#{job_class} is already a live subscriber; with_subscribers is for fixtures declared in a test"
431
+ end
432
+
433
+ raise ArgumentError,
434
+ "#{job_class} was not declared inside EventRail::TestHelper.declare, so with_subscribers cannot " \
435
+ "activate it"
436
+ end
437
+ end
438
+
189
439
  def validate_subscriber!(job_class)
190
440
  unless job_class.instance_methods(false).include?(:perform) ||
191
441
  job_class.private_instance_methods(false).include?(:perform)
@@ -6,10 +6,31 @@ module EventRail
6
6
  # eager_load!, so no reloadable constant outside a conventional root can already be
7
7
  # loaded here -- which is exactly why one declared there raises later instead of
8
8
  # silently receiving nothing.
9
+ # The directories discovery scans, matched as a path suffix against the main
10
+ # autoloader's roots. An application appends to it; the default is a mutable copy, so
11
+ # `config.event_rail.roots << "app/subscribers"` works.
12
+ config.event_rail = ActiveSupport::OrderedOptions.new
13
+ config.event_rail.roots = Internal::Registry::DEFAULT_ROOTS.dup
14
+
9
15
  config.to_prepare do
10
16
  # Unqualified, so lexical lookup reaches the private Internal namespace that a
11
17
  # qualified EventRail::Internal reference would be refused.
12
18
  Internal::Registry.prepare
13
19
  end
20
+
21
+ # A reload deletes the constants and only then runs prepare callbacks, and nothing
22
+ # clears the snapshot in between -- so a declaration arriving in that window would be
23
+ # rejected for lateness even though the imminent prepare would have registered it. That
24
+ # window is reachable by any prepare callback ordered ahead of EventRail's, which is the
25
+ # case for one registered directly on the reloader from an initializer, or belonging to
26
+ # a gem that loads earlier in the Gemfile.
27
+ #
28
+ # An initializer rather than a prepare block: registering the callback from `prepare`
29
+ # would add it again on every reload.
30
+ initializer "event_rail.mark_reload_window" do |app|
31
+ if app.config.reloading_enabled?
32
+ app.reloader.before_class_unload { Internal::Registry.reloading! }
33
+ end
34
+ end
14
35
  end
15
36
  end
@@ -16,10 +16,12 @@ module EventRail
16
16
  # missing that inclusion fails application preparation with a precise message
17
17
  # instead of a bare NoMethodError on the macro.
18
18
  module Subscriptions
19
- # Declares the exact event classes this job handles. The relationship is recorded
20
- # for preparation to validate: `perform` is usually defined after this line, and
21
- # whether this class has subclasses is not knowable until everything is loaded, so
22
- # only what can be judged immediately is judged here.
19
+ # Declares the exact event classes this job handles. Only what can be judged
20
+ # immediately is judged here: that each argument is an event class, that it declares an
21
+ # event_type or a version, that it is not a repeat, and that this job has a name. The rest
22
+ # is recorded for preparation to validate, because `perform` is usually defined after
23
+ # this line and whether this class has subclasses is not knowable until everything is
24
+ # loaded.
23
25
  def subscribes_to(*event_classes)
24
26
  if event_classes.empty?
25
27
  raise DeclarationError, "#{self} must name at least one event class to subscribe to"
@@ -32,6 +34,19 @@ module EventRail
32
34
  raise DeclarationError,
33
35
  "#{self} cannot subscribe to #{event_class.inspect}, which is not an EventRail::Event class"
34
36
  end
37
+ # Judged here rather than during preparation because it is already knowable: the
38
+ # constant reference above has loaded the event class, so in any ordinary load order
39
+ # its contract is final. A class that declares neither half is an abstract base, or a
40
+ # subclass that declared none of its own, and either way publication -- which matches
41
+ # subscribers by exact class -- could never reach it.
42
+ unless event_class.concrete?
43
+ raise DeclarationError,
44
+ "#{self} cannot subscribe to #{event_class}, which declares neither event_type nor version and so " \
45
+ "can never be published; the subscription would receive nothing. Neither declaration is inherited, " \
46
+ "so a subclass of a concrete event needs its own. Subscribe to each concrete event class instead, " \
47
+ "or, if #{event_class} declares its contract outside its own class body, make that run before this " \
48
+ "subscription is declared."
49
+ end
35
50
  if own.include?(event_class)
36
51
  raise DeclarationError, "#{self} already declares a subscription to #{event_class}"
37
52
  end
@@ -39,7 +54,20 @@ module EventRail
39
54
  own << event_class
40
55
  end
41
56
 
42
- Internal::Registry.declare(self)
57
+ # Checked here rather than during preparation, and after the arguments above rather
58
+ # than before them. Active Job cannot enqueue a job it cannot name, so an anonymous
59
+ # subscriber is never a working configuration -- but preparation is the wrong place to
60
+ # say so: the macro has already recorded the class by then, and rejecting it there
61
+ # would make every later rebuild in the process fail, including in unrelated tests.
62
+ # After the argument checks, because a bad argument on an anonymous class deserves the
63
+ # specific error rather than this one.
64
+ if name.nil?
65
+ raise DeclarationError,
66
+ "#{inspect} declared a subscription but has no name, and Active Job cannot enqueue a job it cannot " \
67
+ "name; assign the class to a constant"
68
+ end
69
+
70
+ Internal::Registry.declare(self, caller_locations(1, 1).first)
43
71
  include Internal::SubscriberExecution unless include?(Internal::SubscriberExecution)
44
72
 
45
73
  event_rail_subscriptions
@@ -0,0 +1,74 @@
1
+ require "event_rail"
2
+
3
+ module EventRail
4
+ # Test support, loaded only by an explicit `require "event_rail/test_helper"`. Requiring
5
+ # the library does not load it, and requiring it does not by itself weaken the rule that
6
+ # a declaration arriving after preparation is an error.
7
+ #
8
+ # # test/test_helper.rb
9
+ # require "event_rail/test_helper"
10
+ #
11
+ # class ActiveSupport::TestCase
12
+ # include EventRail::TestHelper
13
+ # end
14
+ #
15
+ # Including the module adds `with_subscribers` and nothing else. `declare` stays a module
16
+ # method rather than becoming an instance method on every test case, because the name is
17
+ # general enough to collide.
18
+ module TestHelper
19
+ # Opens a window in which event contracts and subscriptions may be declared even though
20
+ # preparation has already sealed the registry. Fixtures go at file scope, once per
21
+ # process:
22
+ #
23
+ # EventRail::TestHelper.declare do
24
+ # module OrderTests
25
+ # class Placed < EventRail::Event
26
+ # event_type "tests.order_placed"
27
+ # version 1
28
+ # default_source "tests"
29
+ #
30
+ # attribute :order_id, :string
31
+ # end
32
+ #
33
+ # class AuditJob < ApplicationJob
34
+ # subscribes_to Placed
35
+ #
36
+ # def perform(event) = (self.class.seen << event)
37
+ # end
38
+ # end
39
+ # end
40
+ #
41
+ # Contracts declared here are indexed when the window closes and stay indexed: a class
42
+ # cannot be unloaded, so two fixtures claiming one event type and version fail every
43
+ # later rebuild, not just the first. Give each fixture a unique type, and declare it at
44
+ # file scope -- a declaration inside a test method reopens the same constant and runs the
45
+ # writer again on a sealed registry.
46
+ #
47
+ # Subscriptions declared here are dormant. They receive nothing until `with_subscribers`
48
+ # activates them, so a fixture cannot fan out in a later test that never mentions it.
49
+ def self.declare(&block)
50
+ # Unqualified, so lexical lookup reaches the private Internal namespace that a
51
+ # qualified EventRail::Internal reference would be refused.
52
+ Internal::Registry.declare_fixtures(&block)
53
+ end
54
+
55
+ # Activates fixture subscribers for the duration of the block and restores the previous
56
+ # registry afterwards, including when the block raises. Nested calls are additive.
57
+ #
58
+ # test "publication fans out to the audit job" do
59
+ # with_subscribers(OrderTests::AuditJob) do
60
+ # publication = EventRail.publish(OrderTests::Placed.new(order_id: "o-1"))
61
+ #
62
+ # assert_enqueued_with job: OrderTests::AuditJob, args: [ publication.event ]
63
+ # end
64
+ # end
65
+ #
66
+ # The registry snapshot this replaces is process-wide, so activation is not safe under
67
+ # thread-based test parallelisation (`parallelize(with: :threads)`). Process-based
68
+ # parallelisation, which is the Rails default, is unaffected because each worker has its
69
+ # own registry.
70
+ def with_subscribers(*job_classes, &block)
71
+ Internal::Registry.activate(job_classes.flatten, &block)
72
+ end
73
+ end
74
+ end
@@ -1,3 +1,3 @@
1
1
  module EventRail
2
- VERSION = "0.1.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_rail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Dmitriev
@@ -153,6 +153,7 @@ files:
153
153
  - lib/event_rail/publish.rb
154
154
  - lib/event_rail/railtie.rb
155
155
  - lib/event_rail/subscriptions.rb
156
+ - lib/event_rail/test_helper.rb
156
157
  - lib/event_rail/version.rb
157
158
  homepage: https://github.com/alexdmtv/event-rail
158
159
  licenses: