Ways to implement timed tasks using Laravel

Source: Internet
Author: User
This article mainly introduces the Laravel implementation of the Timing task sample code, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.

Brief introduction

Timed tasks are a very common requirement in the backend development process, which often occurs in data statistics, garbage cleanup and other scenarios. Laravel provides a complete set of timing task tools, so that we only need to focus on the logic, the rest of the basic work will be borne by it.

Basic usage

Generate commands

PHP Artisan Make:command Areyouok

5.2 and previous versions, this command is ' PHP artisan make:console xxx '

Edit command

To edit the ' app/console/commands/areyouok.php ' file, modify the following:

... protected $signature = ' Areyou:ok '; Command name protected $description = ' Lei June, the most singing man in the Tech circle '; Command description, nothing with public function __construct () {  parent::__construct ();  The initialization code is written here, and it's nothing.}public function handle () {  //function code written here}

Registration command

Edit the ' app/console/kernel.php ' file to register the newly generated class:

protected $commands = [  \app\console\commands\areyouok::class,];

Write the invocation logic:

protected function Schedule (schedule $schedule) {  $schedule->command (' Areyou:ok ')       ->timezone (' asia/ Shanghai ')       ->everyminute ();}

The logic above is called once per minute. Laravel provides a time function of various lengths from one minute to one year, which can be called directly.

Register this Laravel project in the system cron.

To edit the '/etc/crontab ' file, add the following code:

* * * * * * Root/usr/bin/php/var/www/xxxlaravel/artisan schedule:run >>/dev/null 2>&1

The '/var/www/xxxlaravel ' in the line above needs to be changed to the actual path.

Kindle

Restart Cron to activate this feature: ' Systemctl restart Crond.service ', done!

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.