mirror of
https://github.com/anikeen-com/print-cli.git
synced 2026-03-13 13:46:07 +00:00
Initial commit
This commit is contained in:
48
app/Commands/InspireCommand.php
Normal file
48
app/Commands/InspireCommand.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Commands;
|
||||
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use LaravelZero\Framework\Commands\Command;
|
||||
|
||||
use function Termwind\{render};
|
||||
|
||||
class InspireCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The signature of the command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'inspire {name=Artisan}';
|
||||
|
||||
/**
|
||||
* The description of the command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Display an inspiring quote';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
render(<<<'HTML'
|
||||
<div class="py-1 ml-2">
|
||||
<div class="px-1 bg-blue-300 text-black">Laravel Zero</div>
|
||||
<em class="ml-1">
|
||||
Simplicity is the ultimate sophistication.
|
||||
</em>
|
||||
</div>
|
||||
HTML);
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the command's schedule.
|
||||
*/
|
||||
public function schedule(Schedule $schedule): void
|
||||
{
|
||||
// $schedule->command(static::class)->everyMinute();
|
||||
}
|
||||
}
|
||||
119
app/Commands/ServeCommand.php
Normal file
119
app/Commands/ServeCommand.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace App\Commands;
|
||||
|
||||
use Illuminate\Http\Client\RequestException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use LaravelZero\Framework\Commands\Command;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Throwable;
|
||||
|
||||
class ServeCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'serve';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Command description';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->info('Starting service...');
|
||||
|
||||
$yaml = $this->getConfiguration();
|
||||
$printerIds = array_map(fn($printer) => $printer['id'], $yaml['printers']);
|
||||
|
||||
$response = Http::patch(sprintf('%s/api/printers/register', $yaml['base_url']), [
|
||||
'printers' => $yaml['printers'],
|
||||
'status' => 'online',
|
||||
]);
|
||||
|
||||
$response->throw();
|
||||
|
||||
$this->info('Service started!');
|
||||
|
||||
do {
|
||||
$response = Http::get(sprintf('%s/api/printers/jobs', $yaml['base_url']), [
|
||||
'printer_ids' => $printerIds,
|
||||
]);
|
||||
|
||||
if ($response->failed()) {
|
||||
$this->error('Failed to fetch jobs, error: ' . $response->status());
|
||||
sleep(2);
|
||||
continue;
|
||||
}
|
||||
|
||||
$jobs = $response->json();
|
||||
|
||||
if (empty($jobs)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->info('Printing jobs...');
|
||||
|
||||
foreach ($jobs as $job) {
|
||||
try {
|
||||
$this->handleJob($job, $yaml);
|
||||
} catch (Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
sleep(2);
|
||||
|
||||
} while (true);
|
||||
}
|
||||
|
||||
private function getConfiguration(): array
|
||||
{
|
||||
$this->info('Reading configuration...');
|
||||
$yaml = file_get_contents(base_path('print-cli.yml'));
|
||||
return Yaml::parse($yaml);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RequestException
|
||||
*/
|
||||
private function handleJob(array $job, mixed $yaml): void
|
||||
{
|
||||
if (!empty($job['data']['preview'])) {
|
||||
$this->info(sprintf('Job %s is a preview', $job['id']));
|
||||
$this->markCompleted($job, $yaml);
|
||||
return;
|
||||
}
|
||||
|
||||
$this->markCompleted($job, $yaml);
|
||||
|
||||
$this->info(sprintf('Job %s completed', $job['id']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RequestException
|
||||
*/
|
||||
private function markCompleted(array $job, mixed $yaml): void
|
||||
{
|
||||
$response = Http::patch(sprintf('%s/api/printers/jobs/%s/complete', $yaml['base_url'], $job['id']));
|
||||
$response->throw();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RequestException
|
||||
*/
|
||||
private function markFailed(array $job, mixed $yaml): void
|
||||
{
|
||||
$response = Http::patch(sprintf('%s/api/printers/jobs/%s/fail', $yaml['base_url'], $job['id']));
|
||||
$response->throw();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user