Skip to content

Commit 290982a

Browse files
committed
delete batch
1 parent 9dc234d commit 290982a

File tree

16 files changed

+344
-185
lines changed

16 files changed

+344
-185
lines changed

config/processable.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@
9292
],
9393

9494
'actions' => [
95+
'job' => [
96+
'create' => Yuges\Processable\Actions\CreateJobAction::class,
97+
],
9598
'stage' => [
9699
'update' => Yuges\Processable\Actions\UpdateProcessStageAction::class,
97100
'create' => Yuges\Processable\Actions\CreateProcessStagesAction::class,
@@ -106,6 +109,7 @@
106109
'job' => [
107110
'class' => Yuges\Processable\Jobs\ProcessStageJob::class,
108111
'handler' => [
112+
'event' => Yuges\Processable\Handlers\EventHandler::class,
109113
'stage' => Yuges\Processable\Handlers\StageEventHandler::class,
110114
'process' => Yuges\Processable\Handlers\ProcessEventHandler::class,
111115
],

database/migrations/000_create_processes_table.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
use Yuges\Package\Enums\KeyType;
4-
use Yuges\Processable\Models\Batch;
54
use Yuges\Processable\Config\Config;
65
use Yuges\Processable\Models\Process;
76
use Yuges\Processable\Enums\ProcessState;
@@ -40,18 +39,6 @@ public function up(): void
4039
);
4140
}
4241

43-
if (Config::getBatchKeyHas(true)) {
44-
$batch = new (Config::getBatchClass(Batch::class));
45-
46-
$table->string($batch->getForeignKey())->nullable();
47-
$table
48-
->foreign($batch->getForeignKey())
49-
->references('id')
50-
->on(Config::getBatchClass(Batch::class)::getTableName())
51-
->cascadeOnUpdate()
52-
->nullOnDelete();
53-
}
54-
5542
$table->timestamp('started_at')->nullable();
5643
$table->timestamp('finished_at')->nullable();
5744
$table->timestamp('failed_at')->nullable();

src/Abstracts/Process.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Yuges\Processable\Abstracts;
44

5+
use Illuminate\Support\Collection;
56
use Yuges\Processable\Models\Process as ProcessModel;
67

78
abstract class Process implements \Yuges\Processable\Interfaces\Process
@@ -13,9 +14,41 @@ public function stages(): array
1314
return [];
1415
}
1516

17+
public function stageCollection(): Collection
18+
{
19+
return Collection::make($this->stages());
20+
}
21+
22+
public function firstStage(): ?string
23+
{
24+
return $this->stageCollection()->first();
25+
}
26+
27+
public function lastStage(): ?string
28+
{
29+
return $this->stageCollection()->last();
30+
}
31+
32+
public function nextStage(?string $stage = null): ?string
33+
{
34+
$stages = $this->stageCollection();
35+
36+
if (! $stage) {
37+
return $stages->first();
38+
}
39+
40+
$key = $stages->search($stage);
41+
42+
if ($key === false) {
43+
return null;
44+
}
45+
46+
return $stages->get($key + 1);
47+
}
48+
1649
static function process(): ProcessModel
1750
{
18-
return self::createProcess();
51+
return static::createProcess();
1952
}
2053

2154
public static function createProcess(): ProcessModel
@@ -28,7 +61,7 @@ public function getName(): string
2861
return $this->name;
2962
}
3063

31-
public function setName(string $name): self
64+
public function setName(string $name): static
3265
{
3366
$this->name = $name;
3467

src/Actions/CreateJobAction.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Yuges\Processable\Actions;
4+
5+
use Exception;
6+
use Yuges\Processable\Config\Config;
7+
use Yuges\Processable\Models\Process;
8+
use Yuges\Processable\Jobs\ProcessStageJob;
9+
use Yuges\Processable\Interfaces\Processable;
10+
use Yuges\Processable\Interfaces\Stage as StageInterface;
11+
12+
class CreateJobAction
13+
{
14+
public function __construct(
15+
protected Process $process,
16+
protected Processable $processable,
17+
) {
18+
}
19+
20+
public static function create(Process $process, Processable $processable): self
21+
{
22+
return new static($process, $processable);
23+
}
24+
25+
/**
26+
* @param class-string<StageInterface>|null $stage
27+
*/
28+
public function execute(?string $stage): ProcessStageJob
29+
{
30+
if (! $stage) {
31+
throw new Exception('Empty stage');
32+
}
33+
34+
$stage = new $stage;
35+
36+
if (! $stage instanceof StageInterface) {
37+
throw new Exception('Error stage type');
38+
}
39+
40+
$stage = $this->process->stages->firstOrFail('class', '=', $stage::class);
41+
42+
return Config::getProcessStageJob(
43+
$stage,
44+
$this->process,
45+
$this->processable,
46+
ProcessStageJob::class
47+
);
48+
}
49+
}

src/Actions/RunProcessAction.php

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@
22

33
namespace Yuges\Processable\Actions;
44

5-
use Exception;
6-
use Throwable;
7-
use Carbon\Carbon;
8-
use Illuminate\Bus\Batch;
9-
use Illuminate\Support\Collection;
10-
use Illuminate\Support\Facades\Bus;
115
use Yuges\Processable\Config\Config;
126
use Yuges\Processable\Models\Process;
13-
use Yuges\Processable\Enums\ProcessState;
14-
use Yuges\Processable\Jobs\ProcessStageJob;
157
use Yuges\Processable\Interfaces\Processable;
16-
use Yuges\Processable\Interfaces\Stage as InterfacesStage;
178

189
class RunProcessAction
1910
{
@@ -27,46 +18,16 @@ public static function create(Processable $processable): self
2718
return new static($processable);
2819
}
2920

30-
public function execute(Process $model): Process
21+
public function execute(Process $process): Process
3122
{
32-
$process = new $model->class;
23+
$stage = (new $process->class)->firstStage();
3324

34-
$jobs = Collection::make($process->stages())->map(function (string $stage) use ($model) {
35-
$stage = new $stage;
25+
$job = Config::getCreateJobAction($process, $this->processable)->execute($stage);
3626

37-
if (! $stage instanceof InterfacesStage) {
38-
throw new Exception('Error stage type');
39-
}
27+
dispatch($job)
28+
->onConnection(Config::getQueueConnection())
29+
->onQueue(Config::getQueueName());
4030

41-
$stage = $model->stages->firstOrFail('class', '=', $stage::class);
42-
43-
return Config::getProcessStageJob(
44-
$stage,
45-
$model,
46-
$this->processable,
47-
ProcessStageJob::class
48-
);
49-
});
50-
51-
$action = Config::getUpdateProcessAction($model, UpdateProcessAction::class);
52-
53-
Bus::batch([$jobs->toArray()])
54-
->before(function (Batch $batch) use ($action) {
55-
$action->execute(Config::getProcessStateClass(ProcessState::class)::Started, $batch);
56-
})
57-
->progress(function (Batch $batch) use ($action) {
58-
$action->execute(Config::getProcessStateClass(ProcessState::class)::Processing, $batch);
59-
})
60-
->catch(function (Batch $batch, Throwable $e) use ($action) {
61-
$action->execute(Config::getProcessStateClass(ProcessState::class)::Failed, $batch, $e);
62-
})
63-
->finally(function (Batch $batch) use ($action) {
64-
$action->execute(Config::getProcessStateClass(ProcessState::class)::Finished, $batch);
65-
})
66-
->onConnection(Config::getQueueConnection())
67-
->onQueue(Config::getQueueName())
68-
->dispatch();
69-
70-
return $model;
31+
return $process;
7132
}
7233
}

src/Actions/UpdateProcessAction.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace Yuges\Processable\Actions;
44

5-
use Throwable;
65
use Carbon\Carbon;
7-
use Illuminate\Bus\Batch;
6+
use Illuminate\Contracts\Queue\Job;
87
use Yuges\Processable\Models\Process;
98
use Yuges\Processable\Enums\ProcessState;
109
use Yuges\Processable\Interfaces\ProcessState as ProcessStateInterface;
@@ -21,11 +20,10 @@ public static function create(Process $process): self
2120
return new static($process);
2221
}
2322

24-
public function execute(ProcessStateInterface $state, ?Batch $batch = null, ?Throwable $e = null): Process
23+
public function execute(ProcessStateInterface $state, ?Job $job = null): Process
2524
{
2625
$attributes = [
2726
'state' => $state,
28-
'batch_id' => $batch->id ?? $this->process->batch_id,
2927
];
3028

3129
match ($state->value) {

src/Config/Config.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
use Yuges\Processable\Models\FailedJob;
1313
use Yuges\Processable\Enums\ProcessState;
1414
use Yuges\Processable\Jobs\ProcessStageJob;
15+
use Yuges\Processable\Handlers\EventHandler;
1516
use Yuges\Processable\Interfaces\Processable;
17+
use Yuges\Processable\Actions\CreateJobAction;
1618
use Yuges\Processable\Observers\StageObserver;
1719
use Yuges\Processable\Actions\RunProcessAction;
1820
use Yuges\Processable\Observers\ProcessObserver;
1921
use Yuges\Processable\Handlers\StageEventHandler;
2022
use Yuges\Processable\Actions\UpdateProcessAction;
2123
use Yuges\Processable\Actions\CreateProcessAction;
24+
use Yuges\Processable\Handlers\ProcessEventHandler;
2225
use Yuges\Processable\Observers\ProcessableObserver;
2326
use Yuges\Processable\Actions\UpdateProcessStageAction;
2427
use Yuges\Processable\Actions\CreateProcessStagesAction;
@@ -228,6 +231,21 @@ public static function getProcessStateClass(mixed $default = null): string
228231
return self::get('process.state', $default);
229232
}
230233

234+
public static function getCreateJobAction(
235+
Process $process,
236+
Processable $processable,
237+
mixed $default = null
238+
): CreateJobAction
239+
{
240+
return self::getCreateJobActionClass($default)::create($process, $processable);
241+
}
242+
243+
/** @return class-string<CreateJobAction> */
244+
public static function getCreateJobActionClass(mixed $default = null): string
245+
{
246+
return self::get('actions.job.create', $default);
247+
}
248+
231249
public static function getRunProcessAction(
232250
Processable $processable,
233251
mixed $default = null
@@ -316,6 +334,17 @@ public static function getProcessStageJobClass(mixed $default = null): string
316334
return self::get('job.class', $default);
317335
}
318336

337+
public static function getEventHandler(mixed $default = null): EventHandler
338+
{
339+
return self::getEventHandlerClass($default)::create();
340+
}
341+
342+
/** @return class-string<EventHandler> */
343+
public static function getEventHandlerClass(mixed $default = null): string
344+
{
345+
return self::get('job.handler.event', $default);
346+
}
347+
319348
public static function getStageEventHandler(mixed $default = null): StageEventHandler
320349
{
321350
return self::getStageEventHandlerClass($default)::create();
@@ -327,6 +356,17 @@ public static function getStageEventHandlerClass(mixed $default = null): string
327356
return self::get('job.handler.stage', $default);
328357
}
329358

359+
public static function getProcessEventHandler(mixed $default = null): ProcessEventHandler
360+
{
361+
return self::getProcessEventHandlerClass($default)::create();
362+
}
363+
364+
/** @return class-string<ProcessEventHandler> */
365+
public static function getProcessEventHandlerClass(mixed $default = null): string
366+
{
367+
return self::get('job.handler.process', $default);
368+
}
369+
330370
public static function getQueueName(mixed $default = null): string
331371
{
332372
return self::get('job.queue.name', $default);

0 commit comments

Comments
 (0)