supervisor_machines 0.1.4 → 0.4.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.
@@ -3,20 +3,41 @@
3
3
  //! `Supervisor` carries its child count and its restart history in the type,
4
4
  //! so a runtime child count is dispatched to one monomorphised shape per
5
5
  //! supported count and kept behind [`Decide`].
6
+ //!
7
+ //! Every mutating method checks frozen first, then converts and checks all of
8
+ //! its arguments and looks up every class it answers with, before it calls
9
+ //! the core; afterwards it
10
+ //! only builds values from what the core returned. `stopped` answers with a
11
+ //! `Restart` built by Ruby, so it runs in `matryona_machines`'s
12
+ //! clone-then-keep: decided on a copy, kept only once that answer exists and
13
+ //! the decider is neither frozen nor changed. A raise while building it
14
+ //! changes nothing, freezing the decider meanwhile raises `FrozenError` and a
15
+ //! re-entrant change `RuntimeError`, in both cases with the outer call not
16
+ //! applied. No `RefCell` borrow is held while Ruby code runs.
6
17
 
7
18
  use std::cell::RefCell;
19
+ use std::ops::{Deref, DerefMut};
20
+ use std::time::Duration;
8
21
 
9
- use magnus::scan_args::{get_kwargs, scan_args};
10
- use magnus::{Error, RArray, RHash, RModule, Ruby, Value, function, method, prelude::*};
11
- use supervisor_machines::{Decision, Exit, Intensity, Policy, Strategy, Supervisor};
22
+ use magnus::typed_data::Obj;
23
+ use magnus::{Error, Integer, RArray, RModule, Ruby, Value, function, method, prelude::*};
24
+ use matryona_machines::ruby::{
25
+ Class, Symbols, class_name, duration, exact_array_with, integer, keep, nanoseconds,
26
+ };
27
+ use supervisor_machines::{
28
+ Decision, Exit, Intensity, Policy, Recording, Refusal, Span, Strategy, Supervisor,
29
+ };
12
30
 
13
- use crate::{Whole, decision, exit, policy, strategy, strategy_name};
31
+ use crate::{
32
+ DecisionClasses, Exits, Policies, Recordings, Refusals, Strategies, frozen, intensity_from,
33
+ intensity_value,
34
+ };
14
35
 
15
- /// Restart timestamps every shape keeps.
36
+ /// Restart instants every shape keeps.
16
37
  const HISTORY: usize = 64;
17
38
 
18
39
  /// The core escalates one restart past the intensity, so the history has to
19
- /// hold one more timestamp than the intensity tolerates.
40
+ /// hold one more instant than the intensity tolerates.
20
41
  #[expect(clippy::cast_possible_truncation)]
21
42
  const MAX_RESTARTS: u32 = HISTORY as u32 - 1;
22
43
 
@@ -28,10 +49,29 @@ const SUPPORTED_CHILDREN: [usize; 17] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
28
49
  trait Decide: Send {
29
50
  fn children(&self) -> usize;
30
51
  fn strategy(&self) -> Strategy;
52
+ fn intensity(&self) -> Intensity;
31
53
  fn terminated(&self) -> bool;
32
- fn recent_restarts(&self, now_ns: u64) -> u32;
33
- fn restart_delay_ns(&self, base_ns: u64, ceiling_ns: u64, now_ns: u64) -> u64;
34
- fn stopped(&mut self, index: usize, policy: Policy, exit: Exit, now_ns: u64) -> Decision;
54
+ fn history(&self) -> Vec<Duration>;
55
+ fn span(&self, index: usize) -> Option<Span>;
56
+ fn recent_restarts(&self, now: Duration) -> u32;
57
+ fn restart_delay(&self, base: Duration, ceiling: Duration, now: Duration) -> Duration;
58
+ fn stopped(
59
+ &mut self,
60
+ index: usize,
61
+ policy: Policy,
62
+ exit: Exit,
63
+ now: Duration,
64
+ ) -> Result<Decision, Refusal>;
65
+ fn record_external_restart(&mut self, now: Duration) -> Recording;
66
+ fn absorb(&mut self, history: &[Duration], now: Duration) -> usize;
67
+ fn duplicate(&self) -> Box<dyn Decide>;
68
+
69
+ /// Whether `other` holds the same state. The shape, strategy and
70
+ /// intensity never change after construction, so the history and the
71
+ /// escalation are all there is to compare.
72
+ fn same(&self, other: &dyn Decide) -> bool {
73
+ self.terminated() == other.terminated() && self.history() == other.history()
74
+ }
35
75
  }
36
76
 
37
77
  /// One monomorphised supervision tree. The newtype keeps the trait's method
@@ -47,110 +87,274 @@ impl<const CHILDREN: usize> Decide for Shape<CHILDREN> {
47
87
  self.0.strategy()
48
88
  }
49
89
 
90
+ fn intensity(&self) -> Intensity {
91
+ self.0.intensity()
92
+ }
93
+
50
94
  fn terminated(&self) -> bool {
51
95
  self.0.terminated()
52
96
  }
53
97
 
54
- fn recent_restarts(&self, now_ns: u64) -> u32 {
55
- self.0.recent_restarts(now_ns)
98
+ fn history(&self) -> Vec<Duration> {
99
+ self.0.history().collect()
100
+ }
101
+
102
+ fn span(&self, index: usize) -> Option<Span> {
103
+ self.0.span(index)
104
+ }
105
+
106
+ fn recent_restarts(&self, now: Duration) -> u32 {
107
+ self.0.recent_restarts(now)
108
+ }
109
+
110
+ fn restart_delay(&self, base: Duration, ceiling: Duration, now: Duration) -> Duration {
111
+ self.0.restart_delay(base, ceiling, now)
112
+ }
113
+
114
+ fn stopped(
115
+ &mut self,
116
+ index: usize,
117
+ policy: Policy,
118
+ exit: Exit,
119
+ now: Duration,
120
+ ) -> Result<Decision, Refusal> {
121
+ self.0.stopped(index, policy, exit, now)
122
+ }
123
+
124
+ fn record_external_restart(&mut self, now: Duration) -> Recording {
125
+ self.0.record_external_restart(now)
126
+ }
127
+
128
+ fn absorb(&mut self, history: &[Duration], now: Duration) -> usize {
129
+ self.0.absorb(history, now)
130
+ }
131
+
132
+ fn duplicate(&self) -> Box<dyn Decide> {
133
+ Box::new(Self(self.0))
134
+ }
135
+ }
136
+
137
+ /// A decider's tree, copied by [`Decide::duplicate`] and compared by
138
+ /// [`Decide::same`], so clone-then-keep can hold it.
139
+ struct Tree(Box<dyn Decide>);
140
+
141
+ impl Clone for Tree {
142
+ fn clone(&self) -> Self {
143
+ Self(self.0.duplicate())
144
+ }
145
+ }
146
+
147
+ impl PartialEq for Tree {
148
+ fn eq(&self, other: &Self) -> bool {
149
+ self.0.same(&*other.0)
56
150
  }
151
+ }
152
+
153
+ impl Deref for Tree {
154
+ type Target = dyn Decide;
57
155
 
58
- fn restart_delay_ns(&self, base_ns: u64, ceiling_ns: u64, now_ns: u64) -> u64 {
59
- self.0.restart_delay_ns(base_ns, ceiling_ns, now_ns)
156
+ fn deref(&self) -> &Self::Target {
157
+ &*self.0
60
158
  }
159
+ }
61
160
 
62
- fn stopped(&mut self, index: usize, policy: Policy, exit: Exit, now_ns: u64) -> Decision {
63
- self.0.stopped(index, policy, exit, now_ns)
161
+ impl DerefMut for Tree {
162
+ fn deref_mut(&mut self) -> &mut Self::Target {
163
+ &mut *self.0
64
164
  }
65
165
  }
66
166
 
167
+ /// A saved history and the instant it is restored at.
168
+ type Saved<'a> = Option<(&'a [Duration], Duration)>;
169
+
67
170
  /// Each shape is built behind its own call frame. Inlined, a debug build sizes
68
- /// one frame for every arm of the match in [`Wrapper::new`] at once, which
171
+ /// one frame for every arm of the match in [`Wrapper::build`] at once, which
69
172
  /// overflows Ruby's fiber stack.
70
173
  #[inline(never)]
71
- fn boxed<const CHILDREN: usize>(strategy: Strategy, intensity: Intensity) -> Box<dyn Decide> {
72
- Box::new(Shape(Supervisor::<CHILDREN, HISTORY>::new(
73
- strategy, intensity,
74
- )))
174
+ fn boxed<const CHILDREN: usize>(
175
+ strategy: Strategy,
176
+ intensity: Intensity,
177
+ saved: Saved<'_>,
178
+ ) -> Option<Box<dyn Decide>> {
179
+ let tree = match saved {
180
+ None => Supervisor::<CHILDREN, HISTORY>::new(strategy, intensity),
181
+ Some((history, now)) => {
182
+ Supervisor::restore(strategy, intensity, history.iter().copied(), now)
183
+ }
184
+ }?;
185
+ Some(Box::new(Shape(tree)))
186
+ }
187
+
188
+ fn shape(
189
+ children: usize,
190
+ strategy: Strategy,
191
+ intensity: Intensity,
192
+ saved: Saved<'_>,
193
+ ) -> Option<Box<dyn Decide>> {
194
+ match children {
195
+ 1 => boxed::<1>(strategy, intensity, saved),
196
+ 2 => boxed::<2>(strategy, intensity, saved),
197
+ 3 => boxed::<3>(strategy, intensity, saved),
198
+ 4 => boxed::<4>(strategy, intensity, saved),
199
+ 5 => boxed::<5>(strategy, intensity, saved),
200
+ 6 => boxed::<6>(strategy, intensity, saved),
201
+ 7 => boxed::<7>(strategy, intensity, saved),
202
+ 8 => boxed::<8>(strategy, intensity, saved),
203
+ 9 => boxed::<9>(strategy, intensity, saved),
204
+ 10 => boxed::<10>(strategy, intensity, saved),
205
+ 11 => boxed::<11>(strategy, intensity, saved),
206
+ 12 => boxed::<12>(strategy, intensity, saved),
207
+ 13 => boxed::<13>(strategy, intensity, saved),
208
+ 14 => boxed::<14>(strategy, intensity, saved),
209
+ 15 => boxed::<15>(strategy, intensity, saved),
210
+ 16 => boxed::<16>(strategy, intensity, saved),
211
+ 32 => boxed::<32>(strategy, intensity, saved),
212
+ _ => None,
213
+ }
214
+ }
215
+
216
+ /// Integer nanosecond instants, all converted before the core sees any. Only
217
+ /// an `Array` is read, never a subclass that could answer for its elements.
218
+ fn instants(ruby: &Ruby, value: Value) -> Result<Vec<Duration>, Error> {
219
+ let array = exact_array_with(ruby, value, || {
220
+ format!(
221
+ "history must be an Array of Integers, not {}",
222
+ class_name(value)
223
+ )
224
+ })?;
225
+ array
226
+ .into_iter()
227
+ .map(|item| duration(item, "history"))
228
+ .collect()
75
229
  }
76
230
 
77
- fn unsupported(ruby: &Ruby, children: usize) -> Error {
231
+ /// The `RangeError` for an intensity the history cannot enforce.
232
+ fn cannot_escalate(ruby: &Ruby, intensity: Intensity) -> Error {
78
233
  Error::new(
79
- ruby.exception_arg_error(),
234
+ ruby.exception_range_error(),
80
235
  format!(
81
- "{children} children is not a supported shape, expected one of {SUPPORTED_CHILDREN:?}"
236
+ "{} restarts cannot escalate, the most is {MAX_RESTARTS}",
237
+ intensity.restarts()
82
238
  ),
83
239
  )
84
240
  }
85
241
 
242
+ /// What `Supervisor.new` runs under: a `SupervisorMachines::Intensity` read
243
+ /// natively (exact class, fields from the struct), rebuilt through the core,
244
+ /// and refused past [`MAX_RESTARTS`] with the errors `Decider.new` gives, so
245
+ /// a tree that cannot escalate fails before it takes on a child.
246
+ pub fn supervised_intensity_hook(ruby: &Ruby, value: Value) -> Result<Value, Error> {
247
+ let intensity = intensity_from(ruby, value)?;
248
+ if intensity.restarts() > MAX_RESTARTS {
249
+ return Err(cannot_escalate(ruby, intensity));
250
+ }
251
+ intensity_value(ruby, intensity)
252
+ }
253
+
86
254
  #[magnus::wrap(class = "SupervisorMachines::Decider", free_immediately, size)]
87
- struct Wrapper(RefCell<Box<dyn Decide>>);
255
+ struct Wrapper(RefCell<Tree>);
88
256
 
89
257
  impl Wrapper {
90
- fn new(ruby: &Ruby, args: &[Value]) -> Result<Self, Error> {
91
- let args = scan_args::<(), (), (), (), RHash, ()>(args)?;
92
- let (Whole(children), name, Whole(restarts), Whole(window_ns)) =
93
- get_kwargs::<_, (Whole<usize>, Value, Whole<u32>, Whole<u64>), (), ()>(
94
- args.keywords,
95
- &["children", "strategy", "restarts", "window_ns"],
96
- &[],
97
- )?
98
- .required;
99
- if restarts > MAX_RESTARTS {
100
- return Err(Error::new(
101
- ruby.exception_arg_error(),
102
- format!("{restarts} restarts cannot escalate, the most is {MAX_RESTARTS}"),
103
- ));
104
- }
105
- let strategy = strategy(ruby, name)?;
106
- let intensity = Intensity::new(restarts, window_ns);
107
- let tree = match children {
108
- 1 => boxed::<1>(strategy, intensity),
109
- 2 => boxed::<2>(strategy, intensity),
110
- 3 => boxed::<3>(strategy, intensity),
111
- 4 => boxed::<4>(strategy, intensity),
112
- 5 => boxed::<5>(strategy, intensity),
113
- 6 => boxed::<6>(strategy, intensity),
114
- 7 => boxed::<7>(strategy, intensity),
115
- 8 => boxed::<8>(strategy, intensity),
116
- 9 => boxed::<9>(strategy, intensity),
117
- 10 => boxed::<10>(strategy, intensity),
118
- 11 => boxed::<11>(strategy, intensity),
119
- 12 => boxed::<12>(strategy, intensity),
120
- 13 => boxed::<13>(strategy, intensity),
121
- 14 => boxed::<14>(strategy, intensity),
122
- 15 => boxed::<15>(strategy, intensity),
123
- 16 => boxed::<16>(strategy, intensity),
124
- 32 => boxed::<32>(strategy, intensity),
125
- _ => return Err(unsupported(ruby, children)),
126
- };
127
- Ok(Self(RefCell::new(tree)))
128
- }
129
-
130
- /// Feeds one stopped child to the core and returns what it decided:
131
- /// `:ignore`, `:escalate`, or the inclusive range of children to restart.
132
- fn stopped(ruby: &Ruby, rb_self: &Self, args: &[Value]) -> Result<Value, Error> {
133
- let args = scan_args::<(), (), (), (), RHash, ()>(args)?;
134
- let (Whole(index), name, ending, Whole(now_ns)) =
135
- get_kwargs::<_, (Whole<usize>, Value, Value, Whole<u64>), (), ()>(
136
- args.keywords,
137
- &["index", "policy", "exit", "now_ns"],
138
- &[],
139
- )?
140
- .required;
141
- let (policy, ending) = (policy(ruby, name)?, exit(ruby, ending)?);
142
- let children = rb_self.0.borrow().children();
143
- if index >= children {
258
+ /// `RangeError` for a child count with no shape, and for an intensity the
259
+ /// history cannot enforce (the core's `Supervisor::new` answering `None`).
260
+ fn build(
261
+ ruby: &Ruby,
262
+ children: Value,
263
+ name: Value,
264
+ intensity: Value,
265
+ saved: Saved<'_>,
266
+ ) -> Result<Self, Error> {
267
+ let children: usize = integer(children, "children")?;
268
+ let strategy = Strategies::parse(ruby, name)?;
269
+ let intensity = intensity_from(ruby, intensity)?;
270
+ if !SUPPORTED_CHILDREN.contains(&children) {
144
271
  return Err(Error::new(
145
- ruby.exception_arg_error(),
146
- format!("index {index} is outside the {children} children"),
272
+ ruby.exception_range_error(),
273
+ format!(
274
+ "{children} children is not a supported shape, expected one of \
275
+ {SUPPORTED_CHILDREN:?}"
276
+ ),
147
277
  ));
148
278
  }
149
- let verdict = rb_self
150
- .0
151
- .borrow_mut()
152
- .stopped(index, policy, ending, now_ns);
153
- decision(ruby, verdict)
279
+ let tree = shape(children, strategy, intensity, saved)
280
+ .ok_or_else(|| cannot_escalate(ruby, intensity))?;
281
+ Ok(Self(RefCell::new(Tree(tree))))
282
+ }
283
+
284
+ fn new(ruby: &Ruby, children: Value, name: Value, intensity: Value) -> Result<Self, Error> {
285
+ Self::build(ruby, children, name, intensity, None)
286
+ }
287
+
288
+ fn restore(
289
+ ruby: &Ruby,
290
+ children: Value,
291
+ name: Value,
292
+ intensity: Value,
293
+ history: Value,
294
+ now: Value,
295
+ ) -> Result<Self, Error> {
296
+ let (history, now) = (instants(ruby, history)?, duration(now, "now")?);
297
+ Self::build(ruby, children, name, intensity, Some((&history, now)))
298
+ }
299
+
300
+ /// `:ignore`, a `Restart`, `:escalate`, `:already_escalated`, or the
301
+ /// refusal `:unknown_child`. Decided on a copy, kept only once the answer
302
+ /// is built: `RuntimeError` when the decider changed meanwhile.
303
+ fn stopped(
304
+ ruby: &Ruby,
305
+ rb_self: Obj<Self>,
306
+ index: Value,
307
+ name: Value,
308
+ ending: Value,
309
+ now: Value,
310
+ ) -> Result<Value, Error> {
311
+ rb_self.check_frozen()?;
312
+ let index: usize = integer(index, "index")?;
313
+ let (policy, ending) = (Policies::parse(ruby, name)?, Exits::parse(ruby, ending)?);
314
+ let now = duration(now, "now")?;
315
+ let classes = DecisionClasses::fetch(ruby)?;
316
+ keep(
317
+ ruby,
318
+ rb_self,
319
+ &rb_self.0,
320
+ |tree| tree.stopped(index, policy, ending, now),
321
+ |decided| match decided {
322
+ Ok(decision) => classes.decision(ruby, decision),
323
+ Err(refusal) => Ok(Refusals::symbol(ruby, refusal).as_value()),
324
+ },
325
+ )
326
+ }
327
+
328
+ /// `:recorded` or `:outdated`. Never refused.
329
+ fn record_external_restart(
330
+ ruby: &Ruby,
331
+ rb_self: Obj<Self>,
332
+ now: Value,
333
+ ) -> Result<Value, Error> {
334
+ rb_self.check_frozen()?;
335
+ let now = duration(now, "now")?;
336
+ let recording = rb_self.0.borrow_mut().record_external_restart(now);
337
+ Ok(Recordings::symbol(ruby, recording).as_value())
338
+ }
339
+
340
+ /// How many restarts were added, keeping the larger count of each
341
+ /// instant across both histories. Never refused.
342
+ fn absorb(ruby: &Ruby, rb_self: Obj<Self>, history: Value, now: Value) -> Result<usize, Error> {
343
+ rb_self.check_frozen()?;
344
+ let (history, now) = (instants(ruby, history)?, duration(now, "now")?);
345
+ Ok(rb_self.0.borrow_mut().absorb(&history, now))
346
+ }
347
+
348
+ fn history(ruby: &Ruby, rb_self: &Self) -> Result<RArray, Error> {
349
+ let held = rb_self.0.borrow().history();
350
+ frozen(ruby, held.into_iter().map(|at| nanoseconds(ruby, at)))
351
+ }
352
+
353
+ fn span(ruby: &Ruby, rb_self: &Self, index: Value) -> Result<Value, Error> {
354
+ let index: usize = integer(index, "index")?;
355
+ let classes = DecisionClasses::fetch(ruby)?;
356
+ let span = rb_self.0.borrow().span(index);
357
+ span.map_or_else(|| Ok(ruby.qnil().as_value()), |span| classes.span(span))
154
358
  }
155
359
 
156
360
  fn children(&self) -> usize {
@@ -158,32 +362,38 @@ impl Wrapper {
158
362
  }
159
363
 
160
364
  fn strategy(ruby: &Ruby, rb_self: &Self) -> Value {
161
- strategy_name(ruby, rb_self.0.borrow().strategy()).as_value()
365
+ let strategy = rb_self.0.borrow().strategy();
366
+ Strategies::symbol(ruby, strategy).as_value()
367
+ }
368
+
369
+ fn intensity(ruby: &Ruby, rb_self: &Self) -> Result<Value, Error> {
370
+ // Copied out first: `Intensity.new` is Ruby code and may re-enter.
371
+ let intensity = rb_self.0.borrow().intensity();
372
+ intensity_value(ruby, intensity)
162
373
  }
163
374
 
164
375
  fn terminated(&self) -> bool {
165
376
  self.0.borrow().terminated()
166
377
  }
167
378
 
168
- fn recent_restarts(&self, Whole(now_ns): Whole<u64>) -> u32 {
169
- self.0.borrow().recent_restarts(now_ns)
379
+ fn recent_restarts(&self, now: Value) -> Result<u32, Error> {
380
+ let now = duration(now, "now")?;
381
+ Ok(self.0.borrow().recent_restarts(now))
170
382
  }
171
383
 
172
- /// How long to hold off a granted restart, backing off per restart already
173
- /// inside the window.
174
- fn restart_delay_ns(rb_self: &Self, args: &[Value]) -> Result<u64, Error> {
175
- let args = scan_args::<(), (), (), (), RHash, ()>(args)?;
176
- let (Whole(base_ns), Whole(ceiling_ns), Whole(now_ns)) =
177
- get_kwargs::<_, (Whole<u64>, Whole<u64>, Whole<u64>), (), ()>(
178
- args.keywords,
179
- &["base_ns", "ceiling_ns", "now_ns"],
180
- &[],
181
- )?
182
- .required;
183
- Ok(rb_self
184
- .0
185
- .borrow()
186
- .restart_delay_ns(base_ns, ceiling_ns, now_ns))
384
+ fn restart_delay(
385
+ ruby: &Ruby,
386
+ rb_self: &Self,
387
+ base: Value,
388
+ ceiling: Value,
389
+ now: Value,
390
+ ) -> Result<Integer, Error> {
391
+ let (base, ceiling) = (duration(base, "base")?, duration(ceiling, "ceiling")?);
392
+ let now = duration(now, "now")?;
393
+ Ok(nanoseconds(
394
+ ruby,
395
+ rb_self.0.borrow().restart_delay(base, ceiling, now),
396
+ ))
187
397
  }
188
398
 
189
399
  fn inspect(ruby: &Ruby, rb_self: &Self) -> String {
@@ -198,25 +408,31 @@ impl Wrapper {
198
408
 
199
409
  pub fn define(ruby: &Ruby, module: RModule) -> Result<(), Error> {
200
410
  let class = module.define_class("Decider", ruby.class_object())?;
411
+ class.undef_default_alloc_func();
201
412
  class.const_set("HISTORY", HISTORY)?;
202
413
  class.const_set("MAX_RESTARTS", MAX_RESTARTS)?;
203
- class.const_set("SUPPORTED_CHILDREN", supported_children(ruby)?)?;
204
- class.define_singleton_method("new", function!(Wrapper::new, -1))?;
205
- class.define_method("stopped", method!(Wrapper::stopped, -1))?;
414
+ class.const_set(
415
+ "SUPPORTED_CHILDREN",
416
+ frozen(ruby, SUPPORTED_CHILDREN.into_iter())?,
417
+ )?;
418
+ class.define_singleton_method("new", function!(Wrapper::new, 3))?;
419
+ class.define_singleton_method("restore", function!(Wrapper::restore, 5))?;
420
+ class.define_method("stopped", method!(Wrapper::stopped, 4))?;
421
+ class.define_method(
422
+ "record_external_restart",
423
+ method!(Wrapper::record_external_restart, 1),
424
+ )?;
425
+ class.define_method("absorb", method!(Wrapper::absorb, 2))?;
426
+ class.define_method("history", method!(Wrapper::history, 0))?;
427
+ class.define_method("span", method!(Wrapper::span, 1))?;
206
428
  class.define_method("children", method!(Wrapper::children, 0))?;
207
429
  class.define_method("strategy", method!(Wrapper::strategy, 0))?;
430
+ class.define_method("intensity", method!(Wrapper::intensity, 0))?;
208
431
  class.define_method("terminated?", method!(Wrapper::terminated, 0))?;
209
432
  class.define_method("recent_restarts", method!(Wrapper::recent_restarts, 1))?;
210
- class.define_method("restart_delay_ns", method!(Wrapper::restart_delay_ns, -1))?;
433
+ class.define_method("restart_delay", method!(Wrapper::restart_delay, 3))?;
211
434
  class.define_method("inspect", method!(Wrapper::inspect, 0))?;
212
- Ok(())
213
- }
214
-
215
- fn supported_children(ruby: &Ruby) -> Result<RArray, Error> {
216
- let array = ruby.ary_new_capa(SUPPORTED_CHILDREN.len());
217
- for children in SUPPORTED_CHILDREN {
218
- array.push(children)?;
219
- }
220
- array.freeze();
221
- Ok(array)
435
+ // Fix magnus's class for `Wrapper` now: building a Decider (`new`,
436
+ // `restore`) then runs no `Object.const_get` and always answers this class.
437
+ Class::pin_wrapped::<Wrapper>(ruby, class).map(drop)
222
438
  }
@@ -0,0 +1,30 @@
1
+ //! Private native entry point.
2
+ use magnus::{Error, Ruby, function, prelude::*};
3
+ use matryona_machines::ruby::{Symbols, nanoseconds};
4
+ use supervisor_machines::{DEFAULT_RESTARTS, DEFAULT_WINDOW};
5
+
6
+ use crate::{
7
+ Exits, Policies, Strategies, decider, intensity_hook, module, policy_hook, resolve_classes,
8
+ restart_hook, span_hook, strategy_hook,
9
+ };
10
+
11
+ #[magnus::init]
12
+ fn init(ruby: &Ruby) -> Result<(), Error> {
13
+ let module = module(ruby)?;
14
+ module.const_set("DEFAULT_RESTARTS", DEFAULT_RESTARTS)?;
15
+ module.const_set("DEFAULT_WINDOW", nanoseconds(ruby, DEFAULT_WINDOW))?;
16
+ module.const_set("STRATEGIES", Strategies::every(ruby))?;
17
+ module.const_set("POLICIES", Policies::every(ruby))?;
18
+ module.const_set("EXITS", Exits::every(ruby))?;
19
+ resolve_classes(ruby, module)?;
20
+ module.define_singleton_method("__intensity", function!(intensity_hook, 2))?;
21
+ module.define_singleton_method("__span", function!(span_hook, 2))?;
22
+ module.define_singleton_method("__restart", function!(restart_hook, 1))?;
23
+ module.define_singleton_method("__policy", function!(policy_hook, 1))?;
24
+ module.define_singleton_method("__strategy", function!(strategy_hook, 1))?;
25
+ module.define_singleton_method(
26
+ "__supervised_intensity",
27
+ function!(decider::supervised_intensity_hook, 1),
28
+ )?;
29
+ decider::define(ruby, module)
30
+ }