Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Scheduling call controller

I work with Laravel Task Scheduling, but I have a problem when I call some method from my controller.

protected function schedule(Schedule $schedule)
{
    $schedule->call('UserController@deleteInactiveUsers')->everyMinute();
    //$schedule->call('App\Http\Controllers\UserController@deleteInactiveUsers')->everyMinute();
}

When I call with uncommented line i get this error:

[ReflectionException]
Class RecurrenceInvoiceController does not exist

and then I insert fully qualified namespace path and then I get this error:

[PDOException] SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

And

[ErrorException] PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known 

Where is the problem? Which way is correct to call method from Controller from Laravel Task Scheduling.

like image 468
Kikolce Avatar asked Mar 16 '16 14:03

Kikolce


People also ask

How to schedule tasks in Laravel?

In fact, there are a couple of different ways in which Laravel allows you to define schedule tasks: Use the closure/callable. Call the artisan command. Execute the shell command. Moreover, there are plenty of built-in scheduling frequencies you could choose from:

How to run artisan commands from controller in Laravel?

In Laravel Artisan facade that way we can easily run the all artisan command also with argument. So Artisan facade have two method one call () and another one is queue () through we can simply make process in call like seeder and also migration run etc. So Here in bellow example we can learn how can we run artisan commands from controller.

Is it possible to create a curl request in Laravel controller?

Yes, you can write a a shell script that will make a CurlRequest to your controller, and add the shell script to a cron job. or you can use laravel commands and call the controller by a request. why not using the code inside the controller in a command?

What is cron job scheduling in Laravel?

You can learn more about Cron job on Wikipedia. However, Laravel Cron Job Scheduling makes the whole process very easy. Laravel Cron Job is an inbuilt task manager that gives your applications the ability to execute specific commands like sending a slack notification or removing inactive users at a periodic time.


2 Answers

I stumbled months ago with the same problem, until I could fix it. I use laravel 5.2 and the kernel call my drivers this way:

$schedule->call('App\Http\Controllers\MyController@MyAction')->everyMinute();

I hope this will help someone else ;)

like image 51
Jennifer Miranda Beuses Avatar answered Sep 29 '22 05:09

Jennifer Miranda Beuses


Directly put this in schedule function in kernel.php.

$schedule->call('App\Http\Controllers\YourController@function')->everyMinute();

This will run after every minute.

Note:

In localhost, you have to run it manually but on the server, you should try this command in your cronjob this will automatically call your controller function after every minute

php -d register_argc_argv=On /home/iflasity/public_html/foldername /artisan schedule:run > /dev/null 2>&1 

cd /home/iflasity/public_html/foldername && php artisan schedule:run /dev/null 2>&1 
like image 32
Ahmad Dastageer Avatar answered Sep 29 '22 06:09

Ahmad Dastageer