I have function in controller for generating pool(game) each day after one expires.But it invokes only when I calling that function.I need to generate pool automatically.That means generate pool without calling the function manually.
-
1Take a look at [task scheduling](https://laravel.com/docs/6.x/scheduling). – Jerodev Nov 12 '19 at 08:46
-
How can I do it?? Will you please explain that... @Jerodev – Arjun S Nov 12 '19 at 08:48
-
Start by going through the documentation, it explains how you can schedule tasks perfectly. :) – Jerodev Nov 12 '19 at 08:51
-
I am a beginner in laravel. Scheduling is not worked in my project. @Jerodev – Arjun S Nov 12 '19 at 09:18
-
Is command generate necessary for task scheduling??@Jerodev – Arjun S Nov 12 '19 at 09:27
-
Thank you Jerodev for the advice to use task scheduling. It changes my thinking. @Jerodev – Arjun S Nov 12 '19 at 12:50
1 Answers
Welcome to SO! it looks like you're trying to create a Scheduled task, you can find the documentation here: https://laravel.com/docs/6.x/scheduling
to quickly explain the basics of implementing a scheduled task, you'll want to create a cronjob that runs every second depending on your Os, the way you go around this might differ, in windows, use the Task Scheduler, on Unix based systems, you need to define one in a different way, this might help: https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
this cronjon will run the php artisan schedule:run
command, which will in turn execute the Laravel Task Schedule.
now you want to define your schedule, In the class App\Console\Kernel there will be a method called schedule(Schedule $schedule)
, here you will add a new schedule using the call method to start your new pool.
I imagine it would look something like this:
protected function schedule(Schedule $schedule) {
// Telling laravel what the schedule has to do
$schedule->call(function () {
Pool::startNew(); // Dummy function, this would be the method that starts your game
})->daily(); // Runs the task at Midnight every day
}
you can ofcourse run as many scheduled tasks as you want, and intergrate them with other laravel components such as events.
I hope this helped you get started with the daily scheduling.
quick edit: if you want to use a controller method, check out this answer: https://stackoverflow.com/a/37278604/4359569

- 877
- 9
- 29
-
The function for generate pool is located in my controller. Can I call controller function inside this schedule?? @Grey – Arjun S Nov 12 '19 at 11:42
-
yes you can! check out this answer: https://stackoverflow.com/a/37278604/4359569 @ArjunS – Grey Nov 12 '19 at 12:10
-
How can I check that scheduler is working or not in my local server @Gery – Arjun S Nov 12 '19 at 12:11
-
@ArjunS you can run the `php artisan schedule:run` command during in the console yourself, it will tell you what jobs complete and will throw an error if something goes wrong (unless you have custom error handling). another way (something you should do anyway) is to add a log message to your pool code that will tell you when a job has started and finished – Grey Nov 12 '19 at 12:14
-
I will also run php artisan schedule:run . It returns a result in my console as " Running scheduled command: App\Http\Controllers\CronController@createpool " . But I can't get my expected result. @Grey – Arjun S Nov 12 '19 at 12:17
-
that's about as much information that will give you, if you want to know more, use the Log::info() method to log into the log files – Grey Nov 12 '19 at 12:20
-
I'm in a big trouble about this task scheduling. Where is the log file located?? @Grey – Arjun S Nov 12 '19 at 12:21
-
@ArjunS the log file is located in [projectRoot]/storage/logs and the file is called laravel.log – Grey Nov 12 '19 at 12:25
-
It works. Thanks you Grey for your valuable answer. It helps me a lot.... @Grey – Arjun S Nov 12 '19 at 12:49
-
@ArjunS no problem, if it worked, could you upvote and accept my answer please? – Grey Nov 12 '19 at 12:57
-