Laravel延迟队列实现之Lua脚本解析

不言
不言 原创
2023-03-24 06:10:02 2084浏览

这篇文章主要介绍的内容是关于Laravel延迟队列实现之Lua脚本解析,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

Laravel在实现Redis延迟队列时使用了Lua脚本保证不同队列间操作的原子性
在Laravel5.1中主要是通过4个Lua脚本方法保证不同队列操作的原子性的

一、统计队列任务数量方法

1.llen 统计list队列数量

2.zcard统计zset队列数据量

    /**
     * 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 <<<'LUA'
             return redis.call('llen', KEYS[1]) + redis.call('zcard', KEYS[2]) + 
             redis.call('zcard', KEYS[3])
        LUA;
    }

二、pop队列任务放入reserved(保留)队列

    /**
     * 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 <<<'LUA'
          -- Pop the first job off of the queue...
         local job = redis.call('lpop', 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['attempts'] = reserved['attempts'] + 1
            reserved = cjson.encode(reserved)
            redis.call('zadd', KEYS[2], ARGV[1], reserved)
         end
        return {job, reserved}
      LUA;
    }

三、将任务由添加reserved队列到delayed队列中

    /**
     * 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 <<<'LUA'
           -- Remove the job from the current queue...
           redis.call('zrem', KEYS[2], ARGV[1])
           -- Add the job onto the "delayed" queue...
           redis.call('zadd', KEYS[1], ARGV[2], ARGV[1])
           return true
        LUA;
    }

四、将reserved队列满足时间的任务合并到执行队列中

    /**
     * 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 <<<'LUA'
        -- Get all of the jobs with an expired "score"...
           local val = redis.call('zrangebyscore', KEYS[1], '-inf', 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('zremrangebyrank', KEYS[1], 0, #val - 1)

             for i = 1, #val, 100 do
               redis.call('rpush', KEYS[2], unpack(val, i, math.min(i+99, #val)))
             end
           end
          return val
        LUA;
}

以上就是Laravel延迟队列实现之Lua脚本解析的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。