Search code examples
phpbeanstalkdpheanstalk

pheanstalk search fast a job


I have problem with queue in pheanstalk (version: 3.0.2). In queue is 0-10k jobs and sometimes I must search in data in this queue and add next jobs. So I want to add jobs that doesn't exist.

In the class Pheanstalk I don't see method that search in job and doesn't move this job to "current-jobs-reserved".

So I need fast method that only reads data in job without reserved.

My example:

   public function searchId($id)
    {
        $pheanstalk = $this->getPhenstalk();

        while ($job = $pheanstalk->reserveFromTube(self::TUBE)) {
            $json = $job->getData();
            $data = json_decode($json, true);

            if($data['id'] == $id){
                return true;
            }
            $pheanstalk->release($job);

        }
        return false;
    }

But relase() need many time. How I can do it?


Solution

  • As in the 'How can I get a list of all jobs in a beanstalk tube?' answer, Beanstalkd is not an array. It is not designed to be searched, just to get the next job to work on.

    What you are doing with the mass-reserve/release is just putting them right back in the queue, quite possibly to be the very next thing that you reserve, and so on in an endless loop.

    If you want to see if a particular job-task/name/ID is in the queue, make a note elsewhere, in another datastore - Redis or memcached for example. Make an entry when you put the job into the queue, and remove it when the job has been deleted from Beanstalkd.