Home Backend Development PHP Tutorial Lua script analysis of Laravel delay queue implementation

Lua script analysis of Laravel delay queue implementation

Apr 16, 2018 pm 02:29 PM
laravel accomplish Script

The main content of this article is about the Lua script analysis of Laravel delay queue implementation. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

Laravel implements Redis delay Lua scripts are used in queues to ensure the atomicity of operations between different queues
In Laravel5.1, four Lua script methods are mainly used to ensure the atomicity of different queue operations

1. Count the number of queue tasks Method

1.llen counts the number of list queues

2.zcard counts the amount of zset queue data

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

/**

 * Get the Lua script for computing the size of queue.

 *

 * KEYS[1] - The name of the primary queue

 * KEYS[2] - The name of the "delayed" queue

 * KEYS[3] - The name of the "reserved" queue

 *

 * @return string

 */

public static function size()

{

    return <<<&#39;LUA&#39;

         return redis.call(&#39;llen&#39;, KEYS[1]) + redis.call(&#39;zcard&#39;, KEYS[2]) +

         redis.call(&#39;zcard&#39;, KEYS[3])

    LUA;

}

Copy after login

2. Put the pop queue task into the reserved queue

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

/**

 * Get the Lua script for popping the next job off of the queue.

 *

 * KEYS[1] - The queue to pop jobs from, for example: queues:foo

 * KEYS[2] - The queue to place reserved jobs on, for example: queues:foo:reserved

 * ARGV[1] - The time at which the reserved job will expire

 *

 * @return string

 */

public static function pop()

{

  return <<<&#39;LUA&#39;

      -- Pop the first job off of the queue...

     local job = redis.call(&#39;lpop&#39;, KEYS[1])

     local reserved = false

 

     if(job ~= false) then

        -- Increment the attempt count and place job on the reserved queue...

        reserved = cjson.decode(job)

        reserved[&#39;attempts&#39;] = reserved[&#39;attempts&#39;] + 1

        reserved = cjson.encode(reserved)

        redis.call(&#39;zadd&#39;, KEYS[2], ARGV[1], reserved)

     end

    return {job, reserved}

  LUA;

}

Copy after login

3. Add the tasks from the reserved queue to the delayed queue

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

/**

 * Get the Lua script for releasing reserved jobs.

 *

 * KEYS[1] - The "delayed" queue we release jobs onto, for example: queues:foo:delayed

 * KEYS[2] - The queue the jobs are currently on, for example: queues:foo:reserved

 * ARGV[1] - The raw payload of the job to add to the "delayed" queue

 * ARGV[2] - The UNIX timestamp at which the job should become available

 *

 * @return string

 */

public static function release()

{

    return <<<&#39;LUA&#39;

       -- Remove the job from the current queue...

       redis.call(&#39;zrem&#39;, KEYS[2], ARGV[1])

       -- Add the job onto the "delayed" queue...

       redis.call(&#39;zadd&#39;, KEYS[1], ARGV[2], ARGV[1])

       return true

    LUA;

}

Copy after login

4. Merge the tasks that meet the reserved queue time into the execution queue

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

    /**

     * Get the Lua script to migrate expired jobs back onto the queue.

     *

     * KEYS[1] - The queue we are removing jobs from, for example: queues:foo:reserved

     * KEYS[2] - The queue we are moving jobs to, for example: queues:foo

     * ARGV[1] - The current UNIX timestamp

     *

     * @return string

     */

    public static function migrateExpiredJobs()

    {

        return <<<&#39;LUA&#39;

        -- Get all of the jobs with an expired "score"...

           local val = redis.call(&#39;zrangebyscore&#39;, KEYS[1], &#39;-inf&#39;, ARGV[1])

 

        -- If we have values in the array, we will remove them from the first queue

        -- and add them onto the destination queue in chunks of 100, which moves

        -- all of the appropriate jobs onto the destination queue very safely.

           if(next(val) ~= nil) then

             redis.call(&#39;zremrangebyrank&#39;, KEYS[1], 0, #val - 1)

 

             for i = 1, #val, 100 do

               redis.call(&#39;rpush&#39;, KEYS[2], unpack(val, i, math.min(i+99, #val)))

             end

           end

          return val

        LUA;

}

Copy after login

                           

The above is the detailed content of Lua script analysis of Laravel delay queue implementation. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Mar 31, 2025 pm 11:24 PM

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...

In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

The method of handling Laravel's email failure to send verification code is to use Laravel...

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

Laravel - Dump Server Laravel - Dump Server Aug 27, 2024 am 10:51 AM

Laravel - Dump Server - Laravel dump server comes with the version of Laravel 5.7. The previous versions do not include any dump server. Dump server will be a development dependency in laravel/laravel composer file.

Laravel Redis connection sharing: Why does the select method affect other connections? Laravel Redis connection sharing: Why does the select method affect other connections? Apr 01, 2025 am 07:45 AM

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Apr 01, 2025 am 09:09 AM

Custom tenant database connection in Laravel multi-tenant extension package stancl/tenancy When building multi-tenant applications using Laravel multi-tenant extension package stancl/tenancy,...

Laravel - Action URL Laravel - Action URL Aug 27, 2024 am 10:51 AM

Laravel - Action URL - Laravel 5.7 introduces a new feature called “callable action URL”. This feature is similar to the one in Laravel 5.6 which accepts string in action method. The main purpose of the new syntax introduced Laravel 5.7 is to directl

See all articles