Recently encountered a problem in the work, open multiple process processing queue will read the Redis queue repeatedly? Does this result in repetitive tasks being performed? This article mainly introduces to you about Laravel Redis multiple processes simultaneously take the queue problem related data, the article through the sample code introduced very detailed, to everybody's study or the work has the certain reference learning value, needs the friend below to study together to learn. Hope to help everyone.
Use Supervisor to listen for Laravel queue tasks, where Supervisor is configured as follows:
[program:laravel-worker]process_name=% (Program_name) s_% (process_num) 02dcommand=php/var/www/xxx.cn/artisan queue : Work--queue=sendfile--tries=3--daemonautostart=trueautorestart=truenumprocs=8redirect_stderr=truestdout_ Logfile=/var/www/xxx.cn/worker.log
Note: Numprocs = 8, which represents the opening of 8 processes to execute commands in a command.
As follows:
PS c:\users\tanteng\website\laradock> docker-compose exec php-worker sh/etc/supervisor/conf.d # Ps-ef | grep php 7 root 0:00 php/var/www/xxx.cn/artisan queue:work--queue=sendfile--tries=3--daemon 8 root 0:00 php/v Ar/www/xxx.cn/artisan queue:work--queue=sendfile--tries=3--daemon 9 root 0:00 Php/var/www/xxx.cn/artisan queue: Work--queue=sendfile--tries=3--daemon root 0:00 php/var/www/xxx.cn/artisan queue:work--queue=sendfile-- tries=3--daemon root 0:00 php/var/www/xxx.cn/artisan queue:work --queue=sendfile--tries=3--daemon 0:00 Php/var/www/xxx.cn/artisan queue:work--queue=sendfile--tries=3--daemon root 0:00 php/var/www/xxx.cn/ Artisan Queue:work--queue=sendfile--tries=3--daemon root 0:00 php/var/www/xxx.cn/artisan queue:work--queue= Sendfile--tries=3--daemon-root 0:00 grep php
Laravel multi-process Read queue content is duplicated
In a Laravel controller method, put multiple task queues at once:
Public Function Index (Request $request) {$this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this Dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispAtch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile ')); $this->dispatch ((New SendFile3 ())->onqueue (' Sendfile '));}
In the queue processing method, print the log, print the ID of the queue that is processed:
app/jobs/sendfile3.php
Public function handle () {info (' invoke SendFile3 '); dump (' Invoke handle '); $rawbody = $this->job->getrawbody (); $ info = Json_decode ($rawbody, true); Info (' Queue ID: '. $info [' id ']);}
Laravel uses the Redis list as the data structure for the queue and assigns an ID to each queue, with the following data structure:
{"Job": "Illuminate\\queue\\callqueuedhandler@call", "data": {"CommandName": "App\\jobs\\sendfile3", "command": "O : 18:\ "app\\jobs\\sendfile3\": 4:{s:6:\ "\u0000*\u0000job\"; N;s:10:\ "connection\"; N;s:5:\ "Queue\", s:8:\ "sendfile\"; s:5:\ "delay\"; N;} "}," id ":" hadbcy3ipnsnoofqqdhohsa451okqs88 "," Attempts ": 1}
Request this controller route (or command line), you can see many more queue tasks in Redis,
This time turn on the Supervisor Processing Queue task and view the log:
[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local. Info:invoke sendfile3[2017-12-23 19:01:01] local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue ID: jacljzhdevntzlcriz6urqkcvlbe8y9c[2017-12-23 19:01:01] Local.INFO:queue id:ukhv0li4p2vgpa55qu6yeojm27mo5ywj[ 2017-12-23 19:01:01] local.INFO:queue id:obmpwdtmnavebuku7aan5abt3agyt90l[2017-12-23 19:01:01] Local.INFO:queue ID: fo2qzn2ftsdqtdnkocimk7ijb4qlhrge[2017-12-23 19:01:01] Local.INFO:queue id:uljfmoou7wk7boad4zphb3ccrmjhbtr6[ 2017-12-23 19:01:01] local.INFO:queue id:87ulqpbobfmgr16nl5wxfvoi71zgcerm[2017-12-23 19:01:01] Local.INFO:queue ID : 9uvl0muqlzbqlri99rchgw2elxwvemie[2017-12-23 19:01:01] Local.INFO:queue Id:a0vgyzuz9htmh7dghepxqesftcqu3qaf[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue id:2cxuxxoppkgyiv4wo8gv9cj6cwxektyl[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[ 2017-12-23 19:01:01] local.INFO:queue id:9actaya8cxpjx6q3gb1sulokotp8reqz[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue ID: bphqvbbochlv4gr2i0vylvyw9bijttyj[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO: Queue id:fm6tnajdxyktdqbdmydmwwjflnnikryg[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue id:nyabcvskbvpbah3e2itqkoljlp1ficib[2017-12-23 19:01:01] Local.INFO:queue ID: wbhssvztp43569uopxxflljcvympw7cp[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO: Queue id:blipnkcrsdapwvmklnxehakelhm0rdey[2017-12-23 19:01:01] local.INFO:invoke sendfile3[2017-12-23 19:01:01] LocAl.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue id:eoaoquceiwrz9uz64xm6idkgiqj9xc3w[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue id:lzise9eiqqqinrhalbmai4qng7qylpb2[ 2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue ID: wxykvcfohs1ppnwowutsenomv5l5euxe[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO: Queue id:xth5jiwlgnrwwzi02oyi70pihaokujud[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue id:9ehme5himlpnubpy0xwn8uvrozxemqws[2017-12-23 19:01:01] Local.INFO:queue id:c1sk87cpzl47edla0zhfo7pj9miecoyx[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[ 2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01 ] Local.INFO:queue id:2kwl51oh4lyyrrljcregucknijrdl7oe[2017-12-23 19:01:01] Local.INFO:queue ID:OBRPOQRytpyiyv2delmloxu3sappwjln[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue ID : 6qgu6w3tapljsrt688yv9hrxvddlxntz[2017-12-23 19:01:01] Local.INFO:queue id:witlerhwn7s9cqkfuf9lllnadpxjknci[ 2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue ID: zslw0vlfbdpl4wjtjzu3yb3v45pne807[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO: Queue id:qhzlxlgfgwrluienm7vbllmtjzyb2h5n[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue id:lux1ibyd3l2psnl9bzwhhk2knxyrpzw6[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue id:m2respjyo5hpafxxl0eqbwwsuq4jpmwn[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[ 2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue ID: husgaiiaoo6zfgqc5kghgpsv5rporpyo[2017-12-23 19:01:01] Local.INFO:queue id:cehjsoy6blez4nbncpziahqlarmeyyef[ 2017-12-23 19:01:01] LocAl.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue id:w4bkfijkmu5saqg2xkn3zrl5byxgatmk[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] Local.INFO:queue id:0zbuwbxlrehhxkfybkvyty4z35f154si[ 2017-12-23 19:01:01] local.INFO:queue id:mvozvydpvq4tcpjey9g7pmth3mwpkpik[2017-12-23 19:01:01] Local.INFO:invoke sendfile3[2017-12-23 19:01:01] local.INFO:queue id:tlvf74eeidecwktjzqwvw03ujtrptl9r[2017-12-23 19:01:01] Local.INFO : Queue id:me8wypfgcz0nf9xvcxz0hf2xvxqa1ffs
These 8 processes handle the queue concurrently, but from the printed log, the same ID does not appear. Let's take a look at how Laravel uses Redis to process queues.
Analyze the processing of the Laravel queue
Laravel in Queue method
Public Function Pushraw ($payload, $queue = null, array $options = []) {$this->getconnection ()->rpush ($this, Getqueue ($queue), $payload); Return Arr::get (Json_decode ($payload, true), ' id ');}
The Rpush command for Redis is used.
Laravel Queue method
Public Function pop ($queue = null) {$original = $queue?: $this->default; $queue = $this->getqueue ($queue); $this->migrateexpiredjobs ($queue. ':d elayed ', $queue); if (! Is_null ($this->expire)) { $this->migrateexpiredjobs ($queue. ': Reserved ', $queue);} List ($job, $reserved) = $this->getconnection ()->eval ( luascripts::p op (), 2, $queue, $queue. ': Reserved ', $ This->gettime () + $this->expire); if ($reserved) { return new Redisjob ($this->container, $this, $job, $reserved, $original);}}
Here is the LUA script fetch queue, as follows:
public static function Pop () {return <<< ' LUA ' local job = Redis.call (' Lpop ', keys[1]) local reserved = Falseif (Job ~= false) thenreserved = Cjson.decode (Job) reserved[' attempts '] = reserved[' attempts '] + 1reserved = Cjson.encode ( Reserved) redis.call (' Zadd ', keys[2], argv[1], reserved) endreturn {job, Reserved}lua;}
The conclusion is that, from the processing of Laravel and the printed log results, even if multiple processes read the same queue, they will not read the same data.