Skip to content

Commit a68af98

Browse files
committed
allow orchestrations to work
Signed-off-by: Robert Landers <landers.robert@gmail.com>
1 parent 97bc8eb commit a68af98

File tree

10 files changed

+74
-40
lines changed

10 files changed

+74
-40
lines changed

src/Events/Event.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,26 @@
2727
use Bottledcode\DurablePhp\MonotonicClock;
2828
use Crell\fp\Evolvable;
2929
use Crell\Serde\Attributes\ClassNameTypeMap;
30+
use DateTimeImmutable;
3031
use Ramsey\Uuid\Uuid;
32+
use Stringable;
3133

3234
#[ClassNameTypeMap(key: 'eventType')]
33-
abstract class Event implements \Stringable
35+
abstract class Event implements Stringable
3436
{
3537
use Evolvable;
3638

37-
public \DateTimeImmutable $timestamp;
39+
public DateTimeImmutable $timestamp;
3840

39-
public function __construct(public string $eventId)
41+
public function __construct(public string $eventId {
42+
set(string $value) {
43+
if ($this instanceof HasInnerEventInterface && ($this->innerEvent ?? null)) {
44+
$this->innerEvent->eventId = $value;
45+
}
46+
$this->eventId = $value;
47+
}
48+
get => $this->eventId;
49+
})
4050
{
4151
$this->eventId = $this->eventId ?: Uuid::uuid7();
4252
$this->timestamp = MonotonicClock::current()->now();

src/Events/HasInnerEventInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@
2626

2727
interface HasInnerEventInterface
2828
{
29+
public Event $innerEvent { get; }
30+
2931
public function getInnerEvent(): Event;
3032
}

src/Events/WithActivity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
class WithActivity extends Event implements HasInnerEventInterface, StateTargetInterface
3232
{
33-
public function __construct(string $eventId, public StateId $target, private readonly Event $innerEvent)
33+
public function __construct(string $eventId, public StateId $target, public Event $innerEvent)
3434
{
3535
parent::__construct($eventId);
3636
}

src/Events/WithDelay.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@
2424

2525
namespace Bottledcode\DurablePhp\Events;
2626

27+
use DateTimeImmutable;
2728
use Ramsey\Uuid\Uuid;
2829

2930
class WithDelay extends Event implements HasInnerEventInterface
3031
{
31-
public function __construct(string $eventId, public \DateTimeImmutable $fireAt, public Event $innerEvent)
32+
public function __construct(string $eventId, public DateTimeImmutable $fireAt, public Event $innerEvent)
3233
{
3334
parent::__construct($this->innerEvent ?: Uuid::uuid7());
3435
}
3536

36-
public static function forEvent(\DateTimeImmutable $fireAt, Event $innerEvent): static
37+
public static function forEvent(DateTimeImmutable $fireAt, Event $innerEvent): static
3738
{
3839
return new static(
3940
$innerEvent->eventId,
@@ -44,7 +45,6 @@ public static function forEvent(\DateTimeImmutable $fireAt, Event $innerEvent):
4445

4546
public function getInnerEvent(): Event
4647
{
47-
$this->innerEvent->eventId = $this->eventId;
4848
return $this->innerEvent;
4949
}
5050

src/Events/WithEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
class WithEntity extends Event implements HasInnerEventInterface, StateTargetInterface
3030
{
31-
public function __construct(string $eventId, public StateId $target, private readonly Event $innerEvent)
31+
public function __construct(string $eventId, public StateId $target, public Event $innerEvent)
3232
{
3333
parent::__construct($eventId);
3434
}

src/Events/WithFrom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class WithFrom extends Event implements HasInnerEventInterface
88
{
9-
public function __construct(string $eventId, public StateId $from, private readonly Event $innerEvent)
9+
public function __construct(string $eventId, public StateId $from, public readonly Event $innerEvent)
1010
{
1111
parent::__construct($eventId);
1212
}

src/Events/WithOrchestration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class WithOrchestration extends Event implements HasInnerEventInterface, StateTa
3232
public function __construct(
3333
string $eventId,
3434
public StateId $target,
35-
private readonly Event $innerEvent,
35+
public readonly Event $innerEvent,
3636
) {
3737
parent::__construct($this->innerEvent->eventId ?: Uuid::uuid7());
3838
}

src/Events/WithPriority.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
class WithPriority extends Event implements HasInnerEventInterface
3030
{
31-
private function __construct(public string $eventId, public int $priority, private Event $innerEvent)
31+
private function __construct(public string $eventId, public int $priority, public Event $innerEvent)
3232
{
3333
parent::__construct($this->eventId ?? Uuid::uuid7()->toString());
3434
}
@@ -58,7 +58,8 @@ public function __toString(): string
5858
return sprintf('WithPriority(%d, %s)', $this->priority, $this->innerEvent);
5959
}
6060

61-
#[\Override] public function getInnerEvent(): Event
61+
#[\Override]
62+
public function getInnerEvent(): Event
6263
{
6364
return $this->innerEvent;
6465
}

src/OrchestrationContext.php

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Bottledcode\DurablePhp\Events\WithActivity;
3636
use Bottledcode\DurablePhp\Events\WithDelay;
3737
use Bottledcode\DurablePhp\Events\WithEntity;
38+
use Bottledcode\DurablePhp\Events\WithFrom;
3839
use Bottledcode\DurablePhp\Events\WithLock;
3940
use Bottledcode\DurablePhp\Events\WithOrchestration;
4041
use Bottledcode\DurablePhp\Exceptions\Unwind;
@@ -77,9 +78,11 @@ public function callActivity(string $name, ?string $returnType = null, ?RetryOpt
7778

7879
return $this->createFuture(
7980
fn() => $this->taskController->fire(
80-
AwaitResult::forEvent(
81-
StateId::fromInstance($this->id),
82-
WithActivity::forEvent($identity, ScheduleTask::forName($name, $args)),
81+
$this->addFrom(
82+
AwaitResult::forEvent(
83+
StateId::fromInstance($this->id),
84+
WithActivity::forEvent($identity, ScheduleTask::forName($name, $args)),
85+
),
8386
),
8487
),
8588
function (Event $event, string $eventIdentity) use ($identity): array {
@@ -136,6 +139,14 @@ private function createFuture(
136139
return $future;
137140
}
138141

142+
private function addFrom(Event $event): Event
143+
{
144+
static $from = null;
145+
$from ??= StateId::fromInstance($this->id);
146+
147+
return WithFrom::forEvent($from, $event);
148+
}
149+
139150
public function callActivityInline(Closure $activity): DurableFuture
140151
{
141152
$identity = $this->newGuid();
@@ -144,22 +155,26 @@ public function callActivityInline(Closure $activity): DurableFuture
144155
try {
145156
$result = $activity();
146157
$this->taskController->fire(
147-
WithOrchestration::forInstance(
148-
StateId::fromInstance($this->id),
149-
TaskCompleted::forId($identity->toString(), $result),
158+
$this->addFrom(
159+
WithOrchestration::forInstance(
160+
StateId::fromInstance($this->id),
161+
TaskCompleted::forId($identity->toString(), $result),
162+
),
150163
),
151164
);
152165

153166
return [$identity];
154167
} catch (Throwable $exception) {
155168
$this->taskController->fire(
156-
WithOrchestration::forInstance(
157-
StateId::fromInstance($this->id),
158-
TaskFailed::forTask(
159-
$identity->toString(),
160-
$exception->getMessage(),
161-
$exception->getTraceAsString(),
162-
$exception::class,
169+
$this->addFrom(
170+
WithOrchestration::forInstance(
171+
StateId::fromInstance($this->id),
172+
TaskFailed::forTask(
173+
$identity->toString(),
174+
$exception->getMessage(),
175+
$exception->getTraceAsString(),
176+
$exception::class,
177+
),
163178
),
164179
),
165180
);
@@ -197,9 +212,11 @@ public function continueAsNew(array $args = []): never
197212

198213
$this->history->restartAsNew($args);
199214
$this->taskController->fire(
200-
WithOrchestration::forInstance(
201-
StateId::fromInstance($this->id),
202-
StartOrchestration::forInstance($this->id),
215+
$this->addFrom(
216+
WithOrchestration::forInstance(
217+
StateId::fromInstance($this->id),
218+
StartOrchestration::forInstance($this->id),
219+
),
203220
),
204221
);
205222
throw new Unwind();
@@ -216,9 +233,11 @@ public function createTimer(DateInterval|DateTimeImmutable $fireAt): DurableFutu
216233

217234
return $this->createFuture(
218235
fn() => $this->taskController->fire(
219-
WithOrchestration::forInstance(
220-
StateId::fromInstance($this->id),
221-
WithDelay::forEvent($fireAt, RaiseEvent::forTimer($identity)),
236+
$this->addFrom(
237+
WithOrchestration::forInstance(
238+
StateId::fromInstance($this->id),
239+
WithDelay::forEvent($fireAt, RaiseEvent::forTimer($identity)),
240+
),
222241
),
223242
),
224243
function (Event $event) use ($identity): array {
@@ -385,7 +404,7 @@ public function lockEntity(EntityId ...$entityId): EntityLock
385404
);
386405
$identity = $this->newGuid()->toString();
387406
$future = $this->createFuture(
388-
fn() => $this->taskController->fire(WithLock::onEntity($owner, $event, ...$entityId)),
407+
fn() => $this->taskController->fire($this->addFrom(WithLock::onEntity($owner, $event, ...$entityId))),
389408
fn(Event $event, string $eventIdentity) => [$event, $identity === $eventIdentity],
390409
$identity,
391410
);
@@ -396,9 +415,11 @@ public function lockEntity(EntityId ...$entityId): EntityLock
396415
return new EntityLock(function () use ($owner): void {
397416
foreach ($this->history->locks as $lock) {
398417
$this->taskController->fire(
399-
WithLock::onEntity(
400-
$owner,
401-
WithEntity::forInstance($lock, RaiseEvent::forUnlock($owner->id, null, null)),
418+
$this->addFrom(
419+
WithLock::onEntity(
420+
$owner,
421+
WithEntity::forInstance($lock, RaiseEvent::forUnlock($owner->id, null, null)),
422+
),
402423
),
403424
);
404425
}
@@ -624,7 +645,7 @@ public function callEntity(EntityId $entityId, string $operation, array $args =
624645
$identity = $this->newGuid()->toString();
625646

626647
return $this->createFuture(
627-
fn() => $this->taskController->fire($event),
648+
fn() => $this->taskController->fire($this->addFrom($event)),
628649
fn(Event $event, string $eventIdentity) => [$event, $identity === $eventIdentity],
629650
$identity,
630651
);
@@ -645,7 +666,7 @@ public function signalEntity(EntityId $entityId, string $operation, array $args
645666
$event = WithLock::onEntity($id, $event);
646667
}
647668

648-
$this->taskController->fire($event);
669+
$this->taskController->fire($this->addFrom($event));
649670
}
650671

651672
public function getCurrentUserId(): string

tests/Unit/EventDescriptionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,19 @@ public function __toString(): string
145145

146146
class MockWrapperEvent extends Event implements HasInnerEventInterface
147147
{
148-
public function __construct(string $eventId = '', private Event $inner = new SimpleEvent())
148+
public function __construct(string $eventId = '', public Event $innerEvent = new SimpleEvent())
149149
{
150150
parent::__construct($eventId ?: Uuid::uuid7()->toString());
151151
}
152152

153153
public function getInnerEvent(): Event
154154
{
155-
return $this->inner;
155+
return $this->innerEvent;
156156
}
157157

158158
public function __toString(): string
159159
{
160-
return 'MockWrapperEvent(' . $this->inner . ')';
160+
return 'MockWrapperEvent(' . $this->innerEvent . ')';
161161
}
162162
}
163163

0 commit comments

Comments
 (0)