diff --git a/app/Classes/General/Eloquent/Filters/RequestSignature.php b/app/Classes/General/Eloquent/Filters/RequestSignature.php new file mode 100644 index 00000000..67dde0a3 --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/RequestSignature.php @@ -0,0 +1,19 @@ +where('request_signature', $value); + } + +} diff --git a/app/Classes/General/Eloquent/Filters/ResultNotNull.php b/app/Classes/General/Eloquent/Filters/ResultNotNull.php new file mode 100644 index 00000000..3f6a0341 --- /dev/null +++ b/app/Classes/General/Eloquent/Filters/ResultNotNull.php @@ -0,0 +1,18 @@ +whereNotNull('result'); + } +} diff --git a/app/Classes/Modules/Jobs/ControllersLogic/FetchJobResultLogic.php b/app/Classes/Modules/Jobs/ControllersLogic/FetchJobResultLogic.php index 878a4a79..3bbc96df 100644 --- a/app/Classes/Modules/Jobs/ControllersLogic/FetchJobResultLogic.php +++ b/app/Classes/Modules/Jobs/ControllersLogic/FetchJobResultLogic.php @@ -4,7 +4,7 @@ namespace App\Classes\Modules\Jobs\ControllersLogic; use App\Classes\General\Abstracts\AbstractControllerLogic; -use App\Classes\Modules\Jobs\Services\FetchesJobResult; +use App\Classes\Modules\Jobs\Processors\FetchesJobResultProcessor; use App\Http\Resources\JobResultResource; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; @@ -22,16 +22,16 @@ class FetchJobResultLogic extends AbstractControllerLogic ]; } - /** @var FetchesJobResult */ - private $fetchesJobResult; + /** @var FetchesJobResultProcessor */ + private $fetchesJobResultProcessor; /** * FetchJobResultLogic constructor. - * @param FetchesJobResult $fetchesJobResult + * @param FetchesJobResultProcessor $fetchesJobResultProcessor */ - public function __construct(FetchesJobResult $fetchesJobResult) + public function __construct(FetchesJobResultProcessor $fetchesJobResultProcessor) { - $this->fetchesJobResult = $fetchesJobResult; + $this->fetchesJobResultProcessor = $fetchesJobResultProcessor; } @@ -44,10 +44,9 @@ class FetchJobResultLogic extends AbstractControllerLogic */ public function logic(Request $request) : JsonResponse { - $query = $this->fetchesJobResult->execute(['job_id' => $request->route('job_id')]); + $query = $this->fetchesJobResultProcessor->execute($request); return $this->resourceResponse(new JobResultResource($query)); - } } diff --git a/app/Classes/Modules/Jobs/DataTransferObjects/ListGenericJobObject.php b/app/Classes/Modules/Jobs/DataTransferObjects/ListGenericJobObject.php index 8cb135d1..953d77d7 100644 --- a/app/Classes/Modules/Jobs/DataTransferObjects/ListGenericJobObject.php +++ b/app/Classes/Modules/Jobs/DataTransferObjects/ListGenericJobObject.php @@ -15,6 +15,12 @@ class ListGenericJobObject implements DataTransferObject /** @var string */ private $jobId; + /** @var string */ + private $requestSignature; + + /** @var string */ + private $resultSignature; + /** @var object */ private $userInfo; @@ -24,11 +30,13 @@ class ListGenericJobObject implements DataTransferObject /** @var string */ private $jobCommand; - public function __construct(string $name, array $payload, string $jobId, object $userInfo = null) + public function __construct(string $name, array $payload, string $requestSignature, ?string $resultSignature, string $jobId, object $userInfo = null) { $this->name = $name; $this->payload = $payload; $this->jobId = $jobId; + $this->requestSignature = $requestSignature; + $this->resultSignature = $resultSignature; $this->userInfo = $userInfo; } @@ -56,6 +64,22 @@ class ListGenericJobObject implements DataTransferObject return $this->jobId; } + /** + * @return string + */ + public function getRequestSignature(): string + { + return $this->requestSignature; + } + + /** + * @return string + */ + public function getResultSignature(): ?string + { + return $this->resultSignature; + } + /** * @return object */ diff --git a/app/Classes/Modules/Jobs/DataTransferObjects/UpdateJobResultObject.php b/app/Classes/Modules/Jobs/DataTransferObjects/UpdateJobResultObject.php new file mode 100644 index 00000000..636c56b0 --- /dev/null +++ b/app/Classes/Modules/Jobs/DataTransferObjects/UpdateJobResultObject.php @@ -0,0 +1,60 @@ +result = $result; + $this->resultSignature = $resultSignature; + $this->jobCommandName = $jobCommandName; + $this->jobCommand = $jobCommand; + } + + /** + * @return string + */ + public function getResult(): string + { + return $this->result; + } + + /** + * @return array + */ + public function getResultSignature(): string + { + return $this->resultSignature; + } + + /** + * @return string + */ + public function getJobCommandName(): string + { + return $this->jobCommandName; + } + + /** + * @return string + */ + public function getJobCommand(): string + { + return $this->jobCommand; + } +} diff --git a/app/Classes/Modules/Jobs/Processors/FetchesJobResultProcessor.php b/app/Classes/Modules/Jobs/Processors/FetchesJobResultProcessor.php new file mode 100644 index 00000000..dfaea815 --- /dev/null +++ b/app/Classes/Modules/Jobs/Processors/FetchesJobResultProcessor.php @@ -0,0 +1,35 @@ +fetchesJobResult = $fetchesJobResult; + } + + public function execute(Request $request){ + + $res1 = $this->fetchesJobResult->execute(['job_id' => $request->route('job_id')]); + + if(!$res1->result){ + $res2 = $this->fetchesJobResult->execute(['request_signature' => $res1->request_signature, 'result_not_null' => true, 'order_by_id_desc' => true]); + return $res2; + } + + return $res1; + } +} diff --git a/app/Classes/Modules/Jobs/Services/CreatesJobResult.php b/app/Classes/Modules/Jobs/Services/CreatesJobResult.php index 00acc1eb..e70e0e6a 100644 --- a/app/Classes/Modules/Jobs/Services/CreatesJobResult.php +++ b/app/Classes/Modules/Jobs/Services/CreatesJobResult.php @@ -10,18 +10,16 @@ class CreatesJobResult extends AbstractUpdateRecord { /** * @param ListGenericJobObject $listGenericJobObject - * @param string $result * @return \Illuminate\Database\Eloquent\Model * @throws \App\Classes\Exceptions\MalformedRequestException */ - public function execute(ListGenericJobObject $listGenericJobObject, string $result) + public function execute(ListGenericJobObject $listGenericJobObject) { $model = new JobResult(); $model->job_id = $listGenericJobObject->getJobId(); - $model->result = $result; + $model->request_signature = $listGenericJobObject->getRequestSignature(); + $model->result_signature = $listGenericJobObject->getResultSignature(); $model->url = $listGenericJobObject->getName(); - $model->job_command_name = $listGenericJobObject->getJobCommandName(); - $model->job_command = $listGenericJobObject->getJobCommand(); return $this->handler($model); } diff --git a/app/Classes/Modules/Jobs/Services/ListsJobResult.php b/app/Classes/Modules/Jobs/Services/ListsJobResult.php new file mode 100644 index 00000000..55f3b267 --- /dev/null +++ b/app/Classes/Modules/Jobs/Services/ListsJobResult.php @@ -0,0 +1,33 @@ +repository = $repository; + } + + + /** + * @return Builder + */ + function getRepository(): Builder + { + return $this->repository->newQuery(); + } +} diff --git a/app/Classes/Modules/Jobs/Services/UpdatesJobResult.php b/app/Classes/Modules/Jobs/Services/UpdatesJobResult.php new file mode 100644 index 00000000..ab3d9385 --- /dev/null +++ b/app/Classes/Modules/Jobs/Services/UpdatesJobResult.php @@ -0,0 +1,28 @@ +result = $updateJobResultObject->getResult(); + $model->result_signature = $updateJobResultObject->getResultSignature(); + $model->job_command_name = $updateJobResultObject->getJobCommandName(); + $model->job_command = $updateJobResultObject->getJobCommand(); + + return $this->handler($model); + + } +} diff --git a/app/Classes/Modules/PackingLists/ControllersLogic/ListPackingListsJobLogic.php b/app/Classes/Modules/PackingLists/ControllersLogic/ListPackingListsJobLogic.php index 0bdd6244..8b8e672f 100644 --- a/app/Classes/Modules/PackingLists/ControllersLogic/ListPackingListsJobLogic.php +++ b/app/Classes/Modules/PackingLists/ControllersLogic/ListPackingListsJobLogic.php @@ -9,19 +9,33 @@ use App\Classes\Modules\Jobs\DataTransferObjects\ListGenericJobObject; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use App\Classes\Modules\Jobs\Services\CreatesJobResult; class ListPackingListsJobLogic extends AbstractControllerLogic { - /** + /** * @return array */ protected function notification():array { return [ - 'title' => 'List Transaction Job', - 'message' => 'You have successfully submit a job to list transactions' + 'title' => 'Job Retrieving PackingLists', + 'message' => 'You have successfully submit a job to retrieve a list of PackingLists' ]; } + /** @var CreatesJobResult */ + private $createsJobResult; + + /** + * ListPackingListsJobLogic constructor. + * @param CreatesJobResult $createsJobResult + */ + public function __construct(CreatesJobResult $createsJobResult) + { + $this->createsJobResult = $createsJobResult; + } + + /** * @param Request $request * @return JsonResponse @@ -32,13 +46,20 @@ class ListPackingListsJobLogic extends AbstractControllerLogic $user = Auth::user(); $userInfo = (object) [ - 'email' => $user->email, + // 'email' => $user->email, 'type' => $user->type, ]; + + $userInfoJson = json_encode($userInfo); + $requestSignature = md5($userInfoJson . $request->fullUrl()); + + $listGenericJobObject = new ListGenericJobObject( $request->fullUrl(), $request->all(), + $requestSignature, + null, $jobId, $userInfo ); @@ -48,6 +69,9 @@ class ListPackingListsJobLogic extends AbstractControllerLogic $result = []; $result['job_id'] = $jobId; + + $this->createsJobResult->execute($listGenericJobObject); + return $this->response(['data' => $result]); } diff --git a/app/Classes/Modules/PackingLists/Processors/ListPackingListsJobProcessor.php b/app/Classes/Modules/PackingLists/Processors/ListPackingListsJobProcessor.php index 2cbe1846..0849fabc 100644 --- a/app/Classes/Modules/PackingLists/Processors/ListPackingListsJobProcessor.php +++ b/app/Classes/Modules/PackingLists/Processors/ListPackingListsJobProcessor.php @@ -2,10 +2,13 @@ namespace App\Classes\Modules\PackingLists\Processors; +use App\Classes\Exceptions\JobResourceNotFoundException; use App\Classes\Modules\PackingLists\Services\ListsPackingLists; -use App\Classes\Modules\Jobs\Services\CreatesJobResult; +use App\Classes\Modules\Jobs\Services\UpdatesJobResult; +use App\Classes\Modules\Jobs\Services\FetchesJobResult; use App\Classes\General\Helper; use App\Classes\Modules\Jobs\DataTransferObjects\ListGenericJobObject; +use App\Classes\Modules\Jobs\DataTransferObjects\UpdateJobResultObject; use App\Http\Resources\ListPackingListJobResource; class ListPackingListsJobProcessor @@ -14,24 +17,30 @@ class ListPackingListsJobProcessor /** @var ListsPackingLists */ private $listsPackingLists; - /** @var CreatesJobResult */ - private $createsJobResult; + /** @var FetchesJobResult */ + private $fetchesJobResult; + + /** @var UpdatesJobResult */ + private $updatesJobResult; /** * ListPackingListsJobProcessor constructor. * @param ListsPackingLists $listsPackingLists - * @param CreatesJobResult $createsJobResult + * @param FetchesJobResult $fetchesJobResult + * @param UpdatesJobResult $updatesJobResult */ - public function __construct(ListsPackingLists $listsPackingLists, CreatesJobResult $createsJobResult) + public function __construct(ListsPackingLists $listsPackingLists, FetchesJobResult $fetchesJobResult, UpdatesJobResult $updatesJobResult) { $this->listsPackingLists = $listsPackingLists; - $this->createsJobResult = $createsJobResult; + $this->fetchesJobResult = $fetchesJobResult; + $this->updatesJobResult = $updatesJobResult; } /** * @param ListGenericJobObject $listGenericJobObject - * @return null|object + * @return void * @throws \App\Classes\Exceptions\MalformedRequestException + * @throws \App\Classes\Exceptions\JobResourceNotFoundException */ public function execute(ListGenericJobObject $listGenericJobObject) { @@ -39,9 +48,30 @@ class ListPackingListsJobProcessor foreach ($query->items() as &$item) { $item['userInfo'] = $listGenericJobObject->getUserInfo(); } - $result = Helper::collectionResponse(ListPackingListJobResource::collection($query)); - $create = $this->createsJobResult->execute($listGenericJobObject, json_encode($result)); + $resultCurrent = Helper::collectionResponse(ListPackingListJobResource::collection($query)); - return $create; + $jobResultCurrent = $this->fetchesJobResult->execute(['job_id' => $listGenericJobObject->getJobId()]); + $resultCurrentJson = json_encode($resultCurrent); + $resultSignatureCurrent = md5($resultCurrentJson); + + try{ + $jobResultExisting = $this->fetchesJobResult->execute(['request_signature' => $jobResultCurrent->request_signature, 'result_not_null' => true, 'order_by_id_desc' => true]); + $resultSignatureExisting = $jobResultExisting->result_signature; + if($resultSignatureExisting != $resultSignatureCurrent){ + $this->updateJobResult($jobResultCurrent, $resultCurrentJson, $resultSignatureCurrent, $listGenericJobObject->getJobCommandName(), $listGenericJobObject->getJobCommand()); + } + } catch (JobResourceNotFoundException $exception){ + $this->updateJobResult($jobResultCurrent, $resultCurrentJson, $resultSignatureCurrent, $listGenericJobObject->getJobCommandName(), $listGenericJobObject->getJobCommand()); + } + } + + private function updateJobResult($jobResultCurrent, $resultCurrentJson, $resultSignatureCurrent, $jobCommandName, $jobCommand){ + $updateJobResultObject = new UpdateJobResultObject( + $resultCurrentJson, + $resultSignatureCurrent, + $jobCommandName, + $jobCommand + ); + $create = $this->updatesJobResult->execute($jobResultCurrent, $updateJobResultObject); } } diff --git a/database/migrations/2023_08_08_124848_create_job_results_table.php b/database/migrations/2023_08_08_124848_create_job_results_table.php index ee3553bf..ef448407 100644 --- a/database/migrations/2023_08_08_124848_create_job_results_table.php +++ b/database/migrations/2023_08_08_124848_create_job_results_table.php @@ -16,7 +16,7 @@ class CreateJobResultsTable extends Migration Schema::create('job_results', function (Blueprint $table) { $table->id(); $table->string('job_id', 50); - $table->longText('result'); + $table->longText('result')->nullable(); $table->timestamps(); // $table->foreign('job_id')->references('id')->on('jobs')->onDelete('cascade'); diff --git a/database/migrations/2023_12_11_193200_add_new_column_2_to_job_results_table.php b/database/migrations/2023_12_11_193200_add_new_column_2_to_job_results_table.php new file mode 100644 index 00000000..12c6d57f --- /dev/null +++ b/database/migrations/2023_12_11_193200_add_new_column_2_to_job_results_table.php @@ -0,0 +1,34 @@ +string('request_signature')->after('job_id')->nullable(); + $table->string('result_signature')->after('request_signature')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('job_results', function (Blueprint $table) { + $table->dropColumn('request_signature'); + $table->dropColumn('result_signature'); + }); + } +} diff --git a/resources/assets/vue/components/general/elements/ListPollingComponent.vue b/resources/assets/vue/components/general/elements/ListPollingComponent.vue index b7afe520..e3f8a8c3 100644 --- a/resources/assets/vue/components/general/elements/ListPollingComponent.vue +++ b/resources/assets/vue/components/general/elements/ListPollingComponent.vue @@ -126,7 +126,7 @@ errorHandler(error){ this.isPolling = false; }, - startPolling(jobId, maxAttempts = 3) { + startPolling(jobId, maxAttempts = 4) { let attempts = 0; const pollJobResult = () => { if (this.isPolling) { @@ -149,7 +149,7 @@ }; // pollJobResult(); // Initial call - this.pollingInterval = setInterval(pollJobResult, 10000); + this.pollingInterval = setInterval(pollJobResult, 15000); }, stopPolling() { clearInterval(this.pollingInterval);