Laravel scheduler not working when called from controller - laravel

I have created a console command that (for now) just outputs a log statement.
That command is scheduled every minute.
I have a controller endpoint (located at /scheduler/run) that performs an Artisan::call(); to the artisan schedule:run command.
Locally it works perfectly (laravel valet & nginx), on production (apache) however, nothing happens when hitting that endpoint...
I do get the reply from my controller, but the scheduler is never triggered.
My route:
Route::get('/scheduler/run', SchedulerController::class)->name('scheduler');
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Artisan;
class SchedulerController extends Controller
{
public function __invoke()
{
Artisan::call('schedule:run');
return response()->noContent();
}
}
My Kernel.php
<?php
namespace App\Console;
use App\Console\Commands\PingCommand;
use App\Console\Commands\RefreshTwikeyKey;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
PingCommand::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command(PingCommand::class)->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}

Related

Can't run a task schedule in Laravel 8 using cron job in BlueHost

I am running my web app under Bluehost shared hosting, and I am trying to run a cron job which will call my schedule task in my laravel app. For some reason the scheduler is not being called. These are the commands in Bluehost that I have tried so far:
None of these above commands seems to execute my task scheduler, I have tried calling the command from using artisan command, it works... What do you think this is not working?
Note : This is perfectly working on local server.
/home3/urbanhq6/public_html/new/app/Console/Kernel.php
<?php
namespace App\Console;
use App\Console\Commands\SendReminderEmails;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Console\Commands\SendReminderEmails;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
App\Console\Commands\SendReminderEmails::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('update:record')
->everyMinute();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
/home3/urbanhq6/public_html/new/app/Console/Commands/SendReminderEmails.php
<?php
namespace App\Console\Commands;
use App\Booking;
use Illuminate\Console\Command;
use App\User;
use App\Mail\ReminderEmailDigest;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
use DB;
class SendReminderEmails extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'update:record';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send email notification to user about reminders.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
DB::table('bookings')->update(["check_out" => date("Y-m-d")]);
}
Cron Job Command
/usr/local/bin/php /home3/urbanhq6/public_html/new/artisan schedule:run >> /dev/null 2>&1

Laravel automated invoice reminder email on due date, after 3days, after a week from different users to different clients

I am implementing a invoice API system for that I have to implement an automated reminder email on due date, after 3days, after 7days (it is like a API so the email has to be send from login user email to the selected customer)
For this system so far I implemented a checkbox selection which will store the login user, selected customer, due date, day selection for reminder stored the data in database). I have issues in sending emails from different users, adding condition to send email from the due date
console->commands->SendReminderemail.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
Use App\Reminder;
use App\Donor;
use App\Mail\ReminderEmailDigest;
Use Mail;
class SendReminderEmails extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'reminder:emails';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Invoice Reminder Emails';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
//
$pending_Reminders = Reminder::where('status',0)->get();
$data = [];
foreach ($pending_Reminders as $reminder){
$data[$reminder->donor_id][] = $reminder->toArray();
}
foreach($data as $donor_id => $pending_Reminders){
$this->sendEmailToUser($donor_id, $pending_Reminders);
}
// dd($data);
}
private function sendEmailToUser($donor_id, $pending_Reminders){
$user = Donor::find($donor_id);
Mail::to($user)->send(new ReminderEmailDigest($pending_Reminders));
// dd($user);
}
}
App->mail->ReminderEmailDigest
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class ReminderEmailDigest extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
private $pending_Reminders;
public function __construct($pending_Reminders)
{
$this->reminders = $pending_Reminders;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('emails.reminder-digest')
->with('pending_Reminders', $this->reminder);
}
}
App->console->kernal
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Console\Commands\SendReminderEmails;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
SendReminderEmails::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('reminder:emails')->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Now only the email working when I enter the reminder:emails command in console
How can i automate the mail?? any related suggestions??
You need to schedule your command.
For that, check how to register your command to schedular and how to set up cron job for schedular to run.
More specifically, you can do something like this:
$schedule->command('reminder:emails')->daily();
Just a note on how you could clean up your code.
$pending_Reminders = Reminder::where('status',0)
->get()
->groupBy('donor_id')
->each(function($reminders, $donor_id) {
$user = Donor::find($donor_id);
Mail::to($user)->send(new ReminderEmailDigest($reminders->toArray()));
});
To set the artisan command to run daily,
app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('reminder:emails')->daily();
}
To get that to run sudo crontab -e
* * * * * cd path/to/project && /usr/local/bin/php artisan schedule:run >> /dev/null 2>&1

How to execute a task immediately after adding to Schedule Laravel?

I have next code:
foreach ($tasks as $task) {
$schedule->command(TaskCommand::class, [$task->id])->hourly();
}
I add this tasks dynamically.
How to execute a task immediately after adding to schedule and after that to execute the task every hour?
Foreach should be done in scheduler, look at the code
In app/console/kernel
namespace App\Console;
use DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
foreach ($tasks as $task) {
$schedule->command(TaskCommand::class, [$task->id]);
}
})->hourly();
}
}
Then you need to switch it on the server by command:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
More: https://laravel.com/docs/5.6/scheduling
Good luck!

How to clear all Log files data using Monolog in Laravel

How to empty log files data before adding new contents to it.
Using Monolog for saving all logs inside
storage/app/logs/*
You can use rm command through ssh to remove all logs:
rm storage/logs/laravel-*.log. Use * as wildcard to remove all logs if they have suffixes.
Or you can add custom code within Controller method only for admins of app:
$files = glob('storage/logs/laravel*.log');
foreach($files as $file){
if(file_exists($file)){
unlink($file);
}
}
Or create a console command. Depending on version, below 5.3 use for example:
php artisan make:console logsClear --command=logs:clear
For versions 5.3 and above
php artisan make:command logsClear
add signature within command class if it doesn't exist.
protected $signature = 'logs:clear';
Add your class in Console/Kernel.php, in protected $commands array ( note that your code varies upon customization for your app):
<?php
namespace App\Console;
use App\Console\Commands\logsClear;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Utils\ShareHelper;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
logsClear::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
}
}
You should add then custom code in handle() of logsClear class
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class logsClear extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'logs:clear';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
//
$files = glob('storage/logs/laravel*.log');
foreach($files as $file){
if(file_exists($file)){
unlink($file);
}
}
}
}
Then run php artisan logs:clear command

Laravel 5 Command and Handler issue

I am working one of my project with laravel 5. During the implementation i got struct with one issue which is related to command and handler.
I used artisan command to generate command
php artisan make:command TestCommand --handler
I generated command at app/commands folder "TestCommand.php"
<?php
namespace App\Commands;
use App\Commands\Command;
class TestCommand extends Command
{
public $id;
public $name;
public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}
}
Also my TestCommandHandler.php looks like this
<?php
namespace App\Handlers\Commands;
use App\Commands\TestCommand;
use Illuminate\Queue\InteractsWithQueue;
class TestCommandHandler
{
/**
* Create the command handler.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the command.
*
* #param TestCommand $command
* #return void
*/
public function handle(TestCommand $command)
{
dd($command);
}
}
Whenever dispatch this command from controller it shows following issue
InvalidArgumentException in Dispatcher.php line 335:
No handler registered for command [App\Commands\TestCommand]
Please, Anybody help me to solve this problem. Thank you
By default Laravel 5.1.x does not included BusServiceProvider. So we should create BusServiceProvider.php under provider folder and include that in to config/app.php.
BusServiceProvider.php
<?php namespace App\Providers;
use Illuminate\Bus\Dispatcher;
use Illuminate\Support\ServiceProvider;
class BusServiceProvider extends ServiceProvider {
/**
* Bootstrap any application services.
*
* #param \Illuminate\Bus\Dispatcher $dispatcher
* #return void
*/
public function boot(Dispatcher $dispatcher)
{
$dispatcher->mapUsing(function($command)
{
return Dispatcher::simpleMapping(
$command, 'App\Commands', 'App\Handlers\Commands'
);
});
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
config/app.php
'providers' => [
App\Providers\BusServiceProvider::class
]
So it may help others. Thank you

Resources