85 lines
2.3 KiB
PHP
85 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Project;
|
|
use App\Models\ProjectDownload;
|
|
use Illuminate\Bus\Batchable;
|
|
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 Batchable, 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)
|
|
{
|
|
$filename = sprintf('%s.tif', $date->format('Y-m-d'));
|
|
if ($project->downloads()->where(['status' => 'completed', 'name' => $filename])->count() > 0) {
|
|
return;
|
|
}
|
|
|
|
$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
|
|
);
|
|
}
|
|
}
|