Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

idnmedia's avatar

Run Vapor CLI Command Programmatically

I use task scheduler in Vapor and it's something like this:

// App\Console\Kernel.php
$this->schedule('inspect-all-categories:run')->twiceDaily();
// App\Console\Commands\InspectAllCategories
foreach($catrgories as $category) {
		$category->inspect();
}

That command usually runs about 5 to 10 minutes. It should do something heavy for hundreds of categories. And it getting timeout for sure.

The question is:

Can I split the process into smaller command and programmatically without getting timeout? something like this:

// App\Console\Commands\InspectAllCategories

foreach($catrgories as $category) {
		Artisan::call('inspect-one-category:run --category=' . $category->id');
}

And the sub command something like this?

// App\Console\Commands\InspectOneCategory
Category::find($this->options('category')->inspect();

Is there any way to achieve something similar without timeout and without increasing the vapor timeout option?

Thanks in advance

0 likes
1 reply
LaryAI's avatar
Level 58

Yes, you can split the process into smaller commands and run them programmatically using Laravel's Artisan command. To avoid timeouts, you can use Laravel's queue system to run the commands in the background. Here's an example of how you can modify your code to achieve this:

  1. Create a new command to inspect a single category:
php artisan make:command InspectCategory --command=inspect:category
  1. Modify your InspectAllCategories command to dispatch jobs to the queue instead of running them directly:
// App\Console\Commands\InspectAllCategories

foreach($categories as $category) {
    dispatch(new InspectCategory($category));
}
  1. Modify your InspectCategory command to inspect a single category:
// App\Console\Commands\InspectCategory

public function __construct(Category $category)
{
    $this->category = $category;
}

public function handle()
{
    $this->category->inspect();
}
  1. Run the queue worker to process the jobs:
php artisan queue:work

This will run the jobs in the background and avoid timeouts. You can also configure the queue to use a different driver (e.g. Redis) for better performance.

Please or to participate in this conversation.