From fdf627b77880ea93a017f78cf8bd6e4ad7d76743 Mon Sep 17 00:00:00 2001 From: JORGE Date: Fri, 20 Dec 2024 13:19:43 -0400 Subject: [PATCH] [TM-1531] remove entity from delayedjobs and create metadata --- .../TerrafundClipGeometryController.php | 25 +++++++---- .../TerrafundCreateGeometryController.php | 43 +++++++++++++------ app/Jobs/FixPolygonOverlapJob.php | 5 ++- app/Jobs/InsertGeojsonToDBJob.php | 6 ++- app/Jobs/RunSitePolygonsValidationJob.php | 14 +++++- app/Models/DelayedJob.php | 7 +-- ...add_metadata_and_remove_entity_columns.php | 31 +++++++++++++ 7 files changed, 100 insertions(+), 31 deletions(-) create mode 100644 database/migrations/2024_12_20_153701_add_metadata_and_remove_entity_columns.php diff --git a/app/Http/Controllers/V2/Terrafund/TerrafundClipGeometryController.php b/app/Http/Controllers/V2/Terrafund/TerrafundClipGeometryController.php index b9e699c8..29479021 100644 --- a/app/Http/Controllers/V2/Terrafund/TerrafundClipGeometryController.php +++ b/app/Http/Controllers/V2/Terrafund/TerrafundClipGeometryController.php @@ -29,8 +29,11 @@ public function clipOverlappingPolygonsBySite(string $uuid) $delayedJob = DelayedJobProgress::create([ 'processed_content' => 0, 'created_by' => $user->id, - 'entity_id' => $site->id, - 'entity_type' => get_class($site), + 'metadata' => json_encode([ + 'entity_id' => $site->id, + 'entity_type' => get_class($site), + 'entity_name' => $site->name, + ]), 'is_acknowledged' => false, 'name' => 'Polygon Fix', ]); @@ -46,8 +49,8 @@ public function clipOverlappingPolygonsOfProjectBySite(string $uuid) ini_set('max_execution_time', self::MAX_EXECUTION_TIME); ini_set('memory_limit', '-1'); $user = Auth::user(); - $sitePolygon = Site::isUuid($uuid)->first(); - $projectId = $sitePolygon->project_id ?? null; + $site = Site::isUuid($uuid)->first(); + $projectId = $site->project_id ?? null; if (! $projectId) { return response()->json(['error' => 'Project not found for the given site UUID.'], 404); @@ -86,8 +89,11 @@ public function clipOverlappingPolygonsOfProjectBySite(string $uuid) $delayedJob = DelayedJobProgress::create([ 'processed_content' => 0, - 'entity_id' => $sitePolygon->id, - 'entity_type' => get_class($sitePolygon), + 'metadata' => json_encode([ + 'entity_id' => $site->id, + 'entity_type' => get_class($site), + 'entity_name' => $site->name, + ]), 'created_by' => $user->id, 'is_acknowledged' => false, 'name' => 'Polygon Fix', @@ -146,8 +152,11 @@ public function clipOverlappingPolygons(Request $request) $user = Auth::user(); $delayedJob = DelayedJobProgress::create([ 'processed_content' => 0, - 'entity_id' => $entity->id, - 'entity_type' => get_class($entity), + 'metadata' => json_encode([ + 'entity_id' => $entity->id, + 'entity_type' => get_class($entity), + 'entity_name' => $entity->name, + ]), 'created_by' => $user->id, 'is_acknowledged' => false, 'name' => 'Polygon Fix', diff --git a/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php b/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php index 0cc2df51..2a32b3e2 100755 --- a/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php +++ b/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php @@ -242,8 +242,11 @@ function ($attribute, $value, $fail) { Redis::set($redis_key, $geojsonContent, 'EX', 7200); $delayedJob = DelayedJob::create([ 'created_by' => $user->id, - 'entity_id' => $entity->id, - 'entity_type' => get_class($entity), + 'metadata' => json_encode([ + 'entity_id' => $entity->id, + 'entity_type' => get_class($entity), + 'entity_name' => $entity->name, + ]), 'is_acknowledged' => false, 'name' => 'Polygon Upload', ]); @@ -410,8 +413,11 @@ public function uploadShapefile(Request $request) Redis::set($redis_key, $geojsonContent, 'EX', 7200); $delayedJob = DelayedJob::create([ 'created_by' => $user->id, - 'entity_id' => $entity->id, - 'entity_type' => get_class($entity), + 'metadata' => json_encode([ + 'entity_id' => $entity->id, + 'entity_type' => get_class($entity), + 'entity_name' => $entity->name, + ]), 'is_acknowledged' => false, 'name' => 'Polygon Upload', ]); @@ -638,8 +644,11 @@ public function uploadGeoJSONFile(Request $request) Redis::set($redis_key, $geojson_content, 'EX', 7200); $delayedJob = DelayedJob::create([ 'created_by' => $user->id, - 'entity_id' => $entity->id, - 'entity_type' => get_class($entity), + 'metadata' => json_encode([ + 'entity_id' => $entity->id, + 'entity_type' => get_class($entity), + 'entity_name' => $entity->name, + ]), 'is_acknowledged' => false, 'name' => 'Polygon Upload', ]); @@ -1248,14 +1257,17 @@ public function runSiteValidationPolygon(Request $request) $entity = Site::where('uuid', $uuid)->firstOrFail(); $sitePolygonsUuids = GeometryHelper::getSitePolygonsUuids($uuid)->toArray(); $delayedJob = DelayedJobProgress::create([ - 'total_content' => count($sitePolygonsUuids), - 'processed_content' => 0, - 'created_by' => $user->id, + 'total_content' => count($sitePolygonsUuids), + 'processed_content' => 0, + 'created_by' => $user->id, + 'metadata' => json_encode([ 'entity_id' => $entity->id, 'entity_type' => get_class($entity), - 'is_acknowledged' => false, - 'name' => 'Polygon Validation', - ]); + 'entity_name' => $entity->name, + ]), + 'is_acknowledged' => false, + 'name' => 'Polygon Validation', + ]); $job = new RunSitePolygonsValidationJob($delayedJob->id, $sitePolygonsUuids); dispatch($job); @@ -1281,8 +1293,11 @@ public function runPolygonsValidation(Request $request) 'total_content' => count($uuids), 'processed_content' => 0, 'created_by' => $user->id, - 'entity_id' => $entity->id, - 'entity_type' => get_class($entity), + 'metadata' => json_encode([ + 'entity_id' => $entity->id, + 'entity_type' => get_class($entity), + 'entity_name' => $entity->name, + ]), 'is_acknowledged' => false, 'name' => 'Polygon Validation', ]); diff --git a/app/Jobs/FixPolygonOverlapJob.php b/app/Jobs/FixPolygonOverlapJob.php index 16850a39..61c66192 100644 --- a/app/Jobs/FixPolygonOverlapJob.php +++ b/app/Jobs/FixPolygonOverlapJob.php @@ -6,6 +6,7 @@ use App\Mail\PolygonOperationsComplete; use App\Models\DelayedJob; use App\Models\DelayedJobProgress; +use App\Models\V2\Sites\Site; use App\Services\PolygonService; use Exception; use Illuminate\Bus\Queueable; @@ -68,7 +69,9 @@ public function handle(): void try { $delayedJob = DelayedJobProgress::findOrFail($this->delayed_job_id); $user = Auth::user(); - $site = $delayedJob->entity; + $metadata = json_decode($delayedJob->metadata, true); + $entityId = $metadata['entity_id'] ?? null; + $site = Site::findOrFail($entityId); $userForMail = $delayedJob->creator; if ($user) { $polygonsClipped = App::make(PolygonService::class)->processClippedPolygons($this->polygonUuids, $this->delayed_job_id); diff --git a/app/Jobs/InsertGeojsonToDBJob.php b/app/Jobs/InsertGeojsonToDBJob.php index a6caf758..a17747d9 100755 --- a/app/Jobs/InsertGeojsonToDBJob.php +++ b/app/Jobs/InsertGeojsonToDBJob.php @@ -4,6 +4,7 @@ use App\Mail\PolygonOperationsComplete; use App\Models\DelayedJob; +use App\Models\V2\Sites\Site; use App\Services\PolygonService; use App\Services\SiteService; use Exception; @@ -54,7 +55,10 @@ public function handle(PolygonService $service) { $delayedJob = DelayedJob::findOrFail($this->delayed_job_id); $user = $delayedJob->creator; - $site = $delayedJob->entity; + $metadata = json_decode($delayedJob->metadata, true); + $entityId = $metadata['entity_id'] ?? null; + + $site = Site::findOrFail($entityId); try { $geojsonContent = Redis::get($this->redis_key); diff --git a/app/Jobs/RunSitePolygonsValidationJob.php b/app/Jobs/RunSitePolygonsValidationJob.php index 34f4f5a9..edf8c0c9 100644 --- a/app/Jobs/RunSitePolygonsValidationJob.php +++ b/app/Jobs/RunSitePolygonsValidationJob.php @@ -5,6 +5,7 @@ use App\Mail\PolygonOperationsComplete; use App\Models\DelayedJob; use App\Models\DelayedJobProgress; +use App\Models\V2\Sites\Site; use App\Services\PolygonValidationService; use Exception; use Illuminate\Bus\Queueable; @@ -53,7 +54,15 @@ public function handle(PolygonValidationService $validationService) try { $delayedJob = DelayedJobProgress::findOrFail($this->delayed_job_id); $user = $delayedJob->creator; - $site = $delayedJob->entity; + $metadata = json_decode($delayedJob->metadata, true); + $entityId = $metadata['entity_id'] ?? null; + + $site = Site::findOrFail($entityId); + + if (! $site) { + throw new Exception('Site not found for the given site UUID.'); + } + foreach ($this->sitePolygonsUuids as $polygonUuid) { $request = new Request(['uuid' => $polygonUuid]); $validationService->validateOverlapping($request); @@ -78,6 +87,8 @@ public function handle(PolygonValidationService $validationService) 'progress' => 100, ]); + Log::info('site available? ' . $site); + Mail::to($user->email_address) ->send(new PolygonOperationsComplete( $site, @@ -95,5 +106,6 @@ public function handle(PolygonValidationService $validationService) 'status_code' => Response::HTTP_INTERNAL_SERVER_ERROR, ]); } + } } diff --git a/app/Models/DelayedJob.php b/app/Models/DelayedJob.php index 51e547df..b0614794 100644 --- a/app/Models/DelayedJob.php +++ b/app/Models/DelayedJob.php @@ -18,17 +18,12 @@ class DelayedJob extends Model protected $table = 'delayed_jobs'; - protected $fillable = ['uuid', 'status', 'status_code', 'payload', 'entity_type', 'entity_id', 'created_by', 'is_acknowledged', 'name']; + protected $fillable = ['uuid', 'status', 'status_code', 'payload', 'metadata', 'created_by', 'is_acknowledged', 'name']; protected $casts = [ 'uuid' => 'string', ]; - public function entity() - { - return $this->morphTo(); - } - public function creator() { return $this->belongsTo(User::class, 'created_by'); diff --git a/database/migrations/2024_12_20_153701_add_metadata_and_remove_entity_columns.php b/database/migrations/2024_12_20_153701_add_metadata_and_remove_entity_columns.php new file mode 100644 index 00000000..22ab0009 --- /dev/null +++ b/database/migrations/2024_12_20_153701_add_metadata_and_remove_entity_columns.php @@ -0,0 +1,31 @@ +json('metadata')->nullable()->after('payload')->comment('Stores additional information for the delayed job.'); + + $table->dropColumn(['entity_id', 'entity_type']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('delayed_jobs', function (Blueprint $table) { + $table->dropColumn('metadata'); + $table->unsignedBigInteger('entity_id')->nullable()->after('name'); + $table->string('entity_type')->nullable()->after('entityId'); + }); + } +};