Cron Job for laravel web application - php

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.

Related

Schedule Laravel task based on variable value from .env file

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?

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

Fetching near-realtime data from external API

I'm looking for a sustainable solution to fetch data every x seconds (let's say 20) and store it in a relational database using PHP. After doing some research I found a few options:
1) Cronjobs (with shell scripts)
See https://askubuntu.com/questions/800/how-to-run-scripts-every-5-seconds for more information. This basically comes down to run a shell script (looping/sleeping)
This doesn't feel right as I could not catch exceptions and/or race conditions might occur. Also, cronjobs itself are not made for this kind of tasks.
2) Web-worker (with queued jobs)
Laravel provides a queue worker that can process new jobs (asynchronously) as they are pushed onto the queue. I could push multiple (say a lot) of jobs to the queue at once which should processed every x seconds consecutively.
This sounds like a more robust solution as I could catch exceptions and make sure the worker is running (using observers). The downside; it's slower and it might be overengineered.
3) Web socket
I could use node.js to run a websocket client like socket.io and implement some kind of timing mechanism to store the data every x seconds.
This solution feels odd as I was taught that sockets are used to push data to clients (realtime), but I have never seen that they were used to insert data.
All help is appreciated.
What you are looking for are artisan commands.
You would start by creating a command:
php artisan make:command FetchData
This creates a FetchData class. In this class you can edit the handle function.
public function handle()
{
//fetch your data here
}
You also need to edit the $signature variable.
protected $signature = 'fetch:data';
The next step is to register the command in the Kernel.php in the Console namespace.
You need to add your newly created FetchData class to the $commands array.
protected $commands = [
Commands\FetchData::class
];
You could now call this command from the console like php artisan fetch:data
After you registered your command in the Kernel.php you can schedule this command.
You start by adding following line to your crontab on your server (type crontab -e)
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
You can now add following command to the schedule function in the Kernel.php:
$schedule->command('fetch:data')->everyThirtyMinutes();
There is no option for a job to run every twenty minutes so in this example I chose thirty minutes. You can check the available options here.

Laravel Schedule Call Helper Function Not Working

I'm trying to run a Helper function (App\Helpers) on a scheduled timer using Laravel (for testing purposes I have it running once every minute). I'm using Laravel 5.3.
This is my schedule function in my Kernel.php...
protected function schedule(Schedule $schedule)
{
$schedule->call(function()
{
// Calling this function should write a new file with a random number in it.
// I know this works perfectly fine outside of the scheduled task because I
// call it in other places, and it works)
FileEdit::UpdateFile();
})->everyMinute();
}
The issue is that the FileEdit::UpdateFile() part is NOT ever being called by the laravel at the designated time intervals.
Are you running a cron job to execute the schedule command every minute?
https://laravel.com/docs/5.5/scheduling#introduction
* * * * * 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.
Your code looks fine, assuming you have cron configured correctly, this may help you debug your issue https://laravel.com/docs/5.5/scheduling#task-hooks
Still I would double check if your cron is working, and set according to https://laravel.com/docs/5.5/scheduling#introduction

How to setup cron script in moodle through plugin

I want to run my cron script at sheduled intervels created at blocks/plugin_name/cron.php . Please help me how to create and call this cron script to run at regular intervels.
Cron.php is the old way, you should create a function plugin_name_cron() in blocks/plugin_name/lib.php
Then in version.php you have a cron parameter which will tell Moodle to run the plugin every x seconds.
$plugin->cron = 0; // Seconds.
So every 15 minutes is
$plugin->cron = 15*60; // Seconds.
0 seconds means never run the cron.
This does depend on the admin cron being run too. If the admin cron is run ever 60 minutes then your plugin cron will only be run every 60 minutes.
For testing you can run the admin cron manually from http://yoursite.com/admin/cron.php
If you want to schedule the cron to run at a certain time, then you will need to add some code to your cron function to work out the scheduled time.
EDIT:
Actually the cron function is slightly different for blocks. Create a function cron() in your class class block_plugin_name extends block_base..
UPDATE:
From Moodle 2.7+, the above has been replaced with scheduled tasks https://docs.moodle.org/dev/Task_API#Scheduled_task_usage

Categories