Laravel Scheduler with Cron Job - php

I'm using larave 5.1 with php5, i try to create my cron job to remove unpaid invoice in time i want, but i testing it to print an userlog to help me know that the job is working.
This is my app/Console/Kernel.php
protected $commands = [
\App\Console\Commands\Inspire::class,
\App\Console\Commands\RemoveUnpaidInvoice::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('removeUnpaidInvoice')->everyMinute();
// $schedule->command('inspire')->hourly();
}
And this is my RemoveUnpaidInvoice class :
public function handle()
{
UserLog::create([
'user_id' => '3',
'title' => 'Cron Testing',
'log' => 'Time : ' . date('H:i:s')
]);
}
After my files done, i run this command at my terminal to run my cron:
php artisan schedule:run
After i run schedule artisan command, then my terminal show this message :
Running scheduled command: '/usr/bin/php5' 'artisan' removeUnpaidInvoice > '/dev/null' 2>&1 &
I think it's work, then i check my database to look the userlog is created or not, and it's created, the user log is added new via cron.
But the problem is, i wait for one minute, and no userlog added, wait for 2 minute, 3 minute and more, there's no another userlog added to my database?
How to fix it? am i made a mistake??

Starting sheduler
Here is the only Cron entry you need to add to your server:
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. Then, >Laravel evaluates your scheduled tasks and runs the tasks that are due.
You need start the cron, no run php artisan schedule:run in the console.

Related

do i have to do something to have the schedule run automatically every minute?

I created this function but it only works once when I execute this command:
php artisan schedule: run
Should I have to do something to continue automatic? is it then triggered automatically when you enter the site only once and run it alone?
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$dias1 = \Carbon\Carbon::today()->subDays(5);
$dias2 = \Carbon\Carbon::today()->subDays(15);
$encomendasnaopagas = encomendas::where('estado', 1)->where('updated_at', '<', $dias1)
->join('distritos', 'encomendas.distrito', '=', 'distritos.id')
->update(['estado' => 5]);
$encomendasenviadas = encomendas::where('estado', 3)->where('updated_at', '<', $dias2)
->join('distritos', 'encomendas.distrito', '=', 'distritos.id')
->update(['estado' => 4]);
})->everyMinute();
}
This may be something you have missed in the documentation on keeping the scheduler running
https://laravel.com/docs/8.x/scheduling#defining-schedules
When using the scheduler, you only need to add the following Cron entry to your server. If you do not know how to add Cron entries to your server, consider using a service such as Laravel Forge which can manage the Cron entries for you:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. When the schedule:run command is executed, Laravel will evaluate your scheduled tasks and runs the tasks that are due.
You will need to setup an entry in your crontab or similar cron management system.

Laravel queue listener times out

On my Linux server I have the following cron:
* * * * * php /var/www/core/v1/general-api/artisan schedule:run >> /dev/null 2>&1
The CRON works correctly. I have a scheduled command defined in my Kernel.php as such:
protected function schedule(Schedule $schedule)
{
$schedule->command('pickup:save')
->dailyAt('01:00');
$schedule->command('queue:restart')->hourly();
}
The scheduled task at 1AM runs my custom command php artisan pickup:save. The only thing this command does is dispatch a Job I have defined:
public function handle()
{
$job = (new SaveDailyPropertyPickup());
dispatch($job);
}
So this job is dispatched and since I am using the database driver for my Queues, a new row is inserted into the jobs table.
Everything works perfectly up to here.
Since I need a queue listener to process the queue and since this queue listener has to run basically forever, I start the queue listener like this:
nohup php artisan queue:listen --tries=3 &
This will write all the logs from nohup to a file called nohup.out in my /home directory
What happens is this: The first time, queue is processed and the code defined in the handle function of my SaveDailyPropertyPickup job is executed.
AFTER it is executed once, my queue listener just exits. When I check the logs nohup.out, I can see the following error:
In Process.php line 1335:
The process "'/usr/bin/php7.1' 'artisan' queue:work '' --once --queue='default'
--delay=0 --memory=128 --sleep=3 --tries=3" exceeded the timeout of 60 seconds.
I checked this answer and it says to specify timeout as 0 when I start the queue listener but there are also answers not recommending this approach. I haven't tried it so I dont know if it will work in my situation.
Any recommendations for my current situation?
The Laravel version is 5.4
Thanks
Call it with timeout parameter, figure out how long your job takes and scale from there.
nohup php artisan queue:listen --tries=3 --timeout=600
In your config you need to update retry after, it has to be larger than timeout, to avoid the same job running at the same time. Assuming you use beanstalkd.
'beanstalkd' => [
...
'retry_after' => 630,
...
],
In more professional settings, i often end up doing a queue for short running jobs and one for long running operations.

Laravel schedule fire only once on initialization

When i use php artisan schedule:run command the shuffleQuoteOfTheDay method is evaluated once and then it does not fire it every minute.
Kernel#schedule method
protected function schedule(Schedule $schedule)
{
$schedule->call('App\Services\MotivationalQuoteService#shuffleQuoteOfTheDay')->everyMinute();
}
MotivationalQuoteService#shuffleQuoteOfTheDay method
public function shuffleQuoteOfTheDay(){
$currentQuoteOfADay = $this->getQuoteOfTheDay();
$randomQuote = MotivationalQuote::where('quote_of_the_day',false)->inRandomOrder()->first();
$currentQuoteOfADay->update(['quote_of_the_day' => false]);
$randomQuote->update(['quote_of_the_day' => true]);
return $randomQuote;
}
It does work once but not every minute, i have created jobs table.Can someone help me with this?Thank you in advance.
See Task Scheduling:
Here is the only Cron entry you need to add to your server:
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. Then, Laravel evaluates your scheduled tasks and runs the tasks that are due.
Laravel's task scheduler does not stay in memory, it needs to be run every minute. It will then check which tasks need to be run in that minute and run them. When you run the task scheduler using PHP it just runs once, it needs cron to run it every minute.
If your dev machine is a linux machine/homestead then you could test this lcoally.
To add a cron job:
$ crontab -e
then add * * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1 to the crontab file and save it.
Do not forget to start/reload cron service.
On ubuntu:
$ sudo service cron reload
You need to setup cron so it could run the php artisan schedule:run command every minute.
From the docs:
When using the scheduler, you only need to add the following Cron entry to your server. If you do not know how to add Cron entries to your server, consider using a service such as Laravel Forge which can manage the Cron entries for you:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
This Cron will call the Laravel command scheduler every minute. When the schedule:run command is executed, Laravel will evaluate your scheduled tasks and runs the tasks that are due.

Laravel 5.2 - cron job not working on ubuntu

I am setting cron job first time in Laravel 5.2. As per their docs, I have modified my app/Console/Kernel.php as -
protected $commands = [
\App\Console\Commands\Inspire::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->call(function() {
$myfile = fopen("testfile.txt", "w");
}
)->everyMinute();
}
And start scheduler -
* * * * * php /var/www/html/{project_dir}/artisan schedule:run 1>> /dev/null 2>&1
I want to run this cron every minute. But it is totally not working.
If I tried with php artisan schedule:run cron runs and "testfile.txt" generated in root folder. But why it is not calling automatically every minute? I don't know what's going wrong here.
Any help is appreciated.
Thanks.

Laravel 5.3 Schedule Not working ( No scheduled commands are ready to run. )

The below is the schedule function
protected function schedule(Schedule $schedule)
{
$schedule->command('queue:work')
->everyMinute()
->withoutOverlapping();
}
Below is the cron for laravel
* * * * * /usr/local/bin/php /home/space/public_html/project/artisan schedule:run >> /home/space/public_html/project/public/op.txt 2>&1
But each time the cron outputs
No scheduled commands are ready to run.
queue:work is not getting executed, what am I doing wrong?
Deleting all the schedule files in storage/framework did this job.
The problem was the command was executed but was some error so never worked but the command was active preventing it to run again by the cron (because i used withoutOverlapping() ).

Categories