Skip to content

Commit ee0b47a

Browse files
Use synthetic logs for probe replay
1 parent 0ad07ad commit ee0b47a

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

src/Exception.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Workflow\Exceptions\TransitionNotFound;
1616
use Workflow\Middleware\WithoutOverlappingMiddleware;
1717
use Workflow\Models\StoredWorkflow;
18+
use Workflow\Models\StoredWorkflowLog;
1819
use Workflow\Serializers\Serializer;
1920

2021
final class Exception implements ShouldBeEncrypted, ShouldQueue
@@ -100,11 +101,8 @@ private function shouldPersistAfterProbeReplay(): bool
100101
}
101102

102103
$previousContext = WorkflowStub::getContext();
103-
$connection = $this->storedWorkflow->getConnection();
104104
$shouldPersist = false;
105105

106-
$connection->beginTransaction();
107-
108106
try {
109107
$tentativeWorkflow = $this->createTentativeWorkflowState();
110108
$workflow = new $workflowClass($tentativeWorkflow, ...$tentativeWorkflow->workflowArguments());
@@ -130,27 +128,39 @@ private function shouldPersistAfterProbeReplay(): bool
130128
$shouldPersist = WorkflowStub::probeMatched();
131129
} finally {
132130
WorkflowStub::setContext($previousContext);
133-
134-
if ($connection->transactionLevel() > 0) {
135-
$connection->rollBack();
136-
}
137131
}
138132

139133
return $shouldPersist;
140134
}
141135

142136
private function createTentativeWorkflowState(): StoredWorkflow
143137
{
144-
$this->storedWorkflow->createLog([
145-
'index' => $this->index,
146-
'now' => $this->now,
147-
'class' => self::class,
148-
'result' => Serializer::serialize($this->exceptionPayload()),
149-
]);
150-
151138
$storedWorkflowClass = $this->storedWorkflow::class;
152139

153-
return $storedWorkflowClass::query()->findOrFail($this->storedWorkflow->id);
140+
/** @var StoredWorkflow $tentativeWorkflow */
141+
$tentativeWorkflow = $storedWorkflowClass::query()
142+
->findOrFail($this->storedWorkflow->id);
143+
144+
$tentativeWorkflow->loadMissing(['logs', 'signals']);
145+
146+
/** @var StoredWorkflowLog $tentativeLog */
147+
$tentativeLog = $tentativeWorkflow->logs()
148+
->make([
149+
'index' => $this->index,
150+
'now' => $this->now,
151+
'class' => self::class,
152+
'result' => Serializer::serialize($this->exceptionPayload()),
153+
]);
154+
155+
$tentativeWorkflow->setRelation(
156+
'logs',
157+
$tentativeWorkflow->getRelation('logs')
158+
->push($tentativeLog)
159+
->sortBy(static fn ($log): string => sprintf('%020d:%020d', $log->index, $log->id ?? PHP_INT_MAX))
160+
->values()
161+
);
162+
163+
return $tentativeWorkflow;
154164
}
155165

156166
private function exceptionPayload()

0 commit comments

Comments
 (0)