Laravel Advanced - Task Scheduling - CRON Job

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey guys welcome to Laratips. In today's video  we'll be looking at task scheduling in laravel   first of all we'll be looking at what is task  scheduling and how to use it and in the end of   the video we'll be looking at how to set it up for  the real server. I'll show you how to add it in a   shared hosting like this and also if you have a  dedicated server then also i'll show you how to   do that. So without any further ado let's get  started so basically if you want to do a task   at a later point of time but not now then you will  schedule it at a certain point of time so that is   basically a task scheduling and in a server we  can do that by using cron-jobs and in laravel   will handle that cron-job using task scheduling  and laravel makes it so much easier to use it   so first of all let's see how to use it we have  to go into this app console kernel so here i am   currently in this app console kernel file and you  can see here there is a schedule method over here   and whatever we write inside this schedule method  laravel will run it automatically and we have to   do certain work to make it run automatically  and we'll see that and the end of this video   so first of all let's see one thing let's say we  want to run a task every minute so we cannot do it   for every second but we can do it from minutes in  a cron-job that's the limitation of cron-job not   laravel so let's say we want to run something  every minute then we can do it in this way so this   is the basic way i'll just paste it over here and  instead of db table we'll just do info we'll just   log it to our laravel.log file so here i'll say  called every minute and instead of daily we'll say   every minute so here guys it can be  anything not only the logging so it   can be any complicated logic and this code  will run every minute but to test it locally   let's see how we can do this so let me go to the  top here and here run the scheduler locally then   we can just run this php artisan schedule work so  let me run this over here so the schedule worker   has started and you can see here it has already  called this method whatever we have run here and   if i go in the laravel.log you can see here call  every minute and it called it in 18:55 and you   can see here it's 6:55 pm which is 18:55. now  i'll come back just before one minute completes so here guys you can see now this one called every  minute is now again called because it is already   present here and we have written here every minute  now let's say we want to call it every two hours   so we can say every two hours like this and there  are a bunch of helper methods that we'll see   in the documentation so this is the basic thing  that we can do using laravel scheduler or task   scheduler so we can not only schedule the closure  but we can also schedule php artisan commands   some scripts or jobs and you can see it  in the documentation over here like this   i'll show you at the top so here  scheduling an artisan command like this   so you can just schedule it like this so  here we have called here schedule called   but here we'll say schedule a command so  i'll quickly show you that one as well   so i'll say here so let me just stop this and  now let me create a command hello world command   and it's inside this app console commands  hello world command and let me say it hello whatever we write here we can call via php  artisan and now instead of logging here we'll just   log it over here and we'll say here hello  world and now here we'll say command   hello world like this now it will call this hello  world command every minute for that we need to   just run this php artisan schedule work and let  me remove this laravel.log file now one minute   is about to be completed so let's see whenever we  reach 59 over here then it will log this or not   so you can see here guys it has logged the hello  world now whenever it reaches the 7 pm then it   will again log similarly you can log this shell  commands like this after the schedule you can   call this exec and then you can just give  the path of the script that you want to run   and give the timing similarly you can also  schedule a queued job so you can see it instead of   writing this command over here we can just write a  job and we can just pass job object over here and   then it will just run that job now let's see what  are the options that we have so let's go to this   schedule frequency options and here we have every  minute every two minutes every three minutes and   there are lots of options and you can also pass  this raw cron-job command so you can go ahead in   the documentation and look everything that you can  use and i'll be leaving this link just below that   like button and also one thing i want to show  you guys let's say you want to run some code on   a weekly basis but it must be on monday exactly at  13 then you can just change the commands like this   so it will run every monday at 1 pm similarly  you can also set the time zone over here let's   say your application is set to a certain specific  time zone but you want to schedule a command on a   different time zone then you can also set that  and this command what it does is it will run   hourly on the weekdays between 8 am and 5 pm  so between 8 am and 5 pm it will run hourly and   on the other hours it will not run so you can see  that it is so much powerful so let me go to the   top again and there are few very important things  that you need to know whenever you are using task   scheduling so let me show you those now one of  them is preventing task overlaps so let's go here   so what it says is by default schedule tasks will  run even if the previous instance of the task is   still running to prevent this you may use without  overlapping so let me show you this so here we   have run this hello world addition command every  minute now let me go to that command over here let   me delay this by more than one minute so here  i'll say sleep let's say 65 seconds and here   i'll write another info over here and hello world  before and hello world after like this now let me   run the scheduler so if you look here in the time  it is 07:05 and if you look here in the larval log   there is there is nothing over here so here we are  about to reach the 07:06. now you can see we have   reached 07:06 and there is hello world before that  is logged but if you look here we have delayed   more than one minute and after one minute this  hello world after will get logged but if you see   here in the kernel we have we are running this  command every minute so before this hello world   after gets logged this command will get again  executed and the scheduler will not wait for   that command to get completed so if you see here  guys it's already 07:07 and if we go here in the   larval log then you can hear we are again getting  hello world before we are getting this before   hello world after which proves that the scheduler  command doesn't wait for previous commands to be   completed no matter what it will run the previous  one but let's say we don't want this behavior   then what we can do is we can just say here  without overlapping so here if you go here in the   kernel and let's say without overlapping like this  and let me come here and remove this one for now   and if you see the time here it's okay 07:10  already so now you can see here it has been logged   hello world before but now what will happen is it  will wait for this whole command to be completed   and then only it will run the next command now  what happens is even after one minute has gone   then it will not run this command it will  wait for this command to get completed   and only after that it will run this hello world  so let me go here and we are about to hit 07:11 and you will see that it will not log  that hello world before and you can see   here guys it logged the hello world after and  it did not log this hello world before again   it just waited for everything to get completed  now at 19:12 it will again run this command and   you can see here guys it's 19:12 and it ran this  hello world before now let's move to another   important thing that you need to know here in the  documentation it's running tasks on one server   if the scheduler is running on multiple servers  or let's say you have implemented load balancing   in your server and if you have scheduled this  command at 17:00 let's say every day then it   will run on every server and let's say you have  three servers then it will run three times so   to prevent that from happening you can just add  this on one server and then it will run on only   one server and the third most important thing that  you need to know is about background tasks so let   me just show you this so let's say you have three  tasks that are scheduled at the same time but what   happens is one tax will get completed then only  the second tax will be called but if you want all   those stocks to run at the same time then you can  add this run in background so let me show you that   also so here in the in the kernel file so let me  just remove this one from here and let me run it   three times okay and now here in the command  i'll just remove this and i'll just add here   two seconds and you can see here i am running  three commands so this can be different   actions that you want to perform so i am running  here three commands every minute so all of them   will run at the same time so currently i  am here at 07:15 but when i reach 07:16   then first this command will get run and it will  wait for 2 seconds print this one and after that   it will again run this command and wait for two  seconds and run this info and again it will run   this hello world and wait for two seconds one more  time so it is the default behavior so here guys if   you see here i have already reached 07:16 so now  if i come here in the laravel log now you can see   here guys the time stamp over here okay so closely  look at this timestamp so it is it has grown on 2   4 and 6 which means that it ran this and it wait  for two seconds and then ran this and again waited   for two seconds and then ran this but let's say  you want to run all of them at 16:02 at the same   time then you can just add this run in background  so let me go here in the kernel and here i'll say   here run in background like this so i'll come here  and clear this out and if i show you guys here i'm   here in 07:17 now whenever i reach 07:18 then  all those logs will get logged without any delay   so you can see here all of them logged at the  same time so if you want this behavior just add   this run in background and then all the tasks  that are scheduled at the same time will run at   that specific time they will not get delayed  so these are the main things that you need   to know about this task scheduling and also  one more thing that you need to know here is   during the maintenance mode all the scheduler  will not get executed but if you want any schedule   that needs to be executed whenever we are in  the maintenance mode then you can add this even   in maintenance mode method over here and then it  will run on the maintenance mode as well now let's   see how we can add this schedule in our shared  hosting so here in the documentation you can see   here running the scheduler and if you see here  we have to just add this command in a cron-job   and then everything will automatically work so  basically what you need to do is you just need   to give the path of the project over here and  everything will automatically work so let me copy   this one and now let me go to the shared hosting  over here now i'll go to the file manager over   here and you have to just give the path of the  folder wherever your laravel project is located   so currently this project is located inside this  public_html folder and the path will be this   path/public_html so i'll go here in the  cpanel and i'll search for cron-job here   and i'll go here and some of the shared hosting  will have these options and some will not have   these options so what you need to do is just add  this once per minute so here everything will have   asterisks so if you don't have these options then  just write five asterisks and then just write the   command so before i just copied all these commands  i don't need to copy all these commands because   all these five asterisks will be automatically  added to me by this options okay so here i'll just   copy from here and i'll paste it over here and  here i'll write the path of my project so here   i'll say like this and this should match where  you have added your laravel project so this folder   okay so from here starting from here to here and  whenever i do this and i'll add a new cron-job   you can see here it has added the cron-job and now  it will run every minute and it will check in the   this kernel file in our laravel application and  it will see whatever there is inside this schedule   method and it will run those at the specified time  that you have given here okay now we have seen how   to add this scheduler command in the shared  hosting and now i'll show you how you can add   it to the virtual server or a dedicated server  so it is the same way how it will work in our   local linux machine so i am currently in the  ubuntu 18.04 and it will work for any version   of ubuntu servers so just ssh into your server and  run these commands so i'll show you here so let me   just exit this you need to log into your server  and you need to have the sudo privileges then   what you can do is sudo crontab -e just run this  command here like this and provide the password   and you will see all these things so these are  all the comments don't worry about this if you   want to delete this then you can delete them i'll  just go to the bottom of the file and i'll add   whatever i have copied so i have pasted it over  here and now instead of this path to your project   i'll just add the path to my project and if i  go here and add a new terminal and i just do pwd   then it will give me the path of the current  directory so i am currently in the root of my   project and it is giving me this path so i'll  just copy this and i'll go here and i'll just   delete this and i'll paste it over here and i'll  save it now so i have saved it now i don't need to   run that php artisan schedule:work anymore okay  so here i am running this three commands in the   background and i'll just come here and delete  this and now guys we are currently at 07:25 and   also 54 and we i'm not running any of this  command okay i am not running this command now you can see here guys it has logged this  information automatically we don't have to run   now this php artisan schedule:work anymore it  is running in the background and it will run   every minute and likewise i have said before  that command will look into our this kernel   file and it will just look inside this and  it will run whatever is inside this scheduled   method so that's it for this video guys  and if you have any confusion related to   this then just let me know in the comment  section below and i'll try to solve that   as well so that's it for this video guys  thank you for watching have a great day bye
Info
Channel: Laratips
Views: 53,699
Rating: undefined out of 5
Keywords: laravel for beginner, laravel beginner tutorial, laravel tutorial for beginners, laravel 8 tutorial, laravel 8, laravel tutorial, laravel advanced, laravel 8 advanced, laravel advanced tutorial, laravel 8 advanced tutorial, php, php tips, php tips and tricks, tips and tricks, laratips, laravel, laravel tips
Id: _NoWp58pHa4
Channel Id: undefined
Length: 16min 25sec (985 seconds)
Published: Mon Aug 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.