laravel schedule jobs not executed - laravel

I have several jobs in my scheduler:
protected function schedule(Schedule $schedule)
{
$schedule->job(new PingDatabase())->everyFiveMinutes();
$schedule->job(new ImportInstagram())->hourly();
$schedule->job(new ImportBlog())->hourly();
$schedule->job(new SendEmails())->dailyAt('11:00');
}
I just realized that the SendEmails job did not get triggered today.
Could this be because I use the sync queue driver and the jobs take too long?

Related

How can I run scheduler every one minute in Laravel without setting cron job?

I want to run the scheduler in every minute on the server without setting the cron job with ssh .Please let me know Is there any way to do this ?
Thanks
Assumption: you have set cron job to your project directory
Create a command using
php artisan make:command CommandName
add your logic under handle function
public function handle()
{
//add your logic here
}
register your command under Kernel.php under $commands array
protected $commands = [
Commands\Test::class,
];
schedule command under
protected function schedule(Schedule $schedule)
{
$schedule->command('command:name')->everyMinute();
}

How to store records on the end of the day in laravel

For example, If user forgot to approve till the end of the day a message will store to the database at the end of the day. I used the carbon format as below.
if (Carbon::now()->endOfDay()->eq(Carbon::parse($phaseValue->end_date)->endOfDay())) {
// add records here
}
You will have to set up a task scheduler that will check if a specific condition has been met at the end of the day. if not then it will do the insertion in DB or anything according to your business logic. Laravel provides task scheduling, have a look:
https://laravel.com/docs/8.x/scheduling
You could create a cron job that would run every day at the given time. To do that, create a new command:
php artisan make:command YourCommand
Then, your command would look something like this:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class YourCommand extends Command
{
//Name that will be used to call command
protected $signature = 'your:command';
protected $description = 'Command description';
public function __construct()
{
parent::__construct();
}
public function handle()
{
//Here you will handle all the logic. For ex:
$record = App/Example::first();
//Implement condition that will determine if the record will be updated
$record->update();
}
}
After that, inside App/Console/Kernel.php create the schedule for that command:
protected function schedule(Schedule $schedule)
{
//Name that you gave it in the protected $signature = 'your:command';
$schedule->command('your:command')->dailyAt('23:30');
}
This way the command will be run every day at 23:30 and you can update your records any way you want. You can read more about task scheduling & commands on the official docs.

Iterate over serviceproviders in laravel cron

I have inherited this laravel app and still get accustomed to the framework.
At the moment I try to figure out the "best-practise-way" of iterating through "Service Providers" in a cron job. I have set up the schedule, but I know that just creating an object (or object factory) is probably a big no-no.
How can I get access to an array of all the components in this cron job. something with singeltons probably.
The code:
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$some_array = get_all_the_company_components_or_eloquents();
foreach( $some_array as $company )
{
$company->yay("yay");
}
})->daily();
}

Schedule multiple cron jobs in same time in laravel

I have two different cron jobs below.
protected function schedule(Schedule $schedule)
{
$schedule->command('eventnotification:mail');
$schedule->command('workflownotification:mail');
}
How can I run both cron jobs by daily 6:00AM?
You can run schedules daily at a certain time using the dailyAt() function:
protected function schedule(Schedule $schedule)
{
$schedule->command('eventnotification:mail')->dailyAt('6:00');
$schedule->command('workflownotification:mail')->dailyAt('6:00');
}

laravel task scheduler deleting a post

I setup a simple task scheduler in laravel everything works only problem that i am having is that the post is not deleting at the time i set. I want the post to delete after 5 minutes since the post was created at, not sure why my posts are deleting after a minute.I believe i want my task scheduler to check after every minute because each post has a different delete time. here my scheduler:
protected function schedule(Schedule $schedule)
{
$schedule->call(function (){
$post= Post::where('created_at', '<', Carbon::now()->addMinutes(5))->delete();
})->everyMinute();
}
From your question it would look as though you should be subtracting the minutes, not adding them
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
Post::where('created_at', '<', \Carbon\Carbon::now()->subMinutes(5))->delete();
})->everyMinute();
}
If you are just using Carbon::now() make sure you import it with a use statement. If you use it like I have in this example you don't need to import it.
Also since you're not using the $post variable after delete, you do not need to assign it.

Resources