171 lines
5.9 KiB
PHP
171 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Project;
|
|
use App\Models\ProjectMosaic;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Bus\Batchable;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Process\Exceptions\ProcessFailedException;
|
|
use Illuminate\Process\Exceptions\ProcessTimedOutException;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Symfony\Component\Process\Process;
|
|
use Illuminate\Support\Facades\Process as ProcessNew;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class ProjectMosiacGeneratorJob implements ShouldQueue
|
|
{
|
|
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public ProjectMosaic $mosaic;
|
|
|
|
public $timeout = 220;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct(ProjectMosaic $mosaic)
|
|
{
|
|
$this->mosaic = $mosaic;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handleNew(): void
|
|
{
|
|
//$weeksAgo = ProjectReport::weeksAgoForYearAndWeek($this->mosaic->year, $this->mosaic->week);
|
|
|
|
//TODO change to end_date and offset
|
|
|
|
$projectFolder = base_path('../');
|
|
|
|
$command = [
|
|
sprintf('%sbuild_mosaic.sh', $projectFolder),
|
|
sprintf('--end_date=%s', $this->mosaic->end_date),
|
|
sprintf('--offset=%s', $this->mosaic->offset),
|
|
sprintf('--data_dir=%s', $this->mosaic->project->download_path),
|
|
];
|
|
|
|
$process = new Process($command);
|
|
$process->setTimeout(220);
|
|
$currentPath = '/usr/bin:/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin/Users/mfolkerts/anaconda3/bin:/Library/Apple/usr/bin';
|
|
|
|
$process->setEnv(['PATH' => $currentPath.':/usr/local/Cellar/pandoc/3.1.8/bin/pandoc']);
|
|
// $process->setTimeout(36000); // stel een geschikte timeout in
|
|
$process->start();
|
|
try {
|
|
$myOutput = [];
|
|
$process->wait(function ($type, $buffer) use (&$myOutput) {
|
|
// $this->stream(to: 'processOutput', content: $buffer);
|
|
$myOutput[] = $buffer;
|
|
logger($buffer);
|
|
});
|
|
$this->processOutput = collect($myOutput)->join('\n');
|
|
$this->mosaic->setStatusSuccess();
|
|
|
|
} catch (ProcessFailedException $exception) {
|
|
echo $exception->getMessage();
|
|
$this->mosaic->setStatusFailed();
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
//$weeksAgo = ProjectReport::weeksAgoForYearAndWeek($this->mosaic->year, $this->mosaic->week);
|
|
|
|
|
|
$projectFolder = base_path('../');
|
|
|
|
$command = [
|
|
sprintf('%sbuild_mosaic.sh', $projectFolder),
|
|
sprintf('--end_date=%s', $this->mosaic->end_date->format('Y-m-d')),
|
|
sprintf('--offset=%s', $this->mosaic->offset),
|
|
sprintf('--data_dir=%s', $this->mosaic->project->download_path),
|
|
];
|
|
$currentPath = '/usr/bin:/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin/Users/mfolkerts/anaconda3/bin:/Library/Apple/usr/bin';
|
|
|
|
try{
|
|
$process = ProcessNew::timeout(220)
|
|
->env(['PATH' => $currentPath . ':/usr/local/Cellar/pandoc/3.1.8/bin/pandoc'])
|
|
->start($command, function (string $type, string $output ) {
|
|
logger($output);
|
|
});
|
|
$results = $process->wait();
|
|
if($results->successful()){
|
|
$this->mosaic->setStatusSuccess();
|
|
}
|
|
}catch(\RuntimeException|ProcessTimedOutException|ProcessFailedException $e){
|
|
echo $e->getMessage();
|
|
$this->mosaic->setStatusFailed();
|
|
}
|
|
}
|
|
|
|
// public static function handleFor(Project $project, $year, $startWeekNumber)
|
|
// {
|
|
// logger("ProjectMosiacGeneratorJob::handleFor(week $startWeekNumber, year $year)");
|
|
// if ($project->hasInvalidMosaicFor($year, $startWeekNumber)) {
|
|
// logger("ProjecMosaicGeneratorJob::handleFor(week $startWeekNumber, year $year): InvalidMosaic.");
|
|
// return new NullJob();
|
|
// }
|
|
//
|
|
// $mosaic = $project->mosaics()->updateOrCreate(
|
|
// [
|
|
// 'year' => $year,
|
|
// 'week' => $startWeekNumber,
|
|
// ],
|
|
// [
|
|
// 'name' => sprintf('Week %d, %d', $startWeekNumber, $year),
|
|
// 'path' => sprintf('%s/%s/%s',
|
|
// $project->download_path,
|
|
// 'mosaics',
|
|
// ProjectMosaic::getFilenameForYearAndWeek($year, $startWeekNumber)
|
|
// ),
|
|
// 'year' => $year,
|
|
// 'week' => $startWeekNumber,
|
|
// ]);
|
|
// return new self($mosaic);
|
|
// }
|
|
|
|
/**
|
|
* Check if the project has a mosaic for given period.
|
|
*/
|
|
public static function handleFor(Project $project,Carbon $endDate, int $offset): NullJob|ProjectMosiacGeneratorJob
|
|
{
|
|
logger("ProjectMosiacGeneratorJob::handleFor($endDate, $offset)");
|
|
if ($project->hasInvalidMosaicFor($endDate,$offset)) {
|
|
logger("ProjecMosaicGeneratorJob::handleFor(end_date: $endDate, offset: $offset): InvalidMosaic.");
|
|
return new NullJob();
|
|
}
|
|
/**
|
|
* @var ProjectMosaic $mosaic
|
|
*/
|
|
$mosaic = $project->mosaics()->updateOrCreate(
|
|
[
|
|
'end_date' => $endDate,
|
|
'offset' => $offset,
|
|
],
|
|
[
|
|
'name' => sprintf('Period %s - %s', $endDate->format('Y-m-d'), $endDate->copy()->subDays($offset)->format('Y-m-d')),
|
|
'path' => sprintf('%s/%s/%s',
|
|
$project->download_path,
|
|
'mosaics',
|
|
ProjectMosaic::getFilenameByPeriod($endDate,$offset)
|
|
),
|
|
'end_date' => $endDate->format('Y-m-d'),
|
|
'offset' => $offset,
|
|
]);
|
|
return new self($mosaic);
|
|
}
|
|
}
|