Skip to content

Commit b79bbdd

Browse files
author
Óscar J. Baeza
committed
feat: Implement workflow definition caching with invalidation, introduce safe error messages, improve JSON query compatibility, and add cache locking for batch dispatch.
1 parent 31942c0 commit b79bbdd

20 files changed

Lines changed: 160 additions & 103 deletions

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ coverage.xml
4444
clover.xml
4545

4646
# Package development
47-
composer.lock
4847
package-lock.json
4948

5049
# PHPStan

app/Actions/Batch/DispatchJobs.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ public function handle(string $driver, int $count, string $queue, int $delay, in
2626
Cache::put("batch:{$batchId}:queue", $queue, now()->addHours(24));
2727
Cache::put("batch:{$batchId}:started_at", now()->toIso8601String(), now()->addHours(24));
2828

29-
$activeBatches = Cache::get('active_batches', []);
30-
$activeBatches[$batchId] = [
31-
'id' => $batchId,
32-
'driver' => $driver,
33-
'queue' => $queue,
34-
'total' => $count,
35-
'started_at' => now()->toIso8601String(),
36-
];
37-
Cache::put('active_batches', $activeBatches, now()->addHours(24));
29+
Cache::lock('active_batches_lock', 5)->block(3, static function () use ($batchId, $driver, $queue, $count): void {
30+
$activeBatches = Cache::get('active_batches', []);
31+
$activeBatches[$batchId] = [
32+
'id' => $batchId,
33+
'driver' => $driver,
34+
'queue' => $queue,
35+
'total' => $count,
36+
'started_at' => now()->toIso8601String(),
37+
];
38+
Cache::put('active_batches', $activeBatches, now()->addHours(24));
39+
});
3840

3941
$chunkSize = 1000;
4042
for ($i = 0; $i < $count; $i += $chunkSize) {

app/Actions/Workflow/CreateWorkflowDefinition.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
use App\Jobs\WorkflowStepJob;
88
use App\Models\WorkflowDefinition;
9+
use Illuminate\Support\Facades\Cache;
910
use Station\Facades\Workflow;
1011

1112
class CreateWorkflowDefinition
1213
{
1314
public function handle(array $data): array
1415
{
16+
Cache::forget('workflow_definitions');
17+
1518
$definition = WorkflowDefinition::create([
1619
'name' => $data['name'],
1720
'description' => $data['description'] ?? null,

app/Actions/Workflow/DeleteWorkflowDefinition.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
namespace App\Actions\Workflow;
66

77
use App\Models\WorkflowDefinition;
8+
use Illuminate\Support\Facades\Cache;
89

910
class DeleteWorkflowDefinition
1011
{
1112
public function handle(string $name): bool
1213
{
14+
Cache::forget('workflow_definitions');
15+
1316
return WorkflowDefinition::where('name', $name)->delete() > 0;
1417
}
1518
}

app/Actions/Workflow/ListStationWorkflows.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function handle(int $page, ?string $status = null, ?string $connection =
2121
}
2222

2323
if ($connection) {
24-
$query->whereRaw("JSON_UNQUOTE(JSON_EXTRACT(context, '$.connection')) = ?", [$connection]);
24+
$query->where('context->connection', $connection);
2525
}
2626

2727
$total = (clone $query)->count();

app/Actions/Workflow/UpdateWorkflowDefinition.php

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

77
use App\Jobs\WorkflowStepJob;
88
use App\Models\WorkflowDefinition;
9+
use Illuminate\Support\Facades\Cache;
910
use Station\Workflows\WorkflowManager;
1011

1112
class UpdateWorkflowDefinition
@@ -22,7 +23,15 @@ public function handle(string $name, array $data): ?array
2223
return null;
2324
}
2425

25-
$definition->update($data);
26+
Cache::forget('workflow_definitions');
27+
28+
$definition->update([
29+
'name' => $data['name'],
30+
'description' => $data['description'] ?? null,
31+
'steps' => $data['steps'],
32+
'timeout' => $data['timeout'],
33+
'connection' => $data['connection'] ?? null,
34+
]);
2635

2736
$wf = $this->workflowManager->define($data['name'])
2837
->description($data['description'] ?? '')

app/Http/Controllers/Controller.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66

77
abstract class Controller
88
{
9-
//
9+
protected function safeErrorMessage(\Throwable $e): string
10+
{
11+
return app()->environment('local', 'testing')
12+
? $e->getMessage()
13+
: 'An internal error occurred.';
14+
}
1015
}

app/Http/Controllers/DriverController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function index(GetDrivers $action): JsonResponse
1818
return response()->json([
1919
'drivers' => [],
2020
'default' => config('queue.default', 'sync'),
21-
'error' => $e->getMessage(),
21+
'error' => $this->safeErrorMessage($e),
2222
]);
2323
}
2424
}

app/Http/Controllers/FailedJobController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function index(Request $request, ListFailedJobs $action): JsonResponse
2424
} catch (Throwable $e) {
2525
return response()->json([
2626
'success' => false,
27-
'error' => $e->getMessage(),
27+
'error' => $this->safeErrorMessage($e),
2828
'failed_jobs' => [],
2929
'count' => 0,
3030
'pagination' => ['current_page' => 1, 'last_page' => 1, 'total' => 0, 'per_page' => 10],
@@ -39,7 +39,7 @@ public function retry(string $uuid, RetryFailedJob $action): JsonResponse
3939
} catch (Throwable $e) {
4040
return response()->json([
4141
'success' => false,
42-
'error' => $e->getMessage(),
42+
'error' => $this->safeErrorMessage($e),
4343
], 500);
4444
}
4545
}
@@ -51,7 +51,7 @@ public function retryAll(RetryAllFailedJobs $action): JsonResponse
5151
} catch (Throwable $e) {
5252
return response()->json([
5353
'success' => false,
54-
'error' => $e->getMessage(),
54+
'error' => $this->safeErrorMessage($e),
5555
], 500);
5656
}
5757
}
@@ -68,7 +68,7 @@ public function destroy(string $uuid, DeleteFailedJob $action): JsonResponse
6868
} catch (Throwable $e) {
6969
return response()->json([
7070
'success' => false,
71-
'error' => $e->getMessage(),
71+
'error' => $this->safeErrorMessage($e),
7272
], 500);
7373
}
7474
}
@@ -80,7 +80,7 @@ public function flush(FlushFailedJobs $action): JsonResponse
8080
} catch (Throwable $e) {
8181
return response()->json([
8282
'success' => false,
83-
'error' => $e->getMessage(),
83+
'error' => $this->safeErrorMessage($e),
8484
], 500);
8585
}
8686
}

app/Http/Controllers/StationBatchController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function store(Request $request, CreateStationBatch $action): JsonRespons
4040
} catch (Throwable $e) {
4141
return response()->json([
4242
'success' => false,
43-
'error' => $e->getMessage(),
43+
'error' => $this->safeErrorMessage($e),
4444
], 500);
4545
}
4646
}
@@ -57,7 +57,7 @@ public function index(Request $request, ListStationBatches $action): JsonRespons
5757
return response()->json([
5858
'batches' => [],
5959
'pagination' => ['current_page' => 1, 'last_page' => 1, 'total' => 0, 'per_page' => 10],
60-
'error' => $e->getMessage(),
60+
'error' => $this->safeErrorMessage($e),
6161
]);
6262
}
6363
}
@@ -69,7 +69,7 @@ public function cancel(string $id, CancelBatch $action): JsonResponse
6969
} catch (Throwable $e) {
7070
return response()->json([
7171
'success' => false,
72-
'error' => $e->getMessage(),
72+
'error' => $this->safeErrorMessage($e),
7373
], 500);
7474
}
7575
}
@@ -81,7 +81,7 @@ public function retry(string $id, RetryBatch $action): JsonResponse
8181
} catch (Throwable $e) {
8282
return response()->json([
8383
'success' => false,
84-
'error' => $e->getMessage(),
84+
'error' => $this->safeErrorMessage($e),
8585
], 500);
8686
}
8787
}
@@ -93,7 +93,7 @@ public function clear(ClearStationBatches $action): JsonResponse
9393

9494
return response()->json(['success' => true]);
9595
} catch (Throwable $e) {
96-
return response()->json(['success' => false, 'error' => $e->getMessage()], 500);
96+
return response()->json(['success' => false, 'error' => $this->safeErrorMessage($e)], 500);
9797
}
9898
}
9999
}

0 commit comments

Comments
 (0)