I want to run artisan commands simultaneously in parallel, here is my code:
$files = glob($this->fixedDir . '*.csv');
foreach ($files as $csvFile) {
Artisan::call('mycommand:import', ['--file' =>$csvFile]);
}
So idea is to pass file option and then each commands handles that. However above code runs each command sequentially not in parallel. I also tried like this:
$schedule = new Schedule();
$files = glob($this->fixedDir . '*.csv');
foreach ($files as $csvFile) {
$schedule->command('mycommand:import', ['--file' =>$csvFile])
->everyMinute()
->withoutOverlapping();
}
Even that didn't seem to work.
Can somebody tell how do I run commands in parallel ?
You may want to use Jobs and Queues for this.
php artisan make:job ImportCsv
Then edit your job class to process your csv file import. Then dispatch this job from a controller like this:
<?php
namespace App\Http\Controllers;
use App\Jobs\ImportCsv;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class FileController extends Controller
{
/**
* Store a new file.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
// Create file...
dispatch(new ImportCsv($file));
}
}
Make sure your worker process is running. You can start it like this:
php artisan queue:work
You can learn details from documentation; Laravel Queues
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With