Laravel Scheduling call controller

2019-04-23 20:06发布

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.

4条回答
Root(大扎)
2楼-- · 2019-04-23 20:26

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 ;)

查看更多
时光不老,我们不散
3楼-- · 2019-04-23 20:31

Also now I test with more simple function to insert new currency in database. Here is code:

public function handle()
    {
        $currency                = new Currency();
        $currency->currency_name = 'EUR';
        $currency->save();

        $this->info('New currency is successfully generated!');
    }

This function is from laravel/app/Console/Commands. I call from Kernel > schedule(). Not work.

When I insert simple log write to handle() function like:

File::put(base_path() . '/storage/logs/test_logs.txt', 'Test log ' . Carbon::now()->format('Y-m-d H:i:s') . PHP_EOL);

this works. But when I try to create new object from Models and save into db -> this not work.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-04-23 20:39

OK. I solved this problem. The problem was with Docker Cron container. Cron container was not linked with MySQL container. Thanks for all answers.

查看更多
甜甜的少女心
5楼-- · 2019-04-23 20:41

For me, the first error looks like you need to run composer update, then composer dump-autoload.

If it works you will also get the second error, the 2002 error meaning is:

Can't connect to local MySQL server through socket" (see (Client Error Codes and Messages in MySQL docs).

You need to set your database configuration in your .env file

查看更多
登录 后发表回答