【问题标题】:Unable to reserve a job with beanstalkd无法使用 beanstalkd 预订工作
【发布时间】:2010-05-12 11:10:32
【问题描述】:

我已经尝试在两台不同的服务器上启动并运行 beanstalkd,并进行了一些测试(本地在 MacOSX 上从源代码编译,以及在安装了 yum 的 CentOS 服务器上)

我可以让服务器运行

sudo beanstalkd -d -p 11300

或

sudo beanstalkd -p 11300 &

然后我尝试使用 php lib,但它只是冻结了。直接连接:

telnet localhost 11300

我做了以下模仿 PHP 测试脚本:

use foo
USING foo
put 0 0 120 5
hello
INSERTED 1
reserve-with-timeout 0
TIMED_OUT

如果我只是跑步

reserve

它被无限期地卡住了。

PHP代码是

    /**
    * BeanStalk 0.10 - Example code
    * 
    * This is a quick example to get you started using the client.
    */

    require(dirname(__FILE__).'/../src/BeanStalk.class.php');

    /**
    * Connect to the beanstalkd server(s)
    * 
    * Option array:
    * 
    *       array(
    *           'servers'               => array( 'ip:port'[, 'ip:port'[, ...]] ),
    *           'select'                => 'random wait',
    *           'connection_timeout'    => 0.5,
    *           'peek_usleep'           => 2500,
    *           'connection_retries'    => 3,
    *           'auto_unyaml'           => true
    *       );
    * 
    * select -> this tells the client what type of blocking to use when selecting from 
    * different servers. There are currently four choices:
    * 
    *   random wait:        pick a random server from the list and wait for a job
    * 
    *   sequential wait:    pick the next server in the list and wait for a job
    * 
    *   random peek:        in a loop, pick a random server and peek-ready(), looking for a job
    *                       until a server is found that has something available.
    * 
    *   sequential peek:    in a loop, pick the next server and peek-ready() ... etc.
    * 
    * the *peek modes have a companion setting, peek_usleep, which tells the client how long
    * to usleep() for between peeks to servers.
    * 
    * auto_unyaml -> if true, this causes the client to assume the presence of the syck yaml
    * parser, and attempts to 'unyamlize' yaml output for you before returning it.
    */
echo "opening\n";
    $beanstalk = BeanStalk::open(array(
        'servers'       => array( '127.0.0.1:11300' ),
        'select'        => 'random peek'
    ));

echo "switching tube\n";

    // As in the protocol doc.
    $beanstalk->use_tube('foo');

echo "putting job\n";

    // As in the protocol doc.
    $beanstalk->put(0, 0, 120, 'say hello world');      // Add a job to the queue with highest priority, 
                                                        // no delay, 120 seconds TTR, with the contents
                                                        // 'say hello world'.

                                                        // NOTE: the put() method here supports a final optional 
                                                        // argument, a tube name. If supplied, the server will
                                                        // first switch to that tube, write the job, then switch
                                                        // back to the old tube again.

echo "trying to reserve\n";

    // As in the protocol doc.
    $job = $beanstalk->reserve();                       // Assuming there was nothing in the queue before 
                                                        // we started, this will give us our 'hello world'
                                                        // job back.

echo "about to output\n";    

    // This is a BeanQueueJob object.
    echo $job->get();                                   // Output: 'say hello world'

    Beanstalk::delete($job);                            // Delete the job.

然后只是冻结在“尝试保留”上。原代码来自:

http://sourceforge.net/projects/beanstalk/

有什么想法吗?提前致谢。

【问题讨论】:

  • 能不能把有问题的PHP代码加进去?也是守护进程的版本('beanstalkd -v')。您也不必总是将它作为守护进程运行 - 它会很高兴地在终端窗口的前台定位。
  • 版本如果是 1.4.4,已经添加了上面的 PHP 代码。感谢您提供关于不以 -d 身份运行的提示,同样的问题也适用,但可以节省时间来杀死无限循环。
  • 快速注意,我可以在不使用新管时进行预订,尽管 list-tubes 命令在使用上述 telnet 命令时会列出“foo”。所以可能只是配置或设置中的某些东西阻止了单独的管子工作。

标签: beanstalkd


【解决方案1】:

要使用默认以外的管子,您似乎需要为该管子添加一个“手表”。例如:

use foo
USING foo
put 0 0 120 5
hello
INSERTED 1
reserve-with-timeout 5
TIMED_OUT
list-tubes
OK 20
---
- default
- foo

watch foo
WATCHING 2
reserve-with-timeout 5
RESERVED 1 5
hello

这在文档中不是很清楚,这似乎暗示“use”命令自动具有使用管的保留命令 - 就像 put 自动使用该管一样。

希望这对其他beantalkd新手有所帮助!

【讨论】:

  • 您可能想看看beanstalkc tutorial(尤其是line 138ff)。尽管本教程是为 Python 客户端库编写的,但它涵盖了 beanstalkd 的大部分方面,也可能对其他语言/库的用户有用。
  • 太棒了,感谢伯爵的链接 - 很容易将其翻译成其他语言/图书馆作为介绍。
  • using关键字是把数据放到管子里,watch关键字是取一个。答案很好,问题作者写给他没看过的管子
【解决方案2】:

https://raw.github.com/kr/beanstalkd/master/doc/protocol.txt

这个文件比较清楚。

“使用”命令适用于生产者。随后的 put 命令会将作业放入 此命令指定的管。如果未发出使用命令,则作业 将被放入名为“default”的试管中。

“watch”命令将命名管添加到当前的观察列表中 联系。保留命令将从中的任何管中获取工作 观察名单。对于每个新连接,监视列表最初包含一个 管,命名为“默认”。

所以在消费者中你不需要调用“使用”,而是调用“手表”

【讨论】:

    【解决方案3】:

    它的设计目的是阻止保留 - 它正在等待工作。在另一个终端中,将一份工作放入试管中 - 您的储备命令将与新工作一起返回。

    【讨论】:

    • 首先,OP使用reserve-with-timeout;这不应该阻塞。其次,他们先把工作放在管子上,然后再保留工作,所以如果其他一切都好(而且管子上没有其他观察者),该工作应该立即可用。
    猜你喜欢
    • 2021-06-05
    • 2013-04-19
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多