Skip to content

Commit

Permalink
[TM-1531] delayed job with data (#603)
Browse files Browse the repository at this point in the history
* [TM-1531] entity record and creator to delayedJOb

* [TM-1531] add useer to endpoint

* [TM-1531] add entity data for polygons validations

* [TM-1531] lint

* [TM-1531] add is_cleared

* [TM-1531] add to fix polygons entity

* [TM-1531] store delayed data for uploads

* [TM-1531] send mails when job for upload, check or fix is complete

* [TM-1531] send correct user for mails

* [TM-1531] modify column name on delayed jobs table

* [TM-1531] change attribute name to progress message

* [TM-1531] lint fix

---------

Co-authored-by: cesarLima1 <[email protected]>
  • Loading branch information
egrojMonroy and cesarLima1 authored Dec 9, 2024
1 parent 2b7366c commit 2619ca9
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ public function clipOverlappingPolygonsBySite(string $uuid)
ini_set('max_execution_time', self::MAX_EXECUTION_TIME);
ini_set('memory_limit', '-1');
$user = Auth::user();
$site = Site::isUuid($uuid)->first();
$polygonUuids = GeometryHelper::getSitePolygonsUuids($uuid)->toArray();
$delayedJob = DelayedJobProgress::create([
'processed_content' => 0,
'created_by' => $user->id,
'entity_id' => $site->id,
'entity_type' => get_class($site),
]);
$job = new FixPolygonOverlapJob($delayedJob->id, $polygonUuids, $user->id);
dispatch($job);
Expand Down Expand Up @@ -80,6 +84,9 @@ public function clipOverlappingPolygonsOfProjectBySite(string $uuid)

$delayedJob = DelayedJobProgress::create([
'processed_content' => 0,
'entity_id' => $sitePolygon->id,
'entity_type' => get_class($sitePolygon),
'created_by' => $user->id,
]);
$job = new FixPolygonOverlapJob($delayedJob->id, $uniquePolygonUuids, $user->id);
dispatch($job);
Expand All @@ -92,6 +99,11 @@ public function clipOverlappingPolygons(Request $request)
ini_set('max_execution_time', self::MAX_EXECUTION_TIME);
ini_set('memory_limit', '-1');
$uuids = $request->input('uuids');
$uuid = $request->input('entity_uuid');
$type = $request->input('entity_type');
if ($type === 'sites') {
$entity = Site::where('uuid', $uuid)->firstOrFail();
}
Log::info('Clipping polygons', ['uuids' => $uuids]);
if (empty($uuids) || ! is_array($uuids)) {
return response()->json(['error' => 'Invalid or missing UUIDs'], 400);
Expand Down Expand Up @@ -130,6 +142,9 @@ public function clipOverlappingPolygons(Request $request)
$user = Auth::user();
$delayedJob = DelayedJobProgress::create([
'processed_content' => 0,
'entity_id' => $entity->id,
'entity_type' => get_class($entity),
'created_by' => $user->id,
]);
$job = new FixPolygonOverlapJob($delayedJob->id, $polygonUuids, $user->id);
dispatch($job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,16 @@ function ($attribute, $value, $fail) {
return response()->json($polygonLoaded->original, 200);
}

$user = Auth::user();
$entity = Site::where('uuid', $site_id)->firstOrFail();

$redis_key = 'kml_file_' . uniqid();
Redis::set($redis_key, $geojsonContent, 'EX', 7200);
$delayedJob = DelayedJob::create();
$delayedJob = DelayedJob::create([
'created_by' => $user->id,
'entity_id' => $entity->id,
'entity_type' => get_class($entity),
]);

$job = new InsertGeojsonToDBJob(
$redis_key,
Expand Down Expand Up @@ -394,10 +401,16 @@ public function uploadShapefile(Request $request)

return response()->json($polygonLoaded->original, 200);
}
$user = Auth::user();
$entity = Site::where('uuid', $site_id)->firstOrFail();

$redis_key = 'shapefile_file_' . uniqid();
Redis::set($redis_key, $geojsonContent, 'EX', 7200);
$delayedJob = DelayedJob::create();
$delayedJob = DelayedJob::create([
'created_by' => $user->id,
'entity_id' => $entity->id,
'entity_type' => get_class($entity),
]);

$job = new InsertGeojsonToDBJob(
$redis_key,
Expand Down Expand Up @@ -614,10 +627,16 @@ public function uploadGeoJSONFile(Request $request)
return response()->json($polygonLoaded->original, 200);
}

$user = Auth::user();
$entity = Site::where('uuid', $site_id)->firstOrFail();

$redis_key = 'geojson_file_' . uniqid();
Redis::set($redis_key, $geojson_content, 'EX', 7200);
$delayedJob = DelayedJob::create();
$delayedJob = DelayedJob::create([
'created_by' => $user->id,
'entity_id' => $entity->id,
'entity_type' => get_class($entity),
]);

$job = new InsertGeojsonToDBJob(
$redis_key,
Expand Down Expand Up @@ -1219,10 +1238,15 @@ public function runSiteValidationPolygon(Request $request)
try {
$uuid = $request->input('uuid');

$user = Auth::user();
$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,
'entity_id' => $entity->id,
'entity_type' => get_class($entity),
]);
$job = new RunSitePolygonsValidationJob($delayedJob->id, $sitePolygonsUuids);
dispatch($job);
Expand All @@ -1239,9 +1263,18 @@ public function runPolygonsValidation(Request $request)
{
try {
$uuids = $request->input('uuids');
$uuid = $request->input('entity_uuid');
$type = $request->input('entity_type');
if ($type === 'sites') {
$entity = Site::where('uuid', $uuid)->firstOrFail();
}
$user = Auth::user();
$delayedJob = DelayedJobProgress::create([
'total_content' => count($uuids),
'processed_content' => 0,
'created_by' => $user->id,
'entity_id' => $entity->id,
'entity_type' => get_class($entity),
]);
$job = new RunSitePolygonsValidationJob($delayedJob->id, $uuids);
dispatch($job);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/DelayedJobProgressResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function toArray(Request $request): array
'job_uuid' => $this->uuid,
'proccessed_content' => $this->processed_content,
'total_content' => $this->total_content,
'proccess_message' => $this->proccess_message,
'progress_message' => $this->progress_message,
];
}
}
14 changes: 13 additions & 1 deletion app/Jobs/FixPolygonOverlapJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Jobs;

use App\Http\Middleware\SetAuthenticatedUserForJob;
use App\Mail\PolygonOperationsComplete;
use App\Models\DelayedJob;
use App\Models\DelayedJobProgress;
use App\Services\PolygonService;
Expand All @@ -16,6 +17,7 @@
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Throwable;

class FixPolygonOverlapJob implements ShouldQueue
Expand Down Expand Up @@ -66,6 +68,8 @@ public function handle(): void
try {
$delayedJob = DelayedJobProgress::findOrFail($this->delayed_job_id);
$user = Auth::user();
$site = $delayedJob->entity;
$userForMail = $delayedJob->creator;
if ($user) {
$polygonsClipped = App::make(PolygonService::class)->processClippedPolygons($this->polygonUuids, $this->delayed_job_id);
$delayedJob->update([
Expand All @@ -74,6 +78,14 @@ public function handle(): void
'status_code' => Response::HTTP_OK,
'progress' => 100,
]);

Mail::to($user->email_address)
->send(new PolygonOperationsComplete(
$site,
'Fix',
$userForMail,
now()
));
}
} catch (Exception $e) {
Log::error('Error in Fix Polygon Overlap Job: ' . $e->getMessage());
Expand All @@ -84,7 +96,7 @@ public function handle(): void
'status_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
]);
} catch (Throwable $e) {
Log::error('Throwable Error in RunSitePolygonsValidationJob: ' . $e->getMessage());
Log::error('Throwable Error in Fix overlap job: ' . $e->getMessage());

DelayedJob::where('uuid', $this->delayed_job_id)->update([
'status' => DelayedJob::STATUS_FAILED,
Expand Down
14 changes: 13 additions & 1 deletion app/Jobs/InsertGeojsonToDBJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

namespace App\Jobs;

use App\Mail\PolygonOperationsComplete;
use App\Models\DelayedJob;
use App\Services\PolygonService;
use App\Services\SiteService;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Response;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redis;

class InsertGeojsonToDBJob implements ShouldQueue
Expand Down Expand Up @@ -51,6 +53,8 @@ public function __construct(string $redis_key, string $delayed_job_id, ?string $
public function handle(PolygonService $service)
{
$delayedJob = DelayedJob::findOrFail($this->delayed_job_id);
$user = $delayedJob->creator;
$site = $delayedJob->entity;

try {
$geojsonContent = Redis::get($this->redis_key);
Expand Down Expand Up @@ -86,6 +90,14 @@ public function handle(PolygonService $service)
'status_code' => Response::HTTP_OK,
]);

Mail::to($user->email_address)
->send(new PolygonOperationsComplete(
$site,
'Upload',
$user,
now()
));

} catch (Exception $e) {
Log::error('Error in InsertGeojsonToDBJob: ' . $e->getMessage());
$delayedJob->update([
Expand Down
12 changes: 12 additions & 0 deletions app/Jobs/RunSitePolygonsValidationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Jobs;

use App\Mail\PolygonOperationsComplete;
use App\Models\DelayedJob;
use App\Models\DelayedJobProgress;
use App\Services\PolygonValidationService;
Expand All @@ -14,6 +15,7 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;

class RunSitePolygonsValidationJob implements ShouldQueue
{
Expand Down Expand Up @@ -50,6 +52,8 @@ public function handle(PolygonValidationService $validationService)
{
try {
$delayedJob = DelayedJobProgress::findOrFail($this->delayed_job_id);
$user = $delayedJob->creator;
$site = $delayedJob->entity;
foreach ($this->sitePolygonsUuids as $polygonUuid) {
$request = new Request(['uuid' => $polygonUuid]);
$validationService->validateOverlapping($request);
Expand All @@ -74,6 +78,14 @@ public function handle(PolygonValidationService $validationService)
'progress' => 100,
]);

Mail::to($user->email_address)
->send(new PolygonOperationsComplete(
$site,
'Check',
$user,
now()
));

} catch (Exception $e) {
Log::error('Error in RunSitePolygonsValidationJob: ' . $e->getMessage());

Expand Down
35 changes: 35 additions & 0 deletions app/Mail/PolygonOperationsComplete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Mail;

class PolygonOperationsComplete extends I18nMail
{
protected $site;

protected $operation;

protected $completedAt;

public function __construct($site, $operation, $user, $completedAt)
{
parent::__construct($user);

$this->site = $site;
$this->operation = $operation;
$this->completedAt = $completedAt;

$this->setSubjectKey('polygon-validation.subject')
->setTitleKey('polygon-validation.title')
->setBodyKey('polygon-validation.body')
->setParams([
'{operation}' => e($operation),
'{operationUpper}' => strtoupper(e($operation)),
'{siteName}' => e($site->name),
'{completedTime}' => $completedAt->format('H:i'),
])
->setCta('polygon-validation.cta');

$this->link = '/sites/' . $site->uuid;
$this->transactional = true;
}
}
18 changes: 12 additions & 6 deletions app/Models/DelayedJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Models\Traits\HasUuid;
use App\Models\V2\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

Expand All @@ -17,14 +18,19 @@ class DelayedJob extends Model

protected $table = 'delayed_jobs';

protected $fillable = [
'uuid',
'status',
'status_code',
'payload',
];
protected $fillable = ['uuid', 'status', 'status_code', 'payload', 'entity_type', 'entity_id', 'created_by'];

protected $casts = [
'uuid' => 'string',
];

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

public function creator()
{
return $this->belongsTo(User::class, 'created_by');
}
}
6 changes: 3 additions & 3 deletions app/Models/DelayedJobProgress.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public function __construct(array $attributes = [])
$this->fillable = array_merge($this->fillable, [
'processed_content',
'total_content',
'proccess_message',
'progress_message',
]);

$this->casts = array_merge($this->casts, [
'processed_content' => 'integer',
'total_content' => 'integer',
'proccess_message' => 'string',
'progress_message' => 'string',
]);
}

Expand All @@ -30,7 +30,7 @@ public function processMessage(): string
$progress = 0;
}

return $this->proccess_message = 'Running '. $this->processed_content .' out of '
return $this->progress_message = 'Running '. $this->processed_content .' out of '
.$this->total_content. ' polygons ('.$progress.'%)' ;
}
}
5 changes: 5 additions & 0 deletions app/Models/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,9 @@ public function getTotalWorkdaysAttribute(): int
{
return $this->total_paid_workdays + $this->total_volunteer_workdays;
}

public function delayedJobs()
{
return $this->morphMany(DelayedJob::class, 'entity');
}
}
Loading

0 comments on commit 2619ca9

Please sign in to comment.