SmartCane/laravel_app/app/Jobs/ProjectDownloadTiffJob.php
Martin Folkerts da9d94880f wip
2024-01-09 10:49:25 +01:00

83 lines
2.3 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Project;
use App\Models\ProjectDownload;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Process\Exceptions\ProcessFailedException;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Carbon\Carbon;
use Symfony\Component\Process\Process;
class ProjectDownloadTiffJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected Carbon $date;
protected ProjectDownload $download;
protected $days = 1;
/**
* Create a new job instance.
*/
public function __construct(ProjectDownload $download, Carbon $date,)
{
$this->date = $date;
$this->download = $download;
}
/**
* Execute the job.
*/
public function handle(): void
{
$projectFolder = base_path('../');
$command = [
sprintf('%srunpython.sh', $projectFolder),
sprintf('--date=%s', $this->date->format('Y-m-d')),
sprintf('--days=%d', $this->days),
];
// Convert commands array to a single string
$process = new Process($command);
$process->setTimeout(3600); // stel een geschikte timeout in
$process->start();
try {
$process->wait(function ($type, $buffer) use (&$myOutput) {
logger($buffer);
});
} catch (ProcessFailedException $exception) {
logger('error', $exception->getMessage());
}
$this->download->update([
'status' => 'completed',
]);
}
public static function handleForDate(Project $project, Carbon $date)
{
if ($project->downloads()->where(['status' => 'completed', 'date' => $date])->count() > 0) {
return;
}
$filename = sprintf('%s.tif', $date->format('Y-m-d'));
$path = sprintf('%s/%s/%s', $project->download_path, 'merged_final_tif', $filename);
return new self(
$project->downloads()->create([
'name' => $filename,
'path' => $path,
'status' => 'completed',
]),
$date
);
}
}