Application Scenarios
First, mass business
The characteristics of these businesses are not very real-time, can guarantee statistics, support to review progress, can be unsuccessful tasks back to the queue, but also can manually eliminate some repetitive abnormal tasks.
Mass SMS
Bulk Mail
Group push
Second, data migration services
Because the characteristics of the database can not be a library to go to the world, so when users publish content, can be more real-time data migration
Requires high availability
Processing is best to be concurrent (because the business is small so it's best
Strong real-time (if the real-time is not strong, user release action, a page jump in the network I/O time will cause the user library trouble)
According to the current unknown enthusiastic answer simple summary
First, crontab + PHP
Timing ability is not strong, although you can run multiple crontab, PHP do their own timer
Also related to locks, or some of the tasks are not to repeat the run
Second, Php-resque Https://github.com/chrisboulton/php-resque
Are you supporting Redis?
Is it convenient to use within the frame such as YII2?
Why didn't anyone mention Swoole workman?
In any case, I have ordered the praise .....
Reply content:
Application Scenarios
First, mass business
The characteristics of these businesses are not very real-time, can guarantee statistics, support to review progress, can be unsuccessful tasks back to the queue, but also can manually eliminate some repetitive abnormal tasks.
Mass SMS
Bulk Mail
Group push
Second, data migration services
Because the characteristics of the database can not be a library to go to the world, so when users publish content, can be more real-time data migration
Requires high availability
Processing is best to be concurrent (because the business is small so it's best
Strong real-time (if the real-time is not strong, user release action, a page jump in the network I/O time will cause the user library trouble)
According to the current unknown enthusiastic answer simple summary
First, crontab + PHP
Timing ability is not strong, although you can run multiple crontab, PHP do their own timer
Also related to locks, or some of the tasks are not to repeat the run
Second, Php-resque Https://github.com/chrisboulton/php-resque
Are you supporting Redis?
Is it convenient to use within the frame such as YII2?
Why didn't anyone mention Swoole workman?
In any case, I have ordered the praise .....
Php-resque, with Dashborad to view task execution
Php-resque-scheduler supports delayed execution of tasks, such as how many hours later
The ruby version of Resque seems to support cron, but the PHP version of Resque is not supported.
Don't think about using PHP Resque, based on Redis, fast enough!
The script, the constant consumption
Crontab
PHP script, life cycle set to 10min
Crontab is scheduled to be called 5 minutes at a time to ensure that 2 tasks are always working
===========================================
For the first question, do a task queue, can be placed in the Redis queue, which is single-threaded, to ensure that no two processes to get a task
With the queue, you can look at Php_resque.
Crontab can also, but the frequency is relatively fixed not very good control.
Write a script loop, you need to open a daemon process, or the process is not known to hang, not recommended.
You can try the multithreading of PHP: Pthreads
In fact, a PHP script has been used to get data from MQ and consume the data. It is also possible to use supervisor or Daemonise as a guardian. However, the PHP script still has a maximum running time is better, so that you can periodically release some of the memory is not freed, and then restarted by the daemon process
Laravel solved this problem in two ways:
Crontab, run once every minute.
Queue (Redis+supervisord). , is actually supervisord constantly up the process to consume, the consumption process stopped, supervisor detected and then re-start the process. Suitable for small concurrency.
summed up nothing: Dead loop/trigger.
while(true){ //如果队列空了 sleep几秒 //处理过程 //处理完一条usleep几毫秒,别把服务器拖垮}
Daemon this piece can use Swoole to make it.
You can use Swoole
My practice is Workman
to execute a cycle of death, and of course swoole
it's good.
Simple for the consumer queue common practice basically almost
Configure crontab, run every minute, use a shell,
This shell is to determine if there is currently a consuming process in the shell when it is a dead loop
For example
if ( 'test.php'进程存在 ) { exit()}while( true ){ sleep 1 php test.php}
With supervisor or daemonise to do the Guardian, combined with MQ, the general business is not a problem