Schedule Laravel task based on variable value from .env file - php

I've built a Laravel system that schedules an artisan command to run daily at a specific time based off of a variable which is set in the .env file. This is because we need to set differing times for each installation of this syten.
.env
DEBTOR_EMAIL_TIME="12:30"
Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('commandt:make --email=test#test.com')->dailyAt(env('DEBTOR_EMAIL_TIME'));
}
If I hard-code a time into the dailyAt() method, the scheduler will either tell me there a no jobs queued to run or the job runs at the specified time.
When I use a variable, I get no output.
What could I be doing wrong?

Related

Cron Job for laravel web application

So I have this laravel web application installed on a Dreamhost server. At the admin end I was given this line of code at the beginning
Please Set Cron Job Now
To automate the return interest, we need to set the cron job and make sure the cron job is running properly. Set the Cron time as minimum as possible. Once per 5-15 minutes is ideal while once every minute is the best option.
curl -s https://capitalmaxoptions.com/cron
How do I go about this? Does it go into the kernel.php file?
Yes, you can have the job in kernel.php file.
Best option is to make a command for your cron job so you don't mix the specific job with the others. You can check about Making Commands.
Inside your handle function you may have your execution
public function handle()
{
// cURL request or your backend function to your logic
}
Next is to schedule the frequency of the command which (5-15 minutes) in you case. You can check about the Frequency Options.In your kernel.php file, inside the schedule function you can call your function and set the frequency.
protected function schedule(Schedule $schedule)
{
// If your command is cron:interest
$schedule->command('cron:interest')->->everyFifteenMinutes();
}
You can test your command by starting the scheduler manually on localhost. Check the Run Task Scheduler link shared in the comments #Autista_z shared to configure scheduler.

Laravel Task Scheduling not running commands

In my app/Console/Kernel.php, I have set my code as
protected function schedule(Schedule $schedule)
{
$schedule->job(new Refresher);
}
When I run the php artisan schedule:run command, I get the No scheduled commands are ready to run. message. I'm not sure if I am missing out on anything, I have tried adding the ->everyMinute(), but it still does not work.
Your code is creating a queued job. The queue should run on its own. However if you want to start the queue on your own use the command:
php artisan queue:work --stop-when-empty
Queued job, like scheduled tasks, should have a frequency option chained after the job method. Add it to your code:
protected function schedule(Schedule $schedule)
{
$schedule->job(new Refresher)->everyMinute();
}
Note: the Refresher class must respect a specific class structure.You can find it in the Laravel docs https://laravel.com/docs/5.8/queues#class-structure ( I don't know the Laravel version you're working on so i posted the 5.8 docs. Just change the version to the one you're using in the URL)
If you need to create a simple task you can simply change the code to:
$schedule->call(new Refresher)->everyMinute();

Laravel Task Scheduling where stores?

Laravel has Task Scheduling where events, job and commands can be scheduled. I just can't figure out where does it store them? It doesn't look like it stores it in database
Your batch jobs can be stored in a database table, then use the scheduler to execute it.
The function below can be put in app/console/Kernel.php
$schedule->call(function () {
//put your logic here e.g. send verification code to newly
//registered users at 10 pm
})->dailyAt('22:00');
To execute the scheduler, you must specify the name of the command:
protected $commands = [
send:email
];
Then you would call it in terminal as php artisan send:email and keep the terminal process running.
Or you can setup a cron job

Laravel Scheduled Command doesn't run

Using Laravel, I have a cron in Kernel.php that doesn't seem to run when just left to run, if I run php artisan command-name manually, it does work. Below is what I have in Kernel.php, I'm not sure what's stopping it running every 5 minutes, automatically:
$schedule->command('command-name')
->everyFiveMinutes()
->withoutOverlapping()
->runInBackground()
->sendOutputTo(storage_path('logs/command-name.log'));
withoutOverlapping should create a file in storage folder with a name as
schedule_123456....
If the command stopped abruptly this file could remain undeleted. I think you have to delete it manually.
Two things:
withoutOverlapping create a lock for 24 hours that avoids to run a new instance of same, while runInBackground let more than one instance to run concurrently. I'ld use only one of the two options..
you can pass a parameter to withoutOverlapping to let the check to last less than 24 hours

How Laravel knows when the scheduler has been updated?

My question is more of a general wondering. I have two commands created using Laravel, let's call them A and B.
Each one of these commands are scheduled with the ->dailyAt($par) method. But the $par parameter comes from a query.
I mean something like this:
protected function schedule(Schedule $schedule)
{
$schedulerTime_commandA = App\Model\CommandsTime::where('id', 1)->first()->time;
$schedulerTime_commandB = App\Model\CommandsTime::where('id', 2)->first()->time;
$schedule->command('A')
->dailyAt($schedulerTime_commandA);
$schedule->command('B')
->dailyAt($schedulerTime_commandB);
}
This is because the superuser wants to schedule the time when those commands will run. My question here is: How Laravel knows this schedule method within the App\Console\Kernel.php file has been changed?
NOTE: I have the following cron entry as Laravel talks about it on the docs.
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
The way Laravel's scheduler system works is it uses a cron job that runs once every minute (that's the * * * * * part in the cron entry: match every minute of every hour of every day of every month of every year).
So every minute, php /path/to/artisan schedule:run >> /dev/null 2>&1 is being run by cron.
When that command runs, it should check the schedule as defined by the schedule() method in the Kernel class when it is run.
So technically, Laravel doesn't know that the schedule has changed, per se. Every minute it should run, hit the schedule() method, which will grab the latest values from the database, and then return the schedule as it is set at that particular minute in time.
Each individual run of the cron knows nothing about the ones that came before it, or the ones that will come after it.
At least that's my understand of the scheduler. I've only spent a little time in the core Laravel Kernel code, but I believe that to what is going on from my own experience.
Hope that helps!
Edit 1
Just confirmed my logic in Laravel's code. Every time the cron script runs, the schedule is rebuilt, so the changes to the schedule in the database will be used the next time the cron entry runs. Laravel doesn't really know that it changed - it just checks every time it runs and uses what it finds.
Specifically, here's the chain through code if it's of interest:
Calling php artisan on the command line will run the artisan file in the project root;
In the artisan file the Application is bootstrapped and a Kernel object is initialized;
When the Kernel class is initialized, the constructor for the Kernel class (Laravel\Lumen\Console\Kernel) calls the defineConsoleSchedule() method on itself;
The defineConsoleSchedule() method initializes a blank Schedule object (Illuminate\Console\Scheduling\Schedule);
The blank Schedule object will be passed to the schedule() method on the Kernel class; and finally
In the schedule() method, which is where you defined your command schedule, your two DB queries will be run, and your two schedule entries will be defined with the values that the DB returns at that moment.
The above actions happen every time the console application bootstraps, which means on every call to php artisan, regardless of what command you wish to run.
When running the schedule:run command specifically, here's what happens next:
After the application bootstraps, the ScheduleRunCommand object (Illuminate\Console\Scheduling\ScheduleRuneCommand) is initialized; and
The fire() method is called on the ScheduleRunCommand object, which runs through all the commands defined on the Schedule object when the application bootstrapped; and
Checks each command to see if that command must be run at that time by calling the isDue() method on the Event (Illuminate\Console\Scheduling\Event) that represents the command; and finally
Each command that needs to be run at that minute, by returning true from isDue() will be run.

Categories