Skip to content

Commit

Permalink
[TM-1457] add delayed job to organisations export (#576)
Browse files Browse the repository at this point in the history
* [TM-1457] add delayed job to organisations export

* [TM-1457] add delayed job functionality to export organisations

* remove comment
  • Loading branch information
LimberHope authored Nov 21, 2024
1 parent f4be18c commit 49ca423
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 6 deletions.
5 changes: 5 additions & 0 deletions app/Exports/V2/OrganisationsExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public function collection(): Collection
])->get();
}

public function chunkSize(): int
{
return 1000;
}

public function headings(): array
{
$headings = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,45 @@

namespace App\Http\Controllers\V2\Organisations;

use App\Exports\V2\OrganisationsExport;
use App\Http\Controllers\Controller;
use App\Http\Resources\DelayedJobResource;
use App\Jobs\ExportAllOrganisationsJob;
use App\Models\DelayedJob;
use App\Models\V2\Organisation;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class AdminExportOrganisationsController extends Controller
{
public function __invoke(Request $request): BinaryFileResponse
public function __invoke(Request $request)
{
$this->authorize('export', Organisation::class);

$filename = 'organisations(' . now()->format('d-m-Y-H-i'). ').csv';
$filename = 'organisations(' . now()->format('d-m-Y'). ').csv';
$relativePath = 'exports/' . $filename;
$absolutePath = storage_path('app/' . $relativePath);

return (new OrganisationsExport())->download($filename, Excel::CSV)->deleteFileAfterSend(true);
try {
$binary_data = Redis::get('exports:organisations:'.$filename);
if (! $binary_data) {
$delayedJob = DelayedJob::create();
$job = new ExportAllOrganisationsJob(
$delayedJob->id,
$filename
);
dispatch($job);

return (new DelayedJobResource($delayedJob))->additional(['message' => "Export for organisations $filename is being processed"]);
} else {
file_put_contents($absolutePath, $binary_data);

return response()->download($absolutePath, $filename)->deleteFileAfterSend(true);
}
} catch (\Exception $e) {
Log::error('Error during export for organisations : ' . $e->getMessage());

return response()->json(['error' => 'An error occurred during organisations export'], 500);
}
}
}
66 changes: 66 additions & 0 deletions app/Jobs/ExportAllOrganisationsJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Jobs;

use App\Models\DelayedJob;
use App\Services\ExportAllOrganisationsService;
use Exception;
use Illuminate\Bus\Queueable;
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\Log;
use Illuminate\Support\Facades\Redis;
use Maatwebsite\Excel\Facades\Excel;

class ExportAllOrganisationsJob implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

public $timeout = 0;

protected $uuid;

protected $delayed_job_id;

protected $file_name;

public function __construct(string $delayed_job_id, string $file_name)
{
$this->file_name = $file_name;
$this->delayed_job_id = $delayed_job_id;
}

public function handle(ExportAllOrganisationsService $exportAllOrganisationsService)
{
try {
$delayedJob = DelayedJob::findOrFail($this->delayed_job_id);
$relativePath = 'exports/' . $this->file_name;

Excel::store($exportAllOrganisationsService->run(), $relativePath);

$absolutePath = storage_path('app/' . $relativePath);
$binary_data = file_get_contents($absolutePath);

Redis::set('exports:organisations:'.$this->file_name, $binary_data, 'EX', 7200);
$delayedJob->update([
'status' => DelayedJob::STATUS_SUCCEEDED,
'payload' => ['message' => 'All Organisations Export completed'],
'status_code' => Response::HTTP_OK,
]);
} catch (Exception $e) {
Log::error('Error in ExportAllOrganisationsJob: ' . $e->getMessage());

DelayedJob::where('id', $this->delayed_job_id)->update([
'status' => DelayedJob::STATUS_FAILED,
'payload' => json_encode(['error' => $e->getMessage()]),
'status_code' => Response::HTTP_INTERNAL_SERVER_ERROR,
]);
}
}
}
13 changes: 13 additions & 0 deletions app/Services/ExportAllOrganisationsService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Services;

use App\Exports\V2\OrganisationsExport;

class ExportAllOrganisationsService
{
public function run()
{
return new OrganisationsExport();
}
}

0 comments on commit 49ca423

Please sign in to comment.