15

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.

2
  • What QUEUE_DRIVER are u using inside your .env file? Commented Mar 17, 2016 at 15:14
  • In my .env file -> QUEUE_DRIVER=sync Commented Mar 18, 2016 at 13:01

5 Answers 5

41

I use Laravel 5.2 and the kernel calls my drivers this way:

$schedule->call('App\Http\Controllers\MyController@MyAction')->everyMinute();
Sign up to request clarification or add additional context in comments.

2 Comments

@BinitGhetiya How did you add parameter option?
although this answer solve the OP problem but i really think that the cron should call a service function not a controller one, i would put the logic you have in that controller function inside a service and i will call it from the controller and the cron
3

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 

Comments

1

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

2 Comments

Because controller is wrong way to use in scheduling task, I created artisan Command with same logic from my Controller. But now I don't gave any error message but don't work this functionality. Also I create route for this controller to test this code and code work perfectly, but there not.
My .env file is OK because I successfully inserted record in database from controller but from scheduling not working
1

The problem was with Docker Cron container. Cron container was not linked with MySQL container.

Comments

0

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.

2 Comments

I test this code row by row. First 2 row where I create new Currency object and add property currency_name is OK. But the error is on row 3, where I call save method. $currency->save()
This is not an answer, so, for proper use of stackoverflow you should edit your question and put any new information there, on the other hand your are adding a lot of blurry informatión, what are you trying to do? an artisan command? calling a controller? running a task? please! stick with one so we can work from something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.