Skip to content

Commit a078d36

Browse files
committed
add before and after
1 parent a9870ad commit a078d36

File tree

2 files changed

+88
-68
lines changed

2 files changed

+88
-68
lines changed

src/Models/Workflow.php

Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use Illuminate\Database\Eloquent\Relations\HasMany;
1111
use Illuminate\Database\Eloquent\Relations\MorphTo;
1212
use Illuminate\Support\Collection as SupportCollection;
13-
use Illuminate\Support\Facades\Bus;
14-
use Throwable;
1513

1614
/**
1715
* @property int $id
@@ -245,86 +243,30 @@ public function markStepAsCanceled(string $name): static
245243
return $this;
246244
}
247245

248-
public function run(
249-
?string $connection = null,
250-
?string $queue = null,
251-
): void {
252-
/** @var ?string $connection */
253-
$connection = $connection ?? config('workflow.queue_connection');
254-
/** @var ?string $queue */
255-
$queue = $queue ?? config('workflow.queue');
256-
246+
public function run(): void
247+
{
257248
if (
258249
$this->isFinished() ||
259250
$this->isFailed() ||
260-
$this->isCanceled() ||
261-
! $this->definition->shouldRun()
251+
$this->isCanceled()
262252
) {
263253
return;
264254
}
265255

266-
if ($this->definition->shouldCancel()) {
256+
if ($this->definition->shouldCancel($this)) {
267257
$this->markAsCanceled();
268258

269259
return;
270260
}
271261

272-
$readySteps = $this->definition
273-
->steps($this)
274-
->filter(function ($step, $name) {
275-
return ! $this->isStepPending($name) &&
276-
! $this->isStepFinished($name) &&
277-
! $this->isStepCanceled($name) &&
278-
! $this->isStepFailed($name) &&
279-
$step->isReady();
280-
});
281-
282-
if ($readySteps->isEmpty()) {
262+
if (! $this->definition->shouldRun($this)) {
283263
return;
284264
}
285265

286-
$items = $this->items()->createMany(
287-
$readySteps->map(fn ($step, $name) => [
288-
'name' => $name,
289-
'dispatched_at' => now(),
290-
])
291-
);
292-
293-
$this->items->push(...$items);
266+
$this->definition->beforeRun($this);
294267

295-
/**
296-
* @var array<int, string> $stepNames
297-
*/
298-
$stepNames = $readySteps->keys()->toArray();
299-
$workflowId = $this->id;
268+
$this->definition->run($this);
300269

301-
Bus::chain([
302-
...$readySteps->flatMap(function ($step, $name) use ($workflowId) {
303-
return [
304-
$step->action,
305-
function () use ($workflowId, $name) {
306-
$workflow = Workflow::query()->findOrFail($workflowId);
307-
$workflow->markStepAsFinished($name);
308-
},
309-
];
310-
}),
311-
])
312-
->catch(function (Throwable $e) use ($workflowId, $stepNames) {
313-
$workflow = Workflow::query()->findOrFail($workflowId);
314-
315-
$item = $workflow
316-
->items
317-
->whereIn('name', $stepNames)
318-
->firstWhere('finished_at', null);
319-
320-
if ($item) {
321-
$workflow->markStepAsFailed($item->name);
322-
} else {
323-
$workflow->markAsFailed();
324-
}
325-
})
326-
->onConnection($connection)
327-
->onQueue($queue)
328-
->dispatch();
270+
$this->definition->afterRun($this);
329271
}
330272
}

src/WorkflowDefinition.php

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Elegantly\Workflow\Models\Workflow;
66
use Illuminate\Queue\SerializesModels;
77
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Facades\Bus;
9+
use Throwable;
810

911
abstract class WorkflowDefinition
1012
{
@@ -28,13 +30,89 @@ public function start(): Workflow
2830
return $workflow;
2931
}
3032

31-
public function shouldCancel(): bool
33+
public function shouldCancel(Workflow $workflow): bool
3234
{
3335
return false;
3436
}
3537

36-
public function shouldRun(): bool
38+
public function shouldRun(Workflow $workflow): bool
3739
{
3840
return true;
3941
}
42+
43+
public function beforeRun(Workflow $workflow): void
44+
{
45+
//
46+
}
47+
48+
public function run(Workflow $workflow): void
49+
{
50+
$readySteps = $this
51+
->steps($workflow)
52+
->filter(function ($step, $name) use ($workflow) {
53+
return ! $workflow->isStepPending($name) &&
54+
! $workflow->isStepFinished($name) &&
55+
! $workflow->isStepCanceled($name) &&
56+
! $workflow->isStepFailed($name) &&
57+
$step->isReady();
58+
});
59+
60+
if ($readySteps->isEmpty()) {
61+
return;
62+
}
63+
64+
$items = $workflow->items()->createMany(
65+
$readySteps->map(fn ($step, $name) => [
66+
'name' => $name,
67+
'dispatched_at' => now(),
68+
])
69+
);
70+
71+
$workflow->items->push(...$items);
72+
73+
/**
74+
* @var array<int, string> $stepNames
75+
*/
76+
$stepNames = $readySteps->keys()->toArray();
77+
$workflowId = $workflow->id;
78+
79+
/** @var ?string $connection */
80+
$connection = config('workflow.queue_connection');
81+
/** @var ?string $queue */
82+
$queue = config('workflow.queue');
83+
84+
Bus::chain([
85+
...$readySteps->flatMap(function ($step, $name) use ($workflowId) {
86+
return [
87+
$step->action,
88+
function () use ($workflowId, $name) {
89+
$workflow = Workflow::query()->findOrFail($workflowId);
90+
$workflow->markStepAsFinished($name);
91+
},
92+
];
93+
}),
94+
])
95+
->catch(function (Throwable $e) use ($workflowId, $stepNames) {
96+
$workflow = Workflow::query()->findOrFail($workflowId);
97+
98+
$item = $workflow
99+
->items
100+
->whereIn('name', $stepNames)
101+
->firstWhere('finished_at', null);
102+
103+
if ($item) {
104+
$workflow->markStepAsFailed($item->name);
105+
} else {
106+
$workflow->markAsFailed();
107+
}
108+
})
109+
->onConnection($connection)
110+
->onQueue($queue)
111+
->dispatch();
112+
}
113+
114+
public function afterRun(Workflow $workflow): void
115+
{
116+
//
117+
}
40118
}

0 commit comments

Comments
 (0)