Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

php - pheanstalk and beanstalk functions

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 PheanstalkPheanstalk;
$pheanstalk = new Pheanstalk('127.0.0.1');

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

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

// ------------ 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
    ");
    

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  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
");

is equivalent to:

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

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
");
  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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...