Console
Overview of Laravel Console in ECOM-Api
The console directory in the ECOM-Api project is essential for managing Artisan commands and scheduling tasks. This document provides a detailed overview of its contents, including the Kernel.php file and custom command classes.
Table of Contents
Kernel.php
Purpose:
The Kernel.php file manages Artisan commands and schedules tasks within the Laravel application. It defines which commands are available and when they should be executed.
Key Components:
- Command Registration
$commands- Description: Registers custom command classes.
- Example:
protected $commands = [ \App\Console\Commands\ClearExpiredPriceCache::class, ];
- Task Scheduling
schedule(Schedule $schedule)- Description: Defines tasks to be executed at specified intervals.
- Example:
protected function schedule(Schedule $schedule) { $schedule->command('horizon:snapshot')->everyFiveMinutes(); $schedule->command(ClearExpiredPriceCache::class)->everyMinute(); $schedule->command(HealthCheckCommand::class)->everyMinute(); }
- Command Loading
commands()- Description: Loads command files from the
Commandsdirectory and includes additional routes for console commands. - Example:
protected function commands() { $this->load(__DIR__.'/Commands'); require base_path('routes/console.php'); }
- Description: Loads command files from the
Commands Directory
The Commands directory contains custom Artisan commands designed for specific functionalities related to server management with the Octane framework.
OctaneSwoole.php
Purpose:
Defines a custom command for managing the Swoole server within the Laravel Octane framework.
Key Methods:
label($string, $verbosity, $level, $background, $foreground)- Description: Outputs formatted labels to the console.
- Example:
public function label($string, $verbosity, $level, $background, $foreground) { if (! empty($string)) { $this->output->writeln([$string]); } }
requestInfo($request, $verbosity = null)- Description: Provides detailed information about HTTP requests processed by the Swoole server.
- Example:
public function requestInfo($request, $verbosity = null) { $url = parse_url($request['url'], PHP_URL_PATH) ?: '/'; $duration = number_format(round($request['duration'], 2), 2, '.', ''); $memory = isset($request['memory']) ? (number_format($request['memory'] / 1024 / 1204, 2, '.', '').' mb ') : ''; ['method' => $method, 'statusCode' => $statusCode] = $request; $this->output->writeln(json_encode([ 'url' => $url, 'method' => $method, 'duration' => $duration, 'memory' => $memory, 'status_code' => $statusCode, ])); }
defaultServerOptions(SwooleExtension $extension)- Description: Configures default server options for Swoole.
- Example:
protected function defaultServerOptions(SwooleExtension $extension) { $defaultOptions = parent::defaultServerOptions($extension); $defaultOptions['log_file'] = '/dev/stdout'; return $defaultOptions; }
Example Usage:
To start the Swoole server, use the following command:
php artisan octane:swoole
OctaneRoadRunner.php
Purpose:
Defines a custom command for managing the RoadRunner server within the Laravel Octane framework.
Key Methods:
label($string, $verbosity, $level, $background, $foreground)- Description: Outputs formatted labels to the console.
- Example:
public function label($string, $verbosity, $level, $background, $foreground) { if (! empty($string) && ! Str::startsWith($string, $this->ignoreMessages)) { $this->output->writeln([$string]); } }
requestInfo($request, $verbosity = null)- Description: Provides detailed information about HTTP requests processed by the RoadRunner server.
- Example:
public function requestInfo($request, $verbosity = null) { $url = parse_url($request['url'], PHP_URL_PATH) ?: '/'; $duration = number_format(round($request['duration'], 2), 2, '.', ''); $memory = isset($request['memory']) ? (number_format($request['memory'] / 1024 / 1204, 2, '.', '').' mb ') : ''; ['method' => $method, 'statusCode' => $statusCode] = $request; $this->output->writeln(json_encode([ 'url' => $url, 'method' => $method, 'duration' => $duration, 'memory' => $memory, 'status_code' => $statusCode, ])); }
handle(ServerProcessInspector $inspector, ServerStateFile $serverStateFile)- Description: Manages the RoadRunner server process, ensuring it is installed, configured, and running.
- Example:
public function handle(ServerProcessInspector $inspector, ServerStateFile $serverStateFile) { if (! $this->isRoadRunnerInstalled()) { $this->error('RoadRunner not installed. Please execute the octane:install Artisan command.'); return 1; } $roadRunnerBinary = $this->ensureRoadRunnerBinaryIsInstalled(); if ($inspector->serverIsRunning()) { $this->error('RoadRunner server is already running.'); return 1; } $this->ensureRoadRunnerBinaryMeetsRequirements($roadRunnerBinary); $this->writeServerStateFile($serverStateFile); $this->forgetEnvironmentVariables(); $extraOpts = []; foreach (config('octane.roadrunner.extra_opts', []) as $extraOpt) { $extraOpts[] = '-o'; $extraOpts[] = $extraOpt; } $server = tap(new Process(array_filter([ $roadRunnerBinary, '-c', $this->configPath(), '-o', 'version=2.7', '-o', 'http.address='.$this->option('host').':'.$this->option('port'), '-o', 'server.command='.(new PhpExecutableFinder())->find().' '.base_path('vendor/bin/roadrunner-worker'), '-o', 'http.pool.num_workers='.$this->workerCount(), '-o', 'http.pool.max_jobs='.$this->option('max-requests'), '-o', 'rpc.listen=tcp://'.$this->option('host').':'.$this->rpcPort(), '-o', 'http.pool.supervisor.exec_ttl='.$this->maxExecutionTime(), '-o', 'http.static.dir='.base_path('public'), '-o', 'http.middleware='.config('octane.roadrunner.http_middleware', 'static'), '-o', 'logs.mode=production', '-o', 'logs.level='.($this->option('log-level') ?: (app()->environment('local') ? 'debug' : 'warn')), '-o', 'logs.output=stdout', '-o', 'logs.encoding=json', ...$extraOpts, 'serve', ]), base_path(), [ 'APP_ENV' => app()->environment(), 'APP_BASE_PATH' => base_path(), 'LARAVEL_OCTANE' => 1, ]))->start(); $serverStateFile->writeProcessId($server->getPid()); return $this->runServer($server, $inspector, 'roadrunner'); }
Example Usage:
To start the RoadRunner server, use the following command:
php artisan octane:roadrunner