In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to achieve the timed release task in Laravel. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
1. Use the cronTab function of Linux
(1) enter the Linux server environment and enter the command line
Crontab-e
This takes you directly to the / var/spool/cron/root file under the server. If there is no root file under / var/spool/cron/, entering the above command will automatically create a new root file.
(2) enter the file editing mode and enter it in the root file.
* / usr/local/php/bin/php / opt/xxx/xxx/artisan schedule:run 1 > > / dev/null 2 > & 1
The first directory above is the location of php on the server. If you are not clear, you can type which php on the command line to see for yourself and then modify it.
The second directory is where your laravel project is stored, which is the root directory where the artisan file is located.
2. Then create a new task under the laravel project directory and enter it on the command line
Php artisan make:command SendTask
Then I found that there was an extra SendTask.php file under the app/Console/Commands path.
Customize the $signature of this file (to correspond to the name in the command in the app/Console/Kernel.php file later, for example, I wrote task:send), $description (description of the task), and the handle () method is the main logic code for this scheduled task.
3. Then modify the app/Console/Kernel.php file
Add the storage path of the task in $commands; when you want to execute multiple tasks at the same time, add the class of the task to be executed at the same time to $commands
Protected $commands = [\ App\ Console\ Commands\ SendTasks::class,\ App\ Console\ Commands\ SendTaskEveryDay::class,\ App\ Console\ Commands\ SendTaskEveryWeek::class
Schedule is the core code for regular execution: you can write the content of the task to be executed directly in it, or you can call the task execution content in the newly created command. The frequency of the task is described later.
Protected function schedule (Schedule $schedule) {/ / $schedule- > call (function () {/ / Task::create (['title' = >' send scheduled tasks']); / /})-> everyMinute (); / / call artisan// $schedule- > command ('tasks:send')-> hourly (); $schedule- > command (' tasks:send')-> cron ('0 * / 2 *'); $schedule- > command ('tasks:everyday')-> dailyAt (' 09schedule-)
In fact, it is already possible to perform the release scheduled task here, but I hope to be able to transfer the reference to this scheduled task, because the content of each task is different and needs to achieve a customizable effect. After entangling here for a long time, I also tried to modify the handle () method to change it to handle ($data), then call this handle () method when I want to publish a scheduled task, and then pass the parameters through $data, but found that once handle () contains parameters, the scheduled task cannot be executed, the specific reason is not known, but this idea is obviously wrong.
In the end, I took a lot of detours and came up with a way that the parameters could not be transmitted from the outside, so the parameters were found out in the method. The effect I want is to create a new task, and then the new task needs to be automatically created every hour.
The solution is to use SQL statements or query constructors in the handle () method to find out the newly created scheduled task and then add it again. This is what I can think of for the time being, at least it can achieve the effect I want.
$cron=Task::where ('type',' xxx')-> first (); $cronTab=$cron- > jsonSerialize (); $task= Task::create ($cronTab); after reading the above, do you have any further understanding of how to implement the timed release task in Laravel? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.