Implemented job queue table and the gubbins around it

This commit is contained in:
2022-05-24 16:37:45 +01:00
parent fb24b2ee72
commit 6806291554
11 changed files with 439 additions and 78 deletions

110
src/Jobs/JobInspector.php Normal file
View File

@@ -0,0 +1,110 @@
<?php
/*
* AVSDev UF Worker (https://avsdev.uk)
*
* @link https://git.avsdev.uk/avsdev/sprinkle-worker
* @license https://git.avsdev.uk/avsdev/sprinkle-worker/blob/master/LICENSE.md (LGPL-3.0 License)
*/
namespace UserFrosting\Sprinkle\Worker\Jobs;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Psr\Container\ContainerInterface;
use UserFrosting\UniformResourceLocator\Resource as ResourceInstance;
/**
* Job Inspector Class.
*
* Finds all job classes across sprinkles
*
* @author Craig Williams (https://avsdev.uk)
*/
class JobInspector
{
/**
* @var ContainerInterface
*/
protected $ci;
/**
* @var string The resource locator scheme
*/
protected $scheme = 'jobs://';
/**
* Class Constructor.
*
* @param ContainerInterface $ci
*/
public function __construct(ContainerInterface $ci)
{
$this->ci = $ci;
}
/**
* Loop all the available sprinkles and return a list of their jobs.
*
* @return array An array of all the task classes found for every sprinkle
*/
public function getAvailableJobDefinitions()
{
$tasks = $this->ci->locator->listResources($this->scheme, false, false);
return $this->loadAvailableJobDefinitions($tasks);
}
/**
* Process jobs Resource into info.
*
* @param array $taskFiles List of jobs files
*
* @return array
*/
protected function loadAvailableJobDefinitions(array $jobFiles)
{
$jobs = [];
foreach ($jobFiles as $jobFile) {
$job = $this->getJobDefinition($jobFile);
if ($job) {
$jobs[] = $job;
}
}
return $jobs;
}
/**
* Return an array of job details including the class name and the sprinkle name.
*
* @param ResourceInstance $file The job file
*
* @return array The details about a job file [name, class, sprinkle]
*/
protected function getJobDefinition(ResourceInstance $file)
{
// Format the sprinkle name for the namespace
$sprinkleName = $file->getLocation()->getName();
$sprinkleName = Str::studly($sprinkleName);
// Getting base path, name and class name
$basePath = str_replace($file->getBasename(), '', $file->getBasePath());
$name = $basePath . $file->getFilename();
$className = str_replace('/', '\\', $basePath) . $file->getFilename();
$classPath = "\\UserFrosting\\Sprinkle\\$sprinkleName\\Worker\\Jobs\\$className";
// Exclude known helper classes
if ($className == "JobInterface" || $className == "JobBase" || $className == "JobInspector") {
return null;
}
// Build the class name and namespace
return [
'sprinkle' => $sprinkleName,
'name' => $name,
'class' => $classPath,
];
}
}