Chapter 14 of 15
CLI and cron.
Reuse application services from explicitly registered commands and schedule them with the system cron.
A custom command
A class implements CommandInterface; run(array $args) receives arguments after the command name and returns a process exit code. The container resolves it only when invoked, inside a Command scope.
ImportProductsCommand.php
<?php
declare(strict_types=1);
use Lemonade\Framework\Cli\CommandInterface;
final class ImportProductsCommand implements CommandInterface
{
public function name(): string { return 'products:import'; }
public function description(): string { return 'Import products.'; }
public function run(array $args): int
{
// Run the application operation and return a process exit code.
return 0;
}
}CommandInput, CommandOutput, and CommandContext provide structured input, output, and execution context.
Registration and scheduling
Register a command name, class, and description in Commands.yaml. Run it through vendor/bin/lemonade; use cron or a supervisor for scheduling.
app/Config/Commands.yaml
module: commands
config:
commands:
- name: products:import
class: App\Console\ImportProductsCommand
description: Import products from the configured source.
# cron: */15 * * * * cd /var/www/app && vendor/bin/lemonade products:importLemonade does not include a scheduler. Framework commands cover migrations, queue workers, and sitemap generation, among other operations.