SmartCane/laravel_app/app/Jobs/ProjectDownloadTiffJob.php
Martin Folkerts 4c22ab6758 wip
2024-01-03 22:06:03 +01:00

66 lines
1.7 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',
]);
}
}