first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-06-08 17:09:23 -04:00
commit df3a033196
17887 changed files with 8637778 additions and 0 deletions
@@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
/**
* @file classes/job/repositories/BaseRepository.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2000-2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class BaseRepository
*
* @brief Abstract class BaseRepository
*/
namespace PKP\job\repositories;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
abstract class BaseRepository
{
protected Model $model;
protected int $perPage = 50;
protected ?string $outputFormat;
public const OUTPUT_CLI = 'cli';
public const OUTPUT_HTTP = 'http';
public function newQuery(): Builder
{
return $this->model->newQuery();
}
public function all(array $columns = ['*']): Collection
{
return $this->model->all($columns);
}
public function get(int $modelId): ?Model
{
return $this->model->find($modelId);
}
public function add(array $attributes = []): ?Model
{
return $this->model->create($attributes);
}
public function edit(int $modelId, array $data): bool
{
return $this->model->find($modelId)->update($data);
}
public function delete(int $modelId): bool
{
return $this->model->find($modelId)->delete();
}
public function total(): int
{
return $this->model->count();
}
public function setOutputFormat(string $format): self
{
$this->outputFormat = $format;
return $this;
}
public function setPage(int $page): self
{
LengthAwarePaginator::currentPageResolver(fn () => $page);
return $this;
}
public function perPage(int $perPage): self
{
$this->perPage = $perPage;
return $this;
}
public function deleteJobs(string $queue = null, array $ids = []): int
{
$query = $this->model->newQuery();
if ($queue) {
$query = $query->queuedAt($queue);
}
if (!empty($ids)) {
$query = $query->whereIn('id', $ids);
}
return $query->delete();
}
/**
* Show jobs
*/
abstract public function showJobs(): LengthAwarePaginator;
}
@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
/**
* @file classes/job/repositories/Job.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2000-2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class FailedJob
*
* @brief Job Repository
*/
namespace PKP\job\repositories;
use Carbon\Carbon;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use PKP\facades\Repo;
use PKP\job\models\FailedJob as PKPFailedJobModel;
use PKP\job\resources\CLIFailedJobResource;
use PKP\job\resources\HttpFailedJobResource;
class FailedJob extends BaseRepository
{
public function __construct(PKPFailedJobModel $model)
{
$this->model = $model;
}
public function showJobs(): LengthAwarePaginator
{
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$sanitizedPage = $currentPage - 1;
$offsetRows = $this->perPage * $sanitizedPage;
$total = $this->model->count();
$data = $this->model
->skip($offsetRows)
->take($this->perPage)
->get();
return new LengthAwarePaginator(
$this->getOutput($data),
$total,
$this->perPage
);
}
public function getRedispatchableJobsInQueue(string $queue = null, array $columns = ['*']): collection
{
$failedJobs = $this->newQuery()->select($columns);
if ($queue) {
$failedJobs = $failedJobs->queuedAt($queue);
}
return $failedJobs->where(
fn ($query) => $query
->whereNotNull('payload')
->whereRaw("payload <> ''")
)->get();
}
public function redispatchToQueue(string $queue = null, array $failedIds = []): int
{
$failedJobs = $this->newQuery();
if ($queue) {
$failedJobs = $failedJobs->queuedAt($queue);
}
if (!empty($failedIds)) {
$failedJobs = $failedJobs->whereIn('id', $failedIds);
}
$failedJobs = $failedJobs->get();
DB::beginTransaction();
$failedJobs->each(fn ($failedJob) => Repo::job()->add([
'queue' => $failedJob->queue,
'payload' => $failedJob->payload,
'attempts' => 0,
'available_at' => Carbon::now()->timestamp,
'created_at' => Carbon::now()->timestamp,
]));
DB::commit();
return $failedJobs->toQuery()->delete();
}
protected function getOutput(Collection $data)
{
if ($this->outputFormat === self::OUTPUT_CLI) {
return CLIFailedJobResource::collection($data);
}
return HttpFailedJobResource::collection($data);
}
}
+72
View File
@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
/**
* @file classes/job/repositories/Job.php
*
* Copyright (c) 2014-2022 Simon Fraser University
* Copyright (c) 2000-2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class Job
*
* @brief Job Repository
*/
namespace PKP\job\repositories;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use PKP\job\models\Job as PKPJobModel;
use PKP\job\resources\CLIJobResource;
use PKP\job\resources\HttpJobResource;
class Job extends BaseRepository
{
public function __construct(PKPJobModel $model)
{
$this->model = $model;
}
public function total(): int
{
return $this->model
->nonEmptyQueue()
->nonReserved()
->count();
}
public function showJobs(): LengthAwarePaginator
{
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$sanitizedPage = $currentPage - 1;
$offsetRows = $this->perPage * $sanitizedPage;
$query = $this->model
->nonEmptyQueue()
->nonReserved();
$total = $query->count();
$data = $query
->skip($offsetRows)
->take($this->perPage)
->get();
return new LengthAwarePaginator(
$this->getOutput($data),
$total,
$this->perPage
);
}
protected function getOutput(Collection $data)
{
if ($this->outputFormat == self::OUTPUT_CLI) {
return CLIJobResource::collection($data);
}
return HttpJobResource::collection($data);
}
}