I found this gearman by chance and thought it was very useful and very suitable for PHP to run some businesses.
Don't say much. Install it.
Find using apt
sudo apt search gearman
Found this
gearman/bionic,bionic 1.1.18+ds-1 all Distributed job queue
OK, start the installation
sudo apt-get install gearman
OK, the installation is complete, and then check to see if it runs
ps -ef |grep gearman
Found running
$ ps -ef |grep gearman gearman 1660 1 0 02:54 ? 00:00:00 /usr/sbin/gearmand --pid-file=/run/gearman/gearmand.pid --listen=localhost --daemon --log-file=/var/log/gearman-job-server/gearmand.log vagrant 2310 1452 0 02:54 pts/0 00:00:00 grep --color=auto gearman
OK, then start to install the PHP extension (because my PHP version is 7.4, I directly check the corresponding version extension of 7.4 here)
$ sudo apt search php7.4-gearman Sorting... Done Full Text Search... Done php7.4-gearman/bionic 2.1.0+1.1.2-5+ubuntu18.04.1+deb.sury.org+1 amd64 PHP wrapper to libgearman
Then start installing the extension
sudo apt-get install php7.4-gearman
After installation, use php -m to filter to see if it is enabled
$ php -m |grep gearman gearman
Find it, and then start building two files
add_job.php
<?php $client = new GearmanClient(); $client->addServer('127.0.0.1', 4730); $res1 = $client->doBackground('runLaterJob', json_encode(array( 'uid' => 'test user id', 'title' => 'Add a code title that requires asynchronous execution at the delay', 'body' => 'Execute the details of this asynchrony', 'sleep_time' => 2, )));
Create another work_job.php
<?php $worker = new GearmanWorker(); $worker->addServer('127.0.0.1', 4730); $worker->addFunction('runLaterJob', function($job) { $data = json_decode($job->workload(), true); if (isset($data['sleep_time']) && $data['sleep_time'] > 0) { sleep($data['sleep_time']);//Delay processing if necessary } echo "Processing tasks uid:{$data['uid']}--title:{$data['title']}--body:{$data['body']} success\n"; }); //Dead cycle waiting job Tasks submitted while($worker->work());
Start work_job.php
php work_job.php
If there is no output on the screen, start add again_ job.php
php add_job.php
Then add_job.php runs quickly, and then you see work_ Job.php has output in 2 seconds
Processing tasks uid:test user id--title:Add a code title that requires asynchronous execution at the delay--body:The execution of the asynchronous concrete content is successful
Here we can see that the person who adds a job can do other things immediately after delivering the task. The person who consumes the job in the work can slowly process the business, which is very suitable for processing the delayed closing of orders.
For example, if no payment is made within 20 minutes after the order is created, the inventory will be released automatically. Then such a job can be delivered when the order is created, and then the subsequent consumer job will process the order after the set 20 minutes to decide whether it needs to release the inventory (if payment is required, no action will be taken. If payment has not been made, the inventory will be cancelled and the order will be set as overdue and unpaid)
In the past, we used crontab to regularly check which orders need to be closed and which orders need to be released. However, this highly depends on the frequency of execution time set by crontab. It can't be set to run every 1 minute. If it is set to run every 10 minutes, for example, we set it to 08:00, 08:10, 08:20, etc., it will appear that we just finished running at 8:00 The order creation time of the user who placed the order after the scheduled task is 08:01, so for it, it should be closed at 08:21, but at this time, the scheduled task at 08:20 has been completed, so the order is scanned at 08:30, which means that it has actually waited for 29 minutes, which is unreasonable to a certain extent.
With this task distribution, its tasks can be processed automatically in the background.