The Method of Timing Publishing Tasks Based on Laravel Framework

  • 2021-10-27 06:46:21
  • OfStack

In this paper, an example is given to describe the method of implementing the task of publishing regularly in Laravel framework. Share it for your reference, as follows:

Background: Need to create a new task every 1 hour

http://laravelacademy.org/post/8484.html

Laravel officially comes with this function, so it is necessary to take advantage of cronTab function of Linux system.

1. Utilize the cronTab function of Linux

(1) Enter the Linux server environment and enter the command line


crontab -e

This goes directly to the/var/spool/cron/root file under the server. If you don't have an root file under/var/spool/cron/, typing the command above will automatically create a new root file.

(2) Enter the file editing mode first, and enter 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 it is unclear, you can enter it on the command line which php View and modify yourself.

The second directory is where your laravel project is stored, which is the root directory where the artisan files are located

2. Then create a new task in the laravel project directory and enter it on the command line


php artisan make:command SendTask

Then I found one more SendTask. php file under the app/Console/Commands path

Customize the $signature of this file (later to correspond to the name in command in app/Console/Kernel. php file, for example, I wrote task:send) , $description(对这个任务的描述) , handle() Method is the main logic code for this timing task.

3, then modify the app/Console/Kernel. php file

In $commands, you need to add the storage path of the task just now; When multiple tasks are to be executed simultaneously, add the class of the tasks to be executed simultaneously to $commands


protected $commands = [
\App\Console\Commands\SendTasks::class,
\App\Console\Commands\SendTaskEveryDay::class,
\App\Console\Commands\SendTaskEveryWeek::class,

schedule is the core code of timing execution: you can write the content of the task to be executed directly, or you can call the task execution content in the newly built command. The frequency of performing tasks is explained later.


protected function schedule(Schedule $schedule)
{
//  $schedule->call(function () {
//   Task::create(['title' => ' Send timed task ']);
//  })->everyMinute();
// Call artisan
// $schedule->command('tasks:send')->hourly();
$schedule->command('tasks:send')->cron('0 */2 * * *');
$schedule->command('tasks:everyday')->dailyAt('09:00');

In fact, it is already expected to execute the release timing task here, but I hope it can be transmitted to this timing task, because the content of each task is not 1, which needs to achieve the effect that can be customized. I have been struggling here for a long time and tried to modify it handle() Method, modified to handle($data) And then call this when you want to publish a timed task handle() Method, and then pass the parameter through the $data Pass it over, but find 1 denier handle() After the timing task can't be executed with parameters, the specific reason is unknown, but this idea is obviously wrong.

Finally, I took a lot of detours and thought of a way. I couldn't pass the parameters from the outside, so I found out the parameters in the method. What I want is to create a new task, and then the new task needs to be created automatically every 1 hour.

The solution that comes to mind is to handle() Method inside the sql statement or query constructor to find out the new timing task, and then add this task once again. For the time being, I can think of this method, which can at least achieve the effect I want.


$cron=Task::where('type', 'xxx')
 ->first();
$cronTab=$cron->jsonSerialize();
$task= Task::create($cronTab);

For more readers interested in Laravel related content, please check the topics on this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

Hope that this article is based on Laravel framework of PHP programming help.


Related articles: