Foreword
Hello everyone, I am back again!
You probably know (if you don’t know now) Bill Gates’ famous quote:
“I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it. ”
Because it’s lazy it’s human nature already, because of being lazy, I’ve neglected this Laravel series for a long time. And thanks to that laziness, I decided to choose to write about Task Scheduling to guide newbie brothers to solve the “laziness” when doing projects.
Just like that, let’s start!
content
What is Task Schedule
Task Schedule name sure enough to say what is its function, right “Scheduling jobs”. That is, you will be ready to do the work to be repeated in a certain cycle and assigned to the server to perform instead of having to run on “rice”.
If you have registered a genuine mobile phone sim, then on your birthday you will surely receive a message of affection like this.
And the problem is, every day hundreds of thousands of customers have a birthday, and this will have to be repeated daily until “no one uses that network anymore”.
And the solution to that problem is the Task Schedule !
How to use the Task Schedule
Cronjob
First to use the Task Schedule, you must understand the concept Cron (Cronjob) has (it’s like to know ahead run, they must know, want to know right away they know this cow).
Cronjob is a tool that automatically implements jobs / tasks automatically in a certain cycle on the system, specifically here on the server (Unix / Linux).
And the jobs / tasks to run automatically are declared in a file called crontab, and to work with this crontab you will need the following commands:
1 2 3 4 5 | <span class="token function">crontab</span> -e: tạo hoặc chỉnh sửa <span class="token function">file</span> <span class="token function">crontab</span> <span class="token function">crontab</span> -l: hiển thị <span class="token function">file</span> <span class="token function">crontab</span> <span class="token function">crontab</span> -r: xóa <span class="token function">file</span> <span class="token function">crontab</span> <span class="token function">service</span> <span class="token function">cron</span> start: khởi động lại <span class="token function">crontab</span> |
After creating / opening crontab file, we need to understand the structure / syntax of crontab. It should look like this:
1 2 3 4 5 6 7 8 | * * * * * command/job cần thực hiện <span class="token comment"># Trong đó ý nghĩa các dấu * lần lượt từ trái sang phải như sau:</span> <span class="token comment"># 1. phút thực hiện (từ 0 đến 59 và *)</span> <span class="token comment"># 2. giờ thực hiện (từ 0 đến 23 và *)</span> <span class="token comment"># 3. ngày thực hiện (ngày trong tháng, từ 1 đến 31 và *)</span> <span class="token comment"># 4. tháng trong năm (từ 1 đến 12 và *)</span> <span class="token comment"># 5. thứ trong tuần (từ 0 đến 6 và *)</span> |
If you ask me why there is a starting from 0 and some start from 1, I do not know, simply because the person who created it
And if you ask why it is not only a numeric value but it also receives the value *
, I can explain, when using *
it will receive all possible values, for example * in the first position. ie command / job that will run once a minute.
Task Schedule in Laravel project
Okay, that’s enough to say. Now we will come to the main part of the article, which is to use the Task Schedule in the Laravel project.
To run a command, you must first create a command first, and the divine command php artisan
again appears.
1 2 | php artisan make:command TenCommand |
After creating, go to the path app/Console/Commands/TenCommand
and add the code you want to run into it, it will look like the following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token keyword">namespace</span> <span class="token package">App Console Commands</span> <span class="token punctuation">;</span> <span class="token keyword">use</span> <span class="token package">Illuminate Console Command</span> <span class="token punctuation">;</span> <span class="token keyword">class</span> <span class="token class-name">TenCommand</span> <span class="token keyword">extends</span> <span class="token class-name">Command</span> <span class="token punctuation">{</span> <span class="token comment">/** * The name and signature of the console command. * * @var string */</span> <span class="token keyword">protected</span> <span class="token variable">$signature</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'command:name'</span> <span class="token punctuation">;</span> <span class="token comment">//tên để gọi thực thi command</span> <span class="token comment">/** * The console command description. * * @var string */</span> <span class="token keyword">protected</span> <span class="token variable">$description</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'Command description'</span> <span class="token punctuation">;</span> <span class="token comment">//mô tả chi tiết công việc mà command sẽ thực hiện</span> <span class="token comment">/** * Create a new command instance. * * @return void */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">__construct</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">parent</span> <span class="token punctuation">:</span> <span class="token punctuation">:</span> <span class="token function">__construct</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/** * Execute the console command. * * @return mixed */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">handle</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// xử lí logic khi mà command được thực thi</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> </span> |
Once you have the command, you will need to declare it in the Kernel.php file, specifically in the function schedule()
, where you will declare the frequency of running the command as follows:
1 2 3 4 5 | <span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function">schedule</span> <span class="token punctuation">(</span> Schedule <span class="token variable">$schedule</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$schedule</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">command</span> <span class="token punctuation">(</span> <span class="token single-quoted-string string">'command:name'</span> <span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token operator">></span> <span class="token function">everyMinutes</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token comment">// command sẽ chạy mỗi phút 1 lần</span> <span class="token punctuation">}</span> |
In addition, to declare the frequency of running commands, there are some common options such as
daily()
: run the command daily at 12:00hourly()
: run the command for hourscron('* * * * *')
: run the command on a custom schedule, like the above declaration- …
To learn more about other declared frequencies (suitable to your needs), you can refer here.
The command declaration in the project has finished, now let the server be declared:
- Run the command
1 2 | <span class="token function">crontab</span> -e <span class="token comment">#tạo hoặc edit crontab</span> |
- Add to the line
1 2 | * * * * * php ~/đường-dẫn-đến-project/artisan schedule:run |
If you understand what I wrote above, I will understand that this line informs the server that it should perform schedule:run
every 1 minute.
And so, all the commands declared in function schedule()
will also be executed in accordance with the frequency that you declare.
Too simple is not it!
Epilogue
In short, I firmly believe that the task schedule will contribute a lot in your Website, whether it’s the actual product or just a project to study.
And if you find the article useful, please upload or clip to easily review it, and if the article is still not satisfied you, leave a comment to answer and improve more in the following posts!
See you in the next posts in this Laravel series , I promise to update more posts more often!
Thank you very much!
References
- Laravel Document: https://laravel.com/docs/6.x/scheduling