pheanstalk and beanstalk functions

送分小仙女□ 提交于 2019-11-27 04:52:41

问题


The following code is a snipet taken from an example of pheanstalk being implemented and working properly (obtained from pheanstalk's github page: https://github.com/pda/pheanstalk):

<?php

require_once("vendor/autoload.php");
use Pheanstalk\Pheanstalk;
$pheanstalk = new Pheanstalk('127.0.0.1');

// ------------ producer (queues jobs):

$pheanstalk
  ->useTube('testtube')
  ->put("job payload goes here\n");

// ------------ worker (performs jobs):

$job = $pheanstalk
  ->watch('testtube')
  ->ignore('default')
  ->reserve();

echo $job->getData();
$pheanstalk->delete($job);

// ------------ check server availability

$pheanstalk->getConnection()->isServiceListening(); // true or false

QUESTIONS:

What I don't understand are the following parts:

  1. I am assuming that the newline spaces in the producer code don't make any difference in the execution, so this line would be equivalent:

    $pheanstalk->useTube('testtube')->put("job payload goes here\n");
    

correct? If that is true, then do those specific function calls have to be in that order, or can they be in any order? My previous understanding of functions and classes in php was that you would directly call a function from an object of it's class type: $object->classFunction(), however is the above code a valid php technique where you can call all those functions simultaneously or is it something special to pheanstalk?

  1. What is the ignore('default') code doing?

  2. What is the $pheanstalk->getConnection()->isServiceListening(); code doing?


回答1:


  1. You're correct that the spaces are insignificant. What you're seeing is called method chaining

$pheanstalk->useTube('testtube')->put("job payload goes here\n");

is equivalent to:

$temp = $pheanstalk->useTube('testtube');
$temp->put("job payload goes here\n");

So first it calls useTube() to specify which tube the payload should be put into, then it puts the payload into that. It's depending on the fact that methods that perform actions return the Pheanstalk object they were called on, so it's also short for:

$pheanstalk->useTube('testtube');
$pheanstalk->put("job payload goes here\n");
  1. ignore(tubename) removes that tube from the watchlist. The default tube is watched by default, so this disables that and just waits for messages in the testtube tube.

  2. It's doing exactly what the comment above it says: Checking that the server is available. You could use this in your producer code to report an error before trying to send to a beanstalk server that's not listening.



来源:https://stackoverflow.com/questions/37370688/pheanstalk-and-beanstalk-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!