Skip to content

Commit

Permalink
[TM-1531] remove entity from delayedjobs and create metadata (#632)
Browse files Browse the repository at this point in the history
* [TM-1531] remove entity from delayedjobs and create metadata

* [TM-1531] change metadata casted

* [TM-1531] change array to json
  • Loading branch information
egrojMonroy authored Dec 20, 2024
1 parent 0d3763a commit 8f21795
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
'entity_id' => $site->id,
'entity_type' => get_class($site),
'entity_name' => $site->name,
],
'is_acknowledged' => false,
'name' => 'Polygon Fix',
]);
Expand All @@ -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);
Expand Down Expand Up @@ -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' => [
'entity_id' => $site->id,
'entity_type' => get_class($site),
'entity_name' => $site->name,
],
'created_by' => $user->id,
'is_acknowledged' => false,
'name' => 'Polygon Fix',
Expand Down Expand Up @@ -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' => [
'entity_id' => $entity->id,
'entity_type' => get_class($entity),
'entity_name' => $entity->name,
],
'created_by' => $user->id,
'is_acknowledged' => false,
'name' => 'Polygon Fix',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
'entity_id' => $entity->id,
'entity_type' => get_class($entity),
'entity_name' => $entity->name,
],
'is_acknowledged' => false,
'name' => 'Polygon Upload',
]);
Expand Down Expand Up @@ -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' => [
'entity_id' => $entity->id,
'entity_type' => get_class($entity),
'entity_name' => $entity->name,
],
'is_acknowledged' => false,
'name' => 'Polygon Upload',
]);
Expand Down Expand Up @@ -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' => [
'entity_id' => $entity->id,
'entity_type' => get_class($entity),
'entity_name' => $entity->name,
],
'is_acknowledged' => false,
'name' => 'Polygon Upload',
]);
Expand Down Expand Up @@ -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' => [
'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);

Expand All @@ -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' => [
'entity_id' => $entity->id,
'entity_type' => get_class($entity),
'entity_name' => $entity->name,
],
'is_acknowledged' => false,
'name' => 'Polygon Validation',
]);
Expand Down
5 changes: 4 additions & 1 deletion app/Jobs/FixPolygonOverlapJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,7 +69,9 @@ public function handle(): void
try {
$delayedJob = DelayedJobProgress::findOrFail($this->delayed_job_id);
$user = Auth::user();
$site = $delayedJob->entity;
$metadata = $delayedJob->metadata;
$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);
Expand Down
6 changes: 5 additions & 1 deletion app/Jobs/InsertGeojsonToDBJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +55,10 @@ public function handle(PolygonService $service)
{
$delayedJob = DelayedJob::findOrFail($this->delayed_job_id);
$user = $delayedJob->creator;
$site = $delayedJob->entity;
$metadata = $delayedJob->metadata;
$entityId = $metadata['entity_id'] ?? null;

$site = Site::findOrFail($entityId);

try {
$geojsonContent = Redis::get($this->redis_key);
Expand Down
19 changes: 18 additions & 1 deletion app/Jobs/RunSitePolygonsValidationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,7 +54,20 @@ public function handle(PolygonValidationService $validationService)
try {
$delayedJob = DelayedJobProgress::findOrFail($this->delayed_job_id);
$user = $delayedJob->creator;
$site = $delayedJob->entity;
$metadata = $delayedJob->metadata;

$entityId = $metadata['entity_id'] ?? null;

if ($entityId) {
$site = Site::findOrFail($entityId);
} else {
Log::error('entityId is null, unable to find site');
}

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);
Expand All @@ -78,6 +92,8 @@ public function handle(PolygonValidationService $validationService)
'progress' => 100,
]);

Log::info('site available? ' . $site);

Mail::to($user->email_address)
->send(new PolygonOperationsComplete(
$site,
Expand All @@ -95,5 +111,6 @@ public function handle(PolygonValidationService $validationService)
'status_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
]);
}

}
}
8 changes: 2 additions & 6 deletions app/Models/DelayedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ 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',
'metadata' => 'json',
];

public function entity()
{
return $this->morphTo();
}

public function creator()
{
return $this->belongsTo(User::class, 'created_by');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('delayed_jobs', function (Blueprint $table) {
$table->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');
});
}
};

0 comments on commit 8f21795

Please sign in to comment.