/** * Migrate the delayed jobs that are ready to the regular queue. * * @param string $from * @param string $to */ public function migrateExpiredJobs(string $from, string $to) { $script = <<<'LUA' local val = redis.call('zrangebyscore', KEYS[1], '-inf', KEYS[3]) 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 true LUA; $this->redis->eval($script, 3, $from, $to, $this->getTime()); }
/** * Copy data from queue to new process list with given size * * @param int $size * @return TaskList */ public function getTaskList($size = 100) { /** * copy part of queue to other redis list */ $taskListUniqueName = $this->getName() . ':' . md5(microtime() . mt_rand()); $queueName = $this->getName(); $queueNameTaskLists = $this->getQueueTaskListsName(); $timestamp = time(); $script = ' local taskListUniqueName = KEYS[1] local queueName = KEYS[2] local size = KEYS[3] local timestamp = KEYS[4] local queueNameTaskList = KEYS[5] -- check if key is unique local is_unique = redis.call("exists", taskListUniqueName) if is_unique == 1 then return 0 end for i=1,size do redis.call("RPOPLPUSH",queueName,taskListUniqueName) end redis.call("hset",queueNameTaskList,taskListUniqueName,timestamp) return 1 '; /** * 1. check llen of queue */ if ($this->getLength() == 0) { throw new Exception("Queue '{$queueName}' is empty or not exists !"); } /** * 2. create task list */ $res = $this->client->eval($script, 5, $taskListUniqueName, $queueName, $size, $timestamp, $queueNameTaskLists); if ($res == 0) { throw new Exception("Task list '{$taskListUniqueName}' exists, it is very rare problem, try run process again !"); } elseif ($res == 1) { return new TaskList($taskListUniqueName, $this); } else { throw new Exception("Strange problem occured ! res = " . var_export($res, true)); } }
/** * Unlock the session data. */ private function unlockSession() { // If we have the right token, then delete the lock $script = <<<LUA if redis.call("GET", KEYS[1]) == ARGV[1] then return redis.call("DEL", KEYS[1]) else return 0 end LUA; if ($this->redis instanceof \Redis) { $this->redis->eval($script, array($this->prefix . $this->lockKey, $this->token), 1); } else { $this->redis->eval($script, 1, $this->prefix . $this->lockKey, $this->token); } $this->locked = false; $this->token = null; }
/** * @param Predis $Instance * @param string $resource * @param string $token * @return mixed */ private function unlockInstance(Predis $Instance, $resource, $token) { $script = ' if redis.call("GET", KEYS[1]) == ARGV[1] then return redis.call("DEL", KEYS[1]) else return 0 end '; return $Instance->eval($script, 1, $resource, $token); }