I need to run a scheduler task from blade view by clicking a button (Sync) and it should go for process in the background.
I have created an artisan command that is php artisan projects:get and then I schedule it once a day for running in cron job, but in some case we need to run the cron job at user’s choice so when he/she is in CMS logged in, they can click a sync button to run the cron job from there, but I think its not possible, but I knew there is some work around in Laravel to process the php artisan command which I already created that is projects:get using queues or process from symphony, but I know I can do it from the command line (terminal) using putty or cPanel terminal window, but As you know client can’t login into cPanel and run the command so we need to give them just a simple button to click and sync in background, right now when user click that button, its getting delay and he/she can’t continue to work on other things while its fetching all the projects from the API’s that I used in that command. We need to queue/background process.
php artisan projects:get
2
Answers
You can run artisan command programmtically like this:
Route
SomeController
As you pointed out, you can run artisan commands from your php code as documented in the documentation
Since the artisan command will likely take some time to execute, it is a good practice to use a queue for this.
You said in the comments, that you are on xampp. On local, you need to run the
pap artisan queue:work
command once you started xampp. After you executed the command, the watcher will pick up jobs and will execute them. However, first you need to configure the queue. This will get you up and running. On a production server, you need to configure a supervisor to run the queue command.