-
Notifications
You must be signed in to change notification settings - Fork 0
Implement retries #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
database/migrations/2024_09_18_113000_magento_bulk_requests_method_field.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up(): void | ||
| { | ||
| Schema::table('magento_bulk_requests', function (Blueprint $table): void { | ||
| $table->string('method')->after('store_code'); | ||
| }); | ||
| } | ||
|
|
||
| public function down(): void | ||
| { | ||
| Schema::dropColumns('magento_bulk_requests', ['method']); | ||
| } | ||
| }; |
22 changes: 22 additions & 0 deletions
22
database/migrations/2024_09_18_140000_magento_bulk_requests_retry_of_field.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up(): void | ||
| { | ||
| Schema::table('magento_bulk_requests', function (Blueprint $table): void { | ||
| $table->unsignedBigInteger('retry_of')->after('id')->nullable(); | ||
|
|
||
| $table->foreign('retry_of')->references('id')->on('magento_bulk_requests')->onDelete('set null'); | ||
| }); | ||
| } | ||
|
|
||
| public function down(): void | ||
| { | ||
| Schema::dropColumns('magento_bulk_requests', ['retry_of']); | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php | ||
|
|
||
| namespace JustBetter\MagentoAsync\Actions; | ||
|
|
||
| use Illuminate\Database\Eloquent\Model; | ||
| use JustBetter\MagentoAsync\Client\MagentoAsync; | ||
| use JustBetter\MagentoAsync\Contracts\RetriesBulkRequest; | ||
| use JustBetter\MagentoAsync\Enums\OperationStatus; | ||
| use JustBetter\MagentoAsync\Exceptions\InvalidMethodException; | ||
| use JustBetter\MagentoAsync\Models\BulkOperation; | ||
| use JustBetter\MagentoAsync\Models\BulkRequest; | ||
| use JustBetter\MagentoClient\Client\Magento; | ||
|
|
||
| class RetryBulkRequest implements RetriesBulkRequest | ||
| { | ||
| public function __construct(protected MagentoAsync $client) {} | ||
|
|
||
| public function retry(BulkRequest $bulkRequest, bool $onlyFailed): ?BulkRequest | ||
| { | ||
| /** @var array<int, mixed> $payload */ | ||
| $payload = []; | ||
| /** @var array<int, ?Model> $subjects */ | ||
| $subjects = []; | ||
|
|
||
| $operations = $bulkRequest->operations; | ||
|
|
||
| foreach ($bulkRequest->request as $index => $request) { | ||
|
|
||
| /** @var BulkOperation $operation */ | ||
| $operation = $operations->where('operation_id', '=', $index)->firstOrFail(); | ||
|
|
||
| if ($onlyFailed && ! in_array($operation->status, OperationStatus::failedStatuses())) { | ||
| continue; | ||
| } | ||
|
|
||
| $payload[] = $request; | ||
| $subjects[] = $operation->subject; | ||
| } | ||
|
|
||
| if ($payload === []) { | ||
| return null; | ||
| } | ||
|
|
||
| $pendingRequest = $this->client | ||
| ->configure(fn (Magento $client): Magento => $client->store($bulkRequest->store_code)) | ||
| ->subjects($subjects); | ||
|
|
||
| $retry = match ($bulkRequest->method) { | ||
| 'POST' => $pendingRequest->postBulk($bulkRequest->path, $payload), | ||
| 'PUT' => $pendingRequest->putBulk($bulkRequest->path, $payload), | ||
| 'DELETE' => $pendingRequest->deleteBulk($bulkRequest->path, $payload), | ||
| default => throw new InvalidMethodException('Unsupported method "'.$bulkRequest->method.'"'), | ||
| }; | ||
|
|
||
| if ($retry !== null) { | ||
| $bulkRequest->retries()->save($retry); | ||
| } | ||
|
|
||
| return $retry; | ||
| } | ||
|
|
||
| public static function bind(): void | ||
| { | ||
| app()->singleton(RetriesBulkRequest::class, static::class); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
|
|
||
| namespace JustBetter\MagentoAsync\Commands; | ||
|
|
||
| use Illuminate\Console\Command; | ||
| use JustBetter\MagentoAsync\Contracts\RetriesBulkRequest; | ||
| use JustBetter\MagentoAsync\Models\BulkRequest; | ||
|
|
||
| class RetryBulkRequestCommand extends Command | ||
| { | ||
| protected $signature = 'magento:async:retry-bulk-request {id} {--only-failed=true}'; | ||
|
|
||
| protected $description = 'Retry bulk request'; | ||
|
|
||
| public function handle(RetriesBulkRequest $contract): int | ||
| { | ||
| /** @var int $id */ | ||
| $id = $this->argument('id'); | ||
|
|
||
| /** @var bool $onlyFailed */ | ||
| $onlyFailed = $this->option('only-failed'); | ||
|
|
||
| /** @var BulkRequest $request */ | ||
| $request = BulkRequest::query()->findOrFail($id); | ||
|
|
||
| $bulkRequest = $contract->retry($request, $onlyFailed); | ||
|
|
||
| if ($bulkRequest === null) { | ||
| $this->error('Failed to retry bulk request'); | ||
|
|
||
| return static::FAILURE; | ||
| } | ||
|
|
||
| $this->info('Retried with bulk uuid "'.$bulkRequest->bulk_uuid.'"'); | ||
|
|
||
| return static::SUCCESS; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace JustBetter\MagentoAsync\Contracts; | ||
|
|
||
| use JustBetter\MagentoAsync\Models\BulkRequest; | ||
|
|
||
| interface RetriesBulkRequest | ||
| { | ||
| public function retry(BulkRequest $bulkRequest, bool $onlyFailed): ?BulkRequest; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?php | ||
|
|
||
| namespace JustBetter\MagentoAsync\Exceptions; | ||
|
|
||
| use Exception; | ||
|
|
||
| class InvalidMethodException extends Exception {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice if you could add a foreign key constraint that will set the value to NULL if the relation has been removed.